|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { DeploymentLogsCache, type DeploymentLogEntry } from "./deploymentLogsCache"; |
| 3 | + |
| 4 | +function lines(count: number): DeploymentLogEntry[] { |
| 5 | + return Array.from({ length: count }, (_, i) => ({ |
| 6 | + message: `line ${i}`, |
| 7 | + timestamp: new Date(0), |
| 8 | + level: "info" as const, |
| 9 | + })); |
| 10 | +} |
| 11 | + |
| 12 | +describe("DeploymentLogsCache", () => { |
| 13 | + it("returns undefined for unknown keys", () => { |
| 14 | + const cache = new DeploymentLogsCache(2, 100); |
| 15 | + expect(cache.get("missing")).toBeUndefined(); |
| 16 | + }); |
| 17 | + |
| 18 | + it("stores and returns entries", () => { |
| 19 | + const cache = new DeploymentLogsCache(2, 100); |
| 20 | + const value = { logs: lines(3), nextSeqNum: 3, finalized: true, complete: true }; |
| 21 | + cache.set("a", value); |
| 22 | + expect(cache.get("a")).toBe(value); |
| 23 | + expect(cache.size).toBe(1); |
| 24 | + expect(cache.lineCount).toBe(3); |
| 25 | + }); |
| 26 | + |
| 27 | + it("evicts the least recently used deployment past the entry limit", () => { |
| 28 | + const cache = new DeploymentLogsCache(2, 100); |
| 29 | + cache.set("a", { logs: lines(1), nextSeqNum: 1, finalized: false, complete: false }); |
| 30 | + cache.set("b", { logs: lines(1), nextSeqNum: 1, finalized: false, complete: false }); |
| 31 | + cache.get("a"); |
| 32 | + cache.set("c", { logs: lines(1), nextSeqNum: 1, finalized: false, complete: false }); |
| 33 | + |
| 34 | + expect(cache.get("b")).toBeUndefined(); |
| 35 | + expect(cache.get("a")).toBeDefined(); |
| 36 | + expect(cache.get("c")).toBeDefined(); |
| 37 | + expect(cache.size).toBe(2); |
| 38 | + }); |
| 39 | + |
| 40 | + it("evicts oldest deployments past the total line budget", () => { |
| 41 | + const cache = new DeploymentLogsCache(10, 10); |
| 42 | + cache.set("a", { logs: lines(4), nextSeqNum: 4, finalized: true, complete: true }); |
| 43 | + cache.set("b", { logs: lines(4), nextSeqNum: 4, finalized: true, complete: true }); |
| 44 | + cache.set("c", { logs: lines(4), nextSeqNum: 4, finalized: true, complete: true }); |
| 45 | + |
| 46 | + expect(cache.get("a")).toBeUndefined(); |
| 47 | + expect(cache.get("b")).toBeDefined(); |
| 48 | + expect(cache.get("c")).toBeDefined(); |
| 49 | + expect(cache.lineCount).toBe(8); |
| 50 | + }); |
| 51 | + |
| 52 | + it("always keeps the entry just set, even when it alone exceeds the budget", () => { |
| 53 | + const cache = new DeploymentLogsCache(10, 10); |
| 54 | + cache.set("a", { logs: lines(4), nextSeqNum: 4, finalized: true, complete: true }); |
| 55 | + cache.set("big", { logs: lines(50), nextSeqNum: 50, finalized: true, complete: true }); |
| 56 | + |
| 57 | + expect(cache.get("a")).toBeUndefined(); |
| 58 | + expect(cache.get("big")?.logs).toHaveLength(50); |
| 59 | + expect(cache.size).toBe(1); |
| 60 | + expect(cache.lineCount).toBe(50); |
| 61 | + }); |
| 62 | + |
| 63 | + it("treats replacing a key as a recent use", () => { |
| 64 | + const cache = new DeploymentLogsCache(2, 100); |
| 65 | + cache.set("a", { logs: lines(1), nextSeqNum: 1, finalized: false, complete: false }); |
| 66 | + cache.set("b", { logs: lines(1), nextSeqNum: 1, finalized: false, complete: false }); |
| 67 | + cache.set("a", { logs: lines(2), nextSeqNum: 2, finalized: true, complete: true }); |
| 68 | + cache.set("c", { logs: lines(1), nextSeqNum: 1, finalized: false, complete: false }); |
| 69 | + |
| 70 | + expect(cache.get("b")).toBeUndefined(); |
| 71 | + expect(cache.get("a")?.logs).toHaveLength(2); |
| 72 | + expect(cache.get("c")).toBeDefined(); |
| 73 | + }); |
| 74 | + |
| 75 | + it("keeps recently read deployments when evicting for the line budget", () => { |
| 76 | + const cache = new DeploymentLogsCache(10, 10); |
| 77 | + cache.set("a", { logs: lines(4), nextSeqNum: 4, finalized: true, complete: true }); |
| 78 | + cache.set("b", { logs: lines(4), nextSeqNum: 4, finalized: true, complete: true }); |
| 79 | + cache.get("a"); |
| 80 | + cache.set("c", { logs: lines(4), nextSeqNum: 4, finalized: true, complete: true }); |
| 81 | + |
| 82 | + expect(cache.get("b")).toBeUndefined(); |
| 83 | + expect(cache.get("a")).toBeDefined(); |
| 84 | + expect(cache.get("c")).toBeDefined(); |
| 85 | + expect(cache.lineCount).toBe(8); |
| 86 | + }); |
| 87 | + |
| 88 | + it("replaces an existing key without double counting lines", () => { |
| 89 | + const cache = new DeploymentLogsCache(10, 100); |
| 90 | + cache.set("a", { logs: lines(4), nextSeqNum: 4, finalized: false, complete: false }); |
| 91 | + cache.set("a", { logs: lines(6), nextSeqNum: 6, finalized: true, complete: true }); |
| 92 | + |
| 93 | + expect(cache.size).toBe(1); |
| 94 | + expect(cache.lineCount).toBe(6); |
| 95 | + expect(cache.get("a")?.complete).toBe(true); |
| 96 | + }); |
| 97 | +}); |
0 commit comments