Skip to content

Commit

Permalink
add tests and fix mistake
Browse files Browse the repository at this point in the history
  • Loading branch information
james-elicx committed Feb 10, 2025
1 parent ddae2a5 commit ea6396a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
43 changes: 43 additions & 0 deletions packages/cloudflare/src/api/memory-queue.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { generateMessageGroupId } from "@opennextjs/aws/core/routing/queue.js";
import { beforeAll, describe, expect, it, vi } from "vitest";

import cache from "./memory-queue";

vi.mock("./.next/prerender-manifest.json", () => ({
preview: { previewModeId: "id" },
}));

const defaultOpts = {
MessageBody: { host: "test.local", url: "/test" },
MessageGroupId: generateMessageGroupId("/test"),
MessageDeduplicationId: "",
};

describe("MemoryQueue", () => {
beforeAll(() => {
vi.useFakeTimers();
globalThis.internalFetch = vi.fn();
});

it("should de-dupe revalidations", async () => {
await cache.send(defaultOpts);
expect(globalThis.internalFetch).toHaveBeenCalledTimes(1);
await cache.send(defaultOpts);
expect(globalThis.internalFetch).toHaveBeenCalledTimes(1);

cache.remove("/test");

await cache.send(defaultOpts);
expect(globalThis.internalFetch).toHaveBeenCalledTimes(2);
await cache.send(defaultOpts);
expect(globalThis.internalFetch).toHaveBeenCalledTimes(2);

vi.advanceTimersByTime(10_000);

await cache.send(defaultOpts);
expect(globalThis.internalFetch).toHaveBeenCalledTimes(3);

await cache.send({ ...defaultOpts, MessageGroupId: generateMessageGroupId("/other") });
expect(globalThis.internalFetch).toHaveBeenCalledTimes(4);
});
});
2 changes: 2 additions & 0 deletions packages/cloudflare/src/api/memory-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class MemoryQueue implements Queue {
public revalidatedPaths = new Map<string, ReturnType<typeof setTimeout>>();

public async send({ MessageBody: { host, url }, MessageGroupId }: QueueMessage): Promise<void> {
if (this.revalidatedPaths.has(MessageGroupId)) return;

this.revalidatedPaths.set(
MessageGroupId,
// force remove to allow new revalidations incase something went wrong
Expand Down

0 comments on commit ea6396a

Please sign in to comment.