Skip to content

Commit

Permalink
ok we got the fakes buddy we're rolling.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gnuxie committed Nov 4, 2024
1 parent f611aa7 commit c51ebaf
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 6 deletions.
1 change: 0 additions & 1 deletion src/Protection/ProtectionHandles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ test('handlePermissionRequirementsMet is called when a new room is added with me
};
const protectionAddResult = await protectedRoomsSet.protections.addProtection(
protectionDescription,
{},
protectedRoomsSet,
undefined
);
Expand Down
25 changes: 25 additions & 0 deletions src/Protection/ProtectionsConfig/FakeProtectionsConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-FileCopyrightText: 2024 Gnuxie <[email protected]>
//
// SPDX-License-Identifier: AFL-3.0

import { StandardPersistentConfigData } from '../../Config/PersistentConfigData';
import { FakePersistentConfigBackend } from '../../Interface/FakePersistentMatrixData';
import { StandardLoggableConfigTracker } from '../../Interface/LoggableConfig';
import { MjolnirEnabledProtectionsDescription } from '../ProtectionsConfig/MjolnirEnabledProtectionsDescription';
import { MjolnirProtectionsConfig } from '../ProtectionsConfig/StandardProtectionsConfig';

export class FakeProtectionsConfig extends MjolnirProtectionsConfig {
public constructor() {
super(
new StandardPersistentConfigData(
MjolnirEnabledProtectionsDescription,
new FakePersistentConfigBackend({})
),
new StandardLoggableConfigTracker(),
{
knownEnabledProtections: [],
unknownEnabledProtections: [],
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,23 @@ import { ProtectionCapabilityProviderSetConfig } from './ProtectionCapabilityPro
export class StandardProtectionCapabilityProviderSetConfig
implements ProtectionCapabilityProviderSetConfig
{
private readonly activeProviders = new Map<string, CapabilityProviderSet>();
public async storeActivateCapabilityProviderSet(
protectionDescription: ProtectionDescription,
capabilityproviderSet: CapabilityProviderSet
): Promise<Result<void>> {
this.activeProviders.set(protectionDescription.name, capabilityproviderSet);
return Ok(undefined);
}
public async getCapabilityProviderSet<
TProtectionDescription extends
ProtectionDescription = ProtectionDescription,
>(
protectionDescription: TProtectionDescription
): Promise<Result<CapabilityProviderSet>> {
return Ok(protectionDescription.defaultCapabilities);
return Ok(
this.activeProviders.get(protectionDescription.name) ??
protectionDescription.defaultCapabilities
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ export class MjolnirProtectionSettingsConfig
) {
// nothing to do mare.
}
public async storeProtectionSettings(
protectionDescription: ProtectionDescription,
settings: Record<string, unknown>
): Promise<Result<void>> {
const persistentConfigData = this.makePersistentConfigBackend(
protectionDescription
);
if (isError(persistentConfigData)) {
return persistentConfigData;
}
return await persistentConfigData.ok.saveConfig(settings);
}
public async getProtectionSettings<TConfigSchema extends TObject = TObject>(
protectionDescription: ProtectionDescription
): Promise<Result<TConfigSchema>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ async function storeProtections(
}

export class MjolnirProtectionsConfig implements ProtectionsConfig {
private constructor(
protected constructor(
private readonly config: PersistentConfigData<
typeof MjolnirEnabledProtectionsDescription.schema
>,
Expand Down
29 changes: 26 additions & 3 deletions src/Protection/ProtectionsManager/FakeProtectionsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,32 @@
//
// SPDX-License-Identifier: AFL-3.0

import { MjolnirProtectionsConfig } from '../ProtectionsConfig/StandardProtectionsConfig';
import { Ok } from '@gnuxie/typescript-result';
import { FakeProtectionsConfig } from '../ProtectionsConfig/FakeProtectionsConfig';
import { StandardProtectionCapabilityProviderSetConfig } from '../ProtectionsConfig/ProtectionCapabilityProviderSetConfig/StandardProtectionCapabilityProviderSetConfig';
import { MjolnirProtectionSettingsConfig } from '../ProtectionsConfig/ProtectionSettingsConfig/MjolnirProtectionSettingsConfig';
import { StandardProtectionsManager } from './StandardProtectionsManager';
import { FakePersistentConfigBackend } from '../../Interface/FakePersistentMatrixData';
import { StandardPersistentConfigData } from '../../Config/PersistentConfigData';
import { describeConfig } from '../../Config/describeConfig';
import { Type } from '@sinclair/typebox';

export function FakeProtectionsManager(): StandardProtectionsManager {
return new StandardProtectionsManager(new MjolnirProtectionsConfig());
export class FakeProtectionsManager extends StandardProtectionsManager {
constructor() {
super(
new FakeProtectionsConfig(),
new StandardProtectionCapabilityProviderSetConfig(),
new MjolnirProtectionSettingsConfig(function () {
const backend = new FakePersistentConfigBackend({});
return Ok(
new StandardPersistentConfigData(
describeConfig({
schema: Type.Object({}, { additionalProperties: true }),
}),
backend
)
);
})
);
}
}

0 comments on commit c51ebaf

Please sign in to comment.