Skip to content

Commit 551dfbe

Browse files
committed
Fix unit test
1 parent 9fb8b4d commit 551dfbe

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

packages/cloudflare/src/api/overrides/tag-cache/do-sharded-tag-cache.spec.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const getMock = vi
99
.fn()
1010
.mockReturnValue({ hasBeenRevalidated: hasBeenRevalidatedMock, writeTags: writeTagsMock });
1111
const waitUntilMock = vi.fn().mockImplementation(async (fn) => fn());
12-
// @ts-expect-error - We define it here only for the test
1312
globalThis.continent = undefined;
1413
const sendDLQMock = vi.fn();
1514
vi.mock("../../cloudflare-context", () => ({
@@ -22,7 +21,6 @@ vi.mock("../../cloudflare-context", () => ({
2221
},
2322
ctx: { waitUntil: waitUntilMock },
2423
cf: {
25-
// @ts-expect-error - We define it here only for the test
2624
continent: globalThis.continent,
2725
},
2826
}),
@@ -148,7 +146,6 @@ describe("DOShardedTagCache", () => {
148146
});
149147

150148
it("should generate one doIds, but in the correct region", () => {
151-
// @ts-expect-error - We define it here only for the test
152149
globalThis.continent = "EU";
153150
const cache = shardedDOTagCache({
154151
baseShardSize: 4,
@@ -168,7 +165,6 @@ describe("DOShardedTagCache", () => {
168165
expect(shardedTagCollection[0]?.doId.region).toBe("weur");
169166
expect(shardedTagCollection[1]?.doId.region).toBe("weur");
170167

171-
//@ts-expect-error - We need to reset the global variable
172168
globalThis.continent = undefined;
173169
});
174170

@@ -216,7 +212,7 @@ describe("DOShardedTagCache", () => {
216212

217213
it("should return false if stub return false", async () => {
218214
const cache = shardedDOTagCache();
219-
cache.getFromRegionalCache = vi.fn();
215+
cache.getFromRegionalCache = vi.fn().mockResolvedValueOnce([])
220216
hasBeenRevalidatedMock.mockImplementationOnce(() => false);
221217
const result = await cache.hasBeenRevalidated(["tag1"], 123456);
222218
expect(cache.getFromRegionalCache).toHaveBeenCalled();
@@ -227,7 +223,7 @@ describe("DOShardedTagCache", () => {
227223

228224
it("should return true if stub return true", async () => {
229225
const cache = shardedDOTagCache();
230-
cache.getFromRegionalCache = vi.fn();
226+
cache.getFromRegionalCache = vi.fn().mockResolvedValueOnce([]);
231227
hasBeenRevalidatedMock.mockImplementationOnce(() => true);
232228
const result = await cache.hasBeenRevalidated(["tag1"], 123456);
233229
expect(cache.getFromRegionalCache).toHaveBeenCalled();
@@ -238,7 +234,7 @@ describe("DOShardedTagCache", () => {
238234

239235
it("should return false if it throws", async () => {
240236
const cache = shardedDOTagCache();
241-
cache.getFromRegionalCache = vi.fn();
237+
cache.getFromRegionalCache = vi.fn().mockResolvedValueOnce([]);
242238
hasBeenRevalidatedMock.mockImplementationOnce(() => {
243239
throw new Error("error");
244240
});
@@ -251,7 +247,7 @@ describe("DOShardedTagCache", () => {
251247

252248
it("Should return from the cache if it was found there", async () => {
253249
const cache = shardedDOTagCache();
254-
cache.getFromRegionalCache = vi.fn().mockReturnValueOnce(new Response("true"));
250+
cache.getFromRegionalCache = vi.fn().mockReturnValueOnce([{ tag: "tag1", time: 1234567 }]);
255251
const result = await cache.hasBeenRevalidated(["tag1"], 123456);
256252
expect(result).toBe(true);
257253
expect(idFromNameMock).not.toHaveBeenCalled();
@@ -260,8 +256,8 @@ describe("DOShardedTagCache", () => {
260256

261257
it("should try to put the result in the cache if it was not revalidated", async () => {
262258
const cache = shardedDOTagCache();
263-
cache.getFromRegionalCache = vi.fn();
264-
cache.putToRegionalCache = vi.fn();
259+
cache.getFromRegionalCache = vi.fn().mockResolvedValueOnce([]);
260+
cache.putToRegionalCache = vi.fn()
265261
hasBeenRevalidatedMock.mockImplementationOnce(() => false);
266262
const result = await cache.hasBeenRevalidated(["tag1"], 123456);
267263
expect(result).toBe(false);
@@ -272,7 +268,7 @@ describe("DOShardedTagCache", () => {
272268

273269
it("should call all the durable object instance", async () => {
274270
const cache = shardedDOTagCache();
275-
cache.getFromRegionalCache = vi.fn();
271+
cache.getFromRegionalCache = vi.fn().mockResolvedValue([]);
276272
const result = await cache.hasBeenRevalidated(["tag1", "tag2"], 123456);
277273
expect(result).toBe(false);
278274
expect(idFromNameMock).toHaveBeenCalledTimes(2);
@@ -338,7 +334,6 @@ describe("DOShardedTagCache", () => {
338334
expect(cache.deleteRegionalCache).toHaveBeenCalledWith({
339335
doId: expect.objectContaining({ key: "tag-hard;shard-1;replica-1" }),
340336
tags: ["tag1"],
341-
type: "boolean",
342337
});
343338
// expect(cache.deleteRegionalCache).toHaveBeenCalledWith("tag-hard;shard-1;replica-1", ["tag1"]);
344339
});
@@ -372,14 +367,14 @@ describe("DOShardedTagCache", () => {
372367
numberOfReplicas: 1,
373368
shardType: "hard",
374369
});
375-
expect(await cache.getFromRegionalCache({ doId, tags: ["tag1"], type: "boolean" })).toBeUndefined();
370+
expect(await cache.getFromRegionalCache({ doId, tags: ["tag1"] })).toEqual([])
376371
});
377372

378373
it("should call .match on the cache", async () => {
379374
// @ts-expect-error - Defined on cloudfare context
380375
globalThis.caches = {
381376
open: vi.fn().mockResolvedValue({
382-
match: vi.fn().mockResolvedValue("response"),
377+
match: vi.fn().mockResolvedValue(new Response('1234567')),
383378
}),
384379
};
385380
const cache = shardedDOTagCache({ baseShardSize: 4, regionalCache: true });
@@ -388,7 +383,9 @@ describe("DOShardedTagCache", () => {
388383
numberOfReplicas: 1,
389384
shardType: "hard",
390385
});
391-
expect(await cache.getFromRegionalCache({ doId, tags: ["tag1"], type: "boolean" })).toBe("response");
386+
const cacheResult = await cache.getFromRegionalCache({ doId, tags: ["tag1"] });
387+
expect(cacheResult.length).toBe(1);
388+
expect(cacheResult[0]).toEqual({ tag: "tag1", time: 1234567 });
392389
// @ts-expect-error - Defined on cloudfare context
393390
globalThis.caches = undefined;
394391
});
@@ -398,17 +395,17 @@ describe("DOShardedTagCache", () => {
398395
it("should return the cache key without the random part", async () => {
399396
const cache = shardedDOTagCache();
400397
const doId1 = new DOId({ baseShardId: "shard-0", numberOfReplicas: 1, shardType: "hard" });
401-
expect(cache.getCacheUrlKey({ doId: doId1, tags: ["_N_T_/tag1"], type: "boolean" })).toBe(
402-
"http://local.cache/shard/tag-hard;shard-0?type=boolean&tags=_N_T_%2Ftag1"
398+
expect(cache.getCacheUrlKey(doId1, "_N_T_/tag1")).toBe(
399+
"http://local.cache/shard/tag-hard;shard-0?tag=_N_T_%2Ftag1"
403400
);
404401

405402
const doId2 = new DOId({
406403
baseShardId: "shard-1",
407404
numberOfReplicas: 1,
408405
shardType: "hard",
409406
});
410-
expect(cache.getCacheUrlKey({ doId: doId2, tags: ["tag1"], type: "boolean" })).toBe(
411-
"http://local.cache/shard/tag-hard;shard-1?type=boolean&tags=tag1"
407+
expect(cache.getCacheUrlKey(doId2, "tag1")).toBe(
408+
"http://local.cache/shard/tag-hard;shard-1?tag=tag1"
412409
);
413410
});
414411
});

packages/cloudflare/src/api/overrides/tag-cache/do-sharded-tag-cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ class ShardedDOTagCache implements NextModeTagCache {
429429
return this.localCache;
430430
}
431431

432-
private getCacheUrlKey(doId: DOId, tag: string) {
432+
getCacheUrlKey(doId: DOId, tag: string) {
433433
return `http://local.cache/shard/${doId.shardId}?tag=${encodeURIComponent(tag)}`;
434434
}
435435

0 commit comments

Comments
 (0)