flesh out policy tests

This commit is contained in:
Siddharth Singh 2025-04-03 19:03:34 +05:30
parent da0bac8c4d
commit deae57a16c
No known key found for this signature in database
2 changed files with 36 additions and 4 deletions

View file

@ -49,7 +49,7 @@ export interface PolicyRegistryOpts {
}
export class PolicyRegistry {
constructor(private opts: PolicyRegistryOpts) { }
constructor(private opts: PolicyRegistryOpts) {}
available: Record<string, PolicyItem> = {
'AntiDuplicationPolicy': {

View file

@ -1,14 +1,46 @@
import { DittoConf } from '@ditto/conf';
import { generateSecretKey, nip19 } from 'nostr-tools';
import { PolicyWorker } from './policy.ts';
import { PolicyWorker } from '@/workers/policy.ts';
import { assertEquals } from '@std/assert/assert-equals';
import { join } from '@std/path';
Deno.test('PolicyWorker', () => {
const blocked = {
id: '19afd70437944671e7f5a02b29221ad444ef7cf60113a5731667e272e59a3979',
pubkey: '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
kind: 1,
tags: [['t', 'porn']],
content: 'this is a test of the policy system #porn',
sig:
'1d73a7480cfd737b89dc1e0e7175dff67119915f31d24a279a45d56622f4b991b01e431d07b693ee6cd652f3f27274d9e203ee43ae44af7e70ce8647e5326196',
created_at: 1743685015,
};
Deno.test('PolicyWorker with script policy', async () => {
const conf = new DittoConf(
new Map([
['DITTO_NSEC', nip19.nsecEncode(generateSecretKey())],
['DITTO_CUSTOM_POLICY', '1'],
['DATABASE_URL', Deno.env.get('DATABASE_URL')],
['DITTO_POLICY', join(Deno.cwd(), 'fixtures', 'policy.ts')],
]),
);
new PolicyWorker(conf);
const worker = new PolicyWorker(conf);
const [, , ok] = await worker.call(blocked);
assertEquals(ok, false);
});
Deno.test('PolicyWorker with event policy', async () => {
const conf = new DittoConf(
new Map([
['DITTO_NSEC', nip19.nsecEncode(generateSecretKey())],
['DATABASE_URL', Deno.env.get('DATABASE_URL')],
]),
);
const worker = new PolicyWorker(conf);
const [, , ok] = await worker.call(blocked);
assertEquals(ok, false);
});