|
| 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 | +}); |
0 commit comments