From e53210bfc6f2827758611a81e13d743851291045 Mon Sep 17 00:00:00 2001 From: Dane Wang Date: Thu, 16 May 2024 14:56:02 +0800 Subject: [PATCH] feat: testing support jobs options (#23) --- src/testing.ts | 53 +++++++++++++++++++++++++++++++-------------- src/testing_test.ts | 30 ++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 17 deletions(-) diff --git a/src/testing.ts b/src/testing.ts index 1fd1f34..31fd730 100644 --- a/src/testing.ts +++ b/src/testing.ts @@ -3,9 +3,17 @@ import { EnqueueFunction, ListenFunction, } from "./use-queue.ts"; -import { JetQueueOptions, QueueJob, QueueJobId } from "./types.ts"; +import { + EnqueueOptions, + JetQueueOptions, + QueueJob, + QueueJobId, +} from "./types.ts"; -let jobs: Array<[string, QueueJob>]> = []; +type maybeOptions = + | Partial>> + | undefined; +let jobs: Array<[string, QueueJob>, maybeOptions]> = []; let jobIdCounter: QueueJobId = BigInt(1); export function makeTestingFunctions< @@ -16,12 +24,11 @@ export function makeTestingFunctions< ) { const enqueue: EnqueueFunction = function ( args, - _options, + options, ): ReturnType> { const jobId: QueueJobId = jobIdCounter; jobIdCounter = jobIdCounter + BigInt(1); - jobs.push([queue, { id: jobId, args }]); - + jobs.push([queue, { id: jobId, args }, options]); return Promise.resolve({ id: jobId, is_conflict: false, @@ -63,18 +70,32 @@ export function getJobs() { export function findEnqueuedJob( expectedQueue: string, expectedArgs: Record, -): QueueJob> | undefined { - return jobs.find(([queue, job]) => { + expectOptions?: maybeOptions, +): [string, QueueJob>, maybeOptions] | undefined { + return jobs.find(([queue, job, options]) => { if (queue !== expectedQueue) return false; - return Object.entries(expectedArgs).every(([key, value]) => { - const argValue = job.args[key]; + const argsMatched = matchObject(job.args, expectedArgs); - if (argValue instanceof Date && value instanceof Date) { - return argValue.getTime() === value.getTime(); - } else { - return argValue === value; - } - }); - })?.[1]; + const optionsMatched = expectOptions === undefined || + matchObject(options, expectOptions); + + return argsMatched && optionsMatched; + }); +} + +function compareValues(a: unknown, b: unknown): boolean { + if (a instanceof Date && b instanceof Date) { + return a.getTime() === b.getTime(); + } + return a === b; +} + +function matchObject( + source: Record | undefined, + target: Record, +): boolean { + return !Object.entries(target).some(([key, value]) => + !compareValues(source !== undefined ? source[key] : undefined, value) + ); } diff --git a/src/testing_test.ts b/src/testing_test.ts index 325c2a3..5053411 100644 --- a/src/testing_test.ts +++ b/src/testing_test.ts @@ -29,7 +29,35 @@ describe("Queue Testing Functions", () => { const foundJob = findEnqueuedJob("testQueue", jobArgs); assert(foundJob, "Job should be found"); - assertEquals(foundJob?.args, jobArgs, "Found job args should match"); + assertEquals(foundJob?.[1].args, jobArgs, "Found job args should match"); + }); + + it("should correctly handle the scheduleAt in options when enqueueing and finding a job", async () => { + const { enqueue } = makeTestingFunctions("testQueue", { instanceName: "" }); + const jobArgs = { task: "testTask" }; + const scheduledAt = new Date(); + + await enqueue(jobArgs, { scheduledAt }); + + const jobs = getJobs(); + + assertEquals(jobs.length, 1, "There should be one job enqueued"); + + assertEquals( + jobs[0][2]?.scheduledAt?.getTime(), + scheduledAt.getTime(), + "Enqueued job scheduleAt should match", + ); + + const foundJob = findEnqueuedJob("testQueue", jobArgs, { scheduledAt }); + + assert(foundJob, "Job should be found"); + + assertEquals( + foundJob?.[2]?.scheduledAt?.getTime(), + scheduledAt.getTime(), + "Found job scheduledAt should match", + ); }); it("should handle clearing jobs correctly", () => {