118 lines
2.9 KiB
TypeScript
118 lines
2.9 KiB
TypeScript
import { describe, expect, jest, test } from "bun:test";
|
|
import { ApiVersion, AppEnv } from "@autumn/shared";
|
|
|
|
const createMockQueue = () => ({
|
|
send: jest.fn(async () => ({
|
|
metadata: { metrics: { backlogCount: 0, backlogBytes: 0 } },
|
|
})),
|
|
sendBatch: jest.fn(async () => ({
|
|
metadata: { metrics: { backlogCount: 0, backlogBytes: 0 } },
|
|
})),
|
|
metrics: jest.fn(async () => ({ backlogCount: 0, backlogBytes: 0 })),
|
|
});
|
|
|
|
import { JobName } from "@/queue/JobName.js";
|
|
import { addTasksToQueueBatch, addTaskToQueue } from "@/queue/queueUtils.js";
|
|
|
|
describe("addTaskToQueue queue override", () => {
|
|
test("uses the Cloudflare JOB_QUEUE binding", async () => {
|
|
const queue = createMockQueue();
|
|
|
|
await addTaskToQueue({
|
|
jobName: JobName.Track,
|
|
env: { JOB_QUEUE: queue as unknown as Queue } as Env,
|
|
payload: {
|
|
orgId: "org_123",
|
|
env: AppEnv.Sandbox,
|
|
customerId: "cus_123",
|
|
requestId: "req_123",
|
|
apiVersion: ApiVersion.V2_1,
|
|
body: {
|
|
customer_id: "cus_123",
|
|
feature_id: "messages",
|
|
},
|
|
},
|
|
});
|
|
|
|
expect(queue.send).toHaveBeenCalledTimes(1);
|
|
expect(queue.send).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
name: JobName.Track,
|
|
data: expect.objectContaining({ customerId: "cus_123" }),
|
|
}),
|
|
{ contentType: "json" },
|
|
);
|
|
});
|
|
|
|
test("uses the selected Cloudflare track queue binding", async () => {
|
|
const queue = createMockQueue();
|
|
|
|
await addTaskToQueue({
|
|
jobName: JobName.Track,
|
|
cloudflareQueueBinding: "TRACK_QUEUE",
|
|
env: {
|
|
TRACK_QUEUE: queue as unknown as Queue,
|
|
} as Env,
|
|
payload: {
|
|
orgId: "org_123",
|
|
env: AppEnv.Sandbox,
|
|
customerId: "cus_123",
|
|
requestId: "req_123",
|
|
apiVersion: ApiVersion.V2_1,
|
|
body: {
|
|
customer_id: "cus_123",
|
|
feature_id: "messages",
|
|
},
|
|
},
|
|
});
|
|
|
|
expect(queue.send).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
name: JobName.Track,
|
|
data: expect.objectContaining({ customerId: "cus_123" }),
|
|
}),
|
|
{ contentType: "json" },
|
|
);
|
|
});
|
|
|
|
test("uses the matching Cloudflare track async queue binding for batches", async () => {
|
|
const queue = createMockQueue();
|
|
|
|
const result = await addTasksToQueueBatch({
|
|
jobName: JobName.Track,
|
|
cloudflareQueueBinding: "TRACK_ASYNC_QUEUE",
|
|
env: {
|
|
TRACK_ASYNC_QUEUE: queue as unknown as Queue,
|
|
} as Env,
|
|
entries: [
|
|
{
|
|
messageGroupId: "org:sandbox:cus_123",
|
|
messageDeduplicationId: "req_123-0",
|
|
payload: {
|
|
orgId: "org_123",
|
|
env: AppEnv.Sandbox,
|
|
customerId: "cus_123",
|
|
requestId: "req_123",
|
|
apiVersion: ApiVersion.V2_1,
|
|
body: {
|
|
customer_id: "cus_123",
|
|
feature_id: "messages",
|
|
},
|
|
},
|
|
},
|
|
],
|
|
});
|
|
|
|
expect(result).toEqual({ successCount: 1, failures: [] });
|
|
expect(queue.sendBatch).toHaveBeenCalledWith([
|
|
{
|
|
body: expect.objectContaining({
|
|
name: JobName.Track,
|
|
data: expect.objectContaining({ customerId: "cus_123" }),
|
|
}),
|
|
contentType: "json",
|
|
},
|
|
]);
|
|
});
|
|
});
|