diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 9464840..e4ad378 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -16,4 +16,4 @@ lint: test: stage: test - script: deno test \ No newline at end of file + script: deno task test \ No newline at end of file diff --git a/deno.json b/deno.json index b5508ad..d23bcde 100644 --- a/deno.json +++ b/deno.json @@ -1,5 +1,6 @@ { "tasks": { + "test": "deno test --allow-read --allow-write" }, "lint": { "files": { diff --git a/src/deps.ts b/src/deps.ts index 9403356..756bf2a 100644 --- a/src/deps.ts +++ b/src/deps.ts @@ -1,3 +1,3 @@ export { readLines } from 'https://deno.land/std@0.181.0/io/mod.ts'; -export { assert } from 'https://deno.land/std@0.181.0/testing/asserts.ts'; +export { assert, assertEquals } from 'https://deno.land/std@0.181.0/testing/asserts.ts'; export { Keydb } from 'https://deno.land/x/keydb@1.0.0/sqlite.ts'; diff --git a/src/policies/anti-duplication-policy.test.ts b/src/policies/anti-duplication-policy.test.ts new file mode 100644 index 0000000..1e37f27 --- /dev/null +++ b/src/policies/anti-duplication-policy.test.ts @@ -0,0 +1,28 @@ +import { assertEquals } from '../deps.ts'; +import { buildEvent, buildInputMessage } from '../test.ts'; + +import antiDuplicationPolicy from './anti-duplication-policy.ts'; + +Deno.test({ + name: 'blocks events that post the same content too quickly', + fn: async () => { + const content = 'Spicy peppermint apricot mediterranean ginger carrot spiced juice edamame hummus'; + + const opts = { + databaseUrl: `sqlite:///tmp/${crypto.randomUUID()}.sqlite3`, + }; + + const msg1 = buildInputMessage({ event: buildEvent({ content }) }); + + assertEquals((await antiDuplicationPolicy(msg1, opts)).action, 'accept'); + assertEquals((await antiDuplicationPolicy(msg1, opts)).action, 'shadowReject'); + assertEquals((await antiDuplicationPolicy(msg1, opts)).action, 'shadowReject'); + + const msg2 = buildInputMessage({ event: buildEvent({ content: 'a' }) }); + + assertEquals((await antiDuplicationPolicy(msg2, opts)).action, 'accept'); + assertEquals((await antiDuplicationPolicy(msg2, opts)).action, 'accept'); + assertEquals((await antiDuplicationPolicy(msg2, opts)).action, 'accept'); + }, + sanitizeResources: false, +});