Add PolicyWorker test

This commit is contained in:
Alex Gleason 2025-02-27 16:52:09 -06:00
parent 5f5d0bc324
commit 7e00b9f9a2
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
4 changed files with 28 additions and 4 deletions

View file

@ -1,2 +0,0 @@
// @ts-ignore Don't try to access the env from this worker.
Deno.env = new Map<string, string>();

View file

@ -0,0 +1,14 @@
import { DittoConf } from '@ditto/conf';
import { generateSecretKey, nip19 } from 'nostr-tools';
import { PolicyWorker } from './policy.ts';
Deno.test('PolicyWorker', () => {
const conf = new DittoConf(
new Map([
['DITTO_NSEC', nip19.nsecEncode(generateSecretKey())],
]),
);
new PolicyWorker(conf);
});

View file

@ -3,6 +3,8 @@ import { NostrEvent, NostrRelayOK, NPolicy } from '@nostrify/nostrify';
import { logi } from '@soapbox/logi';
import * as Comlink from 'comlink';
import { errorJson } from '@/utils/log.ts';
import type { CustomPolicy } from '@/workers/policy.worker.ts';
export class PolicyWorker implements NPolicy {
@ -85,6 +87,15 @@ export class PolicyWorker implements NPolicy {
return;
}
logi({
level: 'error',
ns: 'ditto.system.policy',
msg: 'Failed to load custom policy',
path: conf.policy,
error: errorJson(e),
enabled: false,
});
throw new Error(`DITTO_POLICY (error importing policy): ${conf.policy}`);
}
}

View file

@ -1,5 +1,3 @@
import './deno-env.ts'; // HACK should be removed when `@/config.ts` is removed.
import { DittoConf } from '@ditto/conf';
import { DittoPolyPg } from '@ditto/db';
import '@soapbox/safe-fetch/load';
@ -10,6 +8,9 @@ import * as Comlink from 'comlink';
import { ReadOnlySigner } from '@/signers/ReadOnlySigner.ts';
import { DittoPgStore } from '@/storages/DittoPgStore.ts';
// @ts-ignore Don't try to access the env from this worker.
Deno.env = new Map<string, string>();
/** Serializable object the worker can use to set up the state. */
interface PolicyInit {
/** Path to the policy module (https, jsr, file, etc) */