From a809da4249fa4eb545f9aa04342f83bd4471bccd Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 26 Mar 2023 16:19:33 -0500 Subject: [PATCH] Add test for antiDuplicationPolicy --- .gitlab-ci.yml | 2 +- deno.json | 1 + src/deps.ts | 2 +- src/policies/anti-duplication-policy.test.ts | 28 ++++++++++++++++++++ 4 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 src/policies/anti-duplication-policy.test.ts 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, +});