|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +import * as internal from "../overrides/internal"; |
| 4 | +import { BucketCachePurge } from "./bucket-cache-purge"; |
| 5 | + |
| 6 | +vi.mock("cloudflare:workers", () => ({ |
| 7 | + DurableObject: class { |
| 8 | + constructor( |
| 9 | + public ctx: DurableObjectState, |
| 10 | + public env: CloudflareEnv |
| 11 | + ) {} |
| 12 | + }, |
| 13 | +})); |
| 14 | + |
| 15 | +const createBucketCachePurge = () => { |
| 16 | + const mockState = { |
| 17 | + waitUntil: vi.fn(), |
| 18 | + blockConcurrencyWhile: vi.fn().mockImplementation(async (fn) => fn()), |
| 19 | + storage: { |
| 20 | + setAlarm: vi.fn(), |
| 21 | + getAlarm: vi.fn(), |
| 22 | + sql: { |
| 23 | + exec: vi.fn().mockImplementation(() => ({ |
| 24 | + one: vi.fn(), |
| 25 | + toArray: vi.fn().mockReturnValue([]), |
| 26 | + })), |
| 27 | + }, |
| 28 | + }, |
| 29 | + }; |
| 30 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 31 | + return new BucketCachePurge(mockState as any, {}); |
| 32 | +}; |
| 33 | + |
| 34 | +describe("BucketCachePurge", () => { |
| 35 | + it("should block concurrency while creating the table", async () => { |
| 36 | + const cache = createBucketCachePurge(); |
| 37 | + // @ts-expect-error - testing private method |
| 38 | + expect(cache.ctx.blockConcurrencyWhile).toHaveBeenCalled(); |
| 39 | + // @ts-expect-error - testing private method |
| 40 | + expect(cache.ctx.storage.sql.exec).toHaveBeenCalledWith( |
| 41 | + expect.stringContaining("CREATE TABLE IF NOT EXISTS cache_purge") |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + describe("purgeCacheByTags", () => { |
| 46 | + it("should insert tags into the sql table", async () => { |
| 47 | + const cache = createBucketCachePurge(); |
| 48 | + const tags = ["tag1", "tag2"]; |
| 49 | + await cache.purgeCacheByTags(tags); |
| 50 | + // @ts-expect-error - testing private method |
| 51 | + expect(cache.ctx.storage.sql.exec).toHaveBeenCalledWith( |
| 52 | + expect.stringContaining("INSERT OR REPLACE INTO cache_purge"), |
| 53 | + [tags[0]] |
| 54 | + ); |
| 55 | + // @ts-expect-error - testing private method |
| 56 | + expect(cache.ctx.storage.sql.exec).toHaveBeenCalledWith( |
| 57 | + expect.stringContaining("INSERT OR REPLACE INTO cache_purge"), |
| 58 | + [tags[1]] |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + it("should set an alarm if no alarm is set", async () => { |
| 63 | + const cache = createBucketCachePurge(); |
| 64 | + // @ts-expect-error - testing private method |
| 65 | + cache.ctx.storage.getAlarm.mockResolvedValueOnce(null); |
| 66 | + await cache.purgeCacheByTags(["tag"]); |
| 67 | + // @ts-expect-error - testing private method |
| 68 | + expect(cache.ctx.storage.setAlarm).toHaveBeenCalled(); |
| 69 | + }); |
| 70 | + |
| 71 | + it("should not set an alarm if one is already set", async () => { |
| 72 | + const cache = createBucketCachePurge(); |
| 73 | + // @ts-expect-error - testing private method |
| 74 | + cache.ctx.storage.getAlarm.mockResolvedValueOnce(true); |
| 75 | + await cache.purgeCacheByTags(["tag"]); |
| 76 | + // @ts-expect-error - testing private method |
| 77 | + expect(cache.ctx.storage.setAlarm).not.toHaveBeenCalled(); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe("alarm", () => { |
| 82 | + it("should purge cache by tags and delete them from the sql table", async () => { |
| 83 | + const cache = createBucketCachePurge(); |
| 84 | + // @ts-expect-error - testing private method |
| 85 | + cache.ctx.storage.sql.exec.mockReturnValueOnce({ |
| 86 | + toArray: () => [{ tag: "tag1" }, { tag: "tag2" }], |
| 87 | + }); |
| 88 | + await cache.alarm(); |
| 89 | + // @ts-expect-error - testing private method |
| 90 | + expect(cache.ctx.storage.sql.exec).toHaveBeenCalledWith( |
| 91 | + expect.stringContaining("DELETE FROM cache_purge"), |
| 92 | + ["tag1", "tag2"] |
| 93 | + ); |
| 94 | + }); |
| 95 | + it("should not purge cache if no tags are found", async () => { |
| 96 | + const cache = createBucketCachePurge(); |
| 97 | + // @ts-expect-error - testing private method |
| 98 | + cache.ctx.storage.sql.exec.mockReturnValueOnce({ |
| 99 | + toArray: () => [], |
| 100 | + }); |
| 101 | + await cache.alarm(); |
| 102 | + // @ts-expect-error - testing private method |
| 103 | + expect(cache.ctx.storage.sql.exec).not.toHaveBeenCalledWith( |
| 104 | + expect.stringContaining("DELETE FROM cache_purge"), |
| 105 | + [] |
| 106 | + ); |
| 107 | + }); |
| 108 | + |
| 109 | + it("should call internalPurgeCacheByTags with the correct tags", async () => { |
| 110 | + const cache = createBucketCachePurge(); |
| 111 | + const tags = ["tag1", "tag2"]; |
| 112 | + // @ts-expect-error - testing private method |
| 113 | + cache.ctx.storage.sql.exec.mockReturnValueOnce({ |
| 114 | + toArray: () => tags.map((tag) => ({ tag })), |
| 115 | + }); |
| 116 | + const internalPurgeCacheByTagsSpy = vi.spyOn(internal, "internalPurgeCacheByTags"); |
| 117 | + await cache.alarm(); |
| 118 | + expect(internalPurgeCacheByTagsSpy).toHaveBeenCalledWith( |
| 119 | + // @ts-expect-error - testing private method |
| 120 | + cache.env, |
| 121 | + tags |
| 122 | + ); |
| 123 | + // @ts-expect-error - testing private method 1st is constructor, 2nd is to get the tags and 3rd is to delete them |
| 124 | + expect(cache.ctx.storage.sql.exec).toHaveBeenCalledTimes(3); |
| 125 | + }); |
| 126 | + |
| 127 | + it("should continue until all tags are purged", async () => { |
| 128 | + const cache = createBucketCachePurge(); |
| 129 | + const tags = Array.from({ length: 100 }, (_, i) => `tag${i}`); |
| 130 | + // @ts-expect-error - testing private method |
| 131 | + cache.ctx.storage.sql.exec.mockReturnValueOnce({ |
| 132 | + toArray: () => tags.map((tag) => ({ tag })), |
| 133 | + }); |
| 134 | + const internalPurgeCacheByTagsSpy = vi.spyOn(internal, "internalPurgeCacheByTags"); |
| 135 | + await cache.alarm(); |
| 136 | + expect(internalPurgeCacheByTagsSpy).toHaveBeenCalledWith( |
| 137 | + // @ts-expect-error - testing private method |
| 138 | + cache.env, |
| 139 | + tags |
| 140 | + ); |
| 141 | + // @ts-expect-error - testing private method 1st is constructor, 2nd is to get the tags and 3rd is to delete them, 4th is to get the next 100 tags |
| 142 | + expect(cache.ctx.storage.sql.exec).toHaveBeenCalledTimes(4); |
| 143 | + // @ts-expect-error - testing private method |
| 144 | + expect(cache.ctx.storage.sql.exec).toHaveBeenLastCalledWith( |
| 145 | + expect.stringContaining("SELECT * FROM cache_purge LIMIT 100") |
| 146 | + ); |
| 147 | + }); |
| 148 | + }); |
| 149 | +}); |
0 commit comments