Skip to content

Commit 610ada9

Browse files
committed
fixup! add test
1 parent 471111c commit 610ada9

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { describe, expect, test, vi } from "vitest";
2+
3+
import { CURRENT_VERSION_ID } from "../templates/skew-protection";
4+
import { listWorkerVersions, updateDeploymentMapping } from "./skew-protection";
5+
6+
describe("skew protection", () => {
7+
describe("listWorkerVersions", () => {
8+
test("listWorkerVersions return versions ordered by time DESC", async () => {
9+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
10+
const client: any = {
11+
workers: {
12+
scripts: {
13+
versions: {
14+
list: () => [],
15+
},
16+
},
17+
},
18+
};
19+
20+
const now = Date.now();
21+
22+
client.workers.scripts.versions.list = vi.fn().mockReturnValue([
23+
{
24+
id: "HEAD",
25+
metadata: { created_on: new Date(now) },
26+
},
27+
{
28+
id: "HEAD~2",
29+
metadata: { created_on: new Date(now - 2000) },
30+
},
31+
{
32+
id: "HEAD~1",
33+
metadata: { created_on: new Date(now - 1000) },
34+
},
35+
]);
36+
37+
expect(await listWorkerVersions("scriptName", { client, accountId: "accountId" })).toMatchObject([
38+
{
39+
createdOnMs: now,
40+
id: "HEAD",
41+
},
42+
{
43+
createdOnMs: now - 1000,
44+
id: "HEAD~1",
45+
},
46+
{
47+
createdOnMs: now - 2000,
48+
id: "HEAD~2",
49+
},
50+
]);
51+
});
52+
});
53+
});
54+
55+
describe("updateDeploymentMapping", () => {
56+
test("Update", () => {
57+
const mapping = {
58+
N: CURRENT_VERSION_ID,
59+
"N-1": "vN-1",
60+
"N-2": "vN-2",
61+
};
62+
const versions = [{ id: "vN" }, { id: "vN-1" }]; // "vN-2" is deleted
63+
expect(updateDeploymentMapping(mapping, versions, "N+1")).toMatchObject({
64+
"N+1": CURRENT_VERSION_ID,
65+
N: "vN",
66+
"N-1": "vN-1",
67+
});
68+
});
69+
});

packages/cloudflare/src/cli/commands/skew-protection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export async function getDeploymentMapping(
126126
* @param deploymentId Deployment ID
127127
* @returns The updated mapping
128128
*/
129-
function updateDeploymentMapping(
129+
export function updateDeploymentMapping(
130130
mapping: Record<string, string>,
131131
versions: { id: string }[],
132132
deploymentId: string

0 commit comments

Comments
 (0)