Open History to see the last commit
1import {
2 CALM_HOST_PRESSURE,
3 EnvironmentId,
4 type HostPressure,
5 severeHostPressureKinds,
6} from "@t3tools/contracts";
7import { describe, expect, it } from "vite-plus/test";
8
9import { hideHostPressure, shownHostPressureKinds } from "./hostPressureDismissals.ts";
10
11const studio = EnvironmentId.make("studio");
12const HOUR = 60 * 60 * 1000;
13const machine = (overrides: Partial<HostPressure>) => {
14 const pressure = { ...CALM_HOST_PRESSURE, ...overrides };
15 return { environmentId: studio, pressure, kinds: severeHostPressureKinds(pressure) };
16};
17
18describe("host pressure dismissals", () => {
19 it("hides until the problem clears, then shows its return", () => {
20 const overloaded = machine({ cpu: "severe", severeSince: { cpu: 1_000 } });
21 const hidden = hideHostPressure([], [overloaded], "untilClear", 5_000);
22 expect(shownHostPressureKinds(hidden, overloaded, 5_000 + 48 * HOUR)).toEqual([]);
23
24 const returned = machine({ cpu: "severe", severeSince: { cpu: 90_000 } });
25 expect(shownHostPressureKinds(hidden, returned, 100_000)).toEqual(["cpu"]);
26 });
27
28 it("shows a timed hide again once its time passes", () => {
29 const overloaded = machine({ cpu: "severe", severeSince: { cpu: 1_000 } });
30 const hidden = hideHostPressure([], [overloaded], "hour", 5_000);
31 expect(shownHostPressureKinds(hidden, overloaded, 5_000 + HOUR - 1)).toEqual([]);
32 expect(shownHostPressureKinds(hidden, overloaded, 5_000 + HOUR)).toEqual(["cpu"]);
33 });
34
35 it("keeps a timed hide through a new episode, since the user picked the time", () => {
36 const overloaded = machine({ cpu: "severe", severeSince: { cpu: 1_000 } });
37 const hidden = hideHostPressure([], [overloaded], "tomorrow", 5_000);
38 const restarted = machine({ cpu: "severe", severeSince: { cpu: 60_000 } });
39 expect(shownHostPressureKinds(hidden, restarted, 70_000)).toEqual([]);
40 });
41
42 it("keeps showing a different problem on the same machine", () => {
43 const overloaded = machine({ cpu: "severe", severeSince: { cpu: 1_000 } });
44 const hidden = hideHostPressure([], [overloaded], "untilClear", 5_000);
45 const alsoFull = machine({
46 cpu: "severe",
47 disk: "severe",
48 severeSince: { cpu: 1_000, disk: 6_000 },
49 });
50 expect(shownHostPressureKinds(hidden, alsoFull, 7_000)).toEqual(["disk"]);
51 });
52
53 it("falls back to an hour on a server without episode times", () => {
54 const overloaded = machine({ cpu: "severe" });
55 const hidden = hideHostPressure([], [overloaded], "untilClear", 5_000);
56 expect(hidden).toEqual([
57 { environmentId: studio, kind: "cpu", since: null, until: 5_000 + HOUR },
58 ]);
59 });
60
61 it("hides only the kinds the notice displayed, even if another hide has since expired", () => {
62 const overloaded = machine({ memory: "severe", severeSince: { memory: 1_000 } });
63 const memoryHidden = hideHostPressure([], [overloaded], "hour", 0);
64 const both = machine({
65 memory: "severe",
66 cpu: "severe",
67 severeSince: { memory: 1_000, cpu: 2_000 },
68 });
69 const displayed = { ...both, kinds: shownHostPressureKinds(memoryHidden, both, HOUR - 1) };
70 expect(hideHostPressure(memoryHidden, [displayed], "untilClear", HOUR)).toEqual([
71 { environmentId: studio, kind: "cpu", since: 2_000, until: null },
72 ]);
73 });
74
75 it("drops expired hides when writing a new one", () => {
76 const overloaded = machine({ cpu: "severe", severeSince: { cpu: 1_000 } });
77 const full = machine({ disk: "severe", severeSince: { disk: 1_000 } });
78 const expired = hideHostPressure([], [overloaded], "hour", 0);
79 expect(hideHostPressure(expired, [full], "untilClear", 2 * HOUR)).toEqual([
80 { environmentId: studio, kind: "disk", since: 1_000, until: null },
81 ]);
82 });
83});