Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

updateEverythingRun.test.ts

70 lines2.3 KB

1import { EnvironmentId } from "@t3tools/contracts";
2import { afterEach, describe, expect, it, vi } from "vite-plus/test";
3​
4import { appAtomRegistry } from "~/rpc/atomRegistry";
5import { serverEnvironment } from "~/state/server";
6import { startDesktopUpdateLaunchNotice } from "./updateEverythingRun";
7​
8function relaunchWhileRestarting(environmentId: EnvironmentId) {
9 const now = 1_800_000_000_000;
10 vi.useFakeTimers({ now });
11 const store = new Map([
12 [
13 "t3code:update-everything-result:v1",
14 JSON.stringify({
15 savedAt: now,
16 localVersion: null,
17 updatedServers: 0,
18 updatedProviders: 0,
19 failures: [],
20 handedOff: [{ environmentId, label: "Slow", targetVersion: "9.9.9", startedAt: now }],
21 }),
22 ],
23 ]);
24 vi.stubGlobal("window", {
25 localStorage: {
26 getItem: (key: string) => store.get(key) ?? null,
27 removeItem: (key: string) => store.delete(key),
28 },
29 });
30 return startDesktopUpdateLaunchNotice();
32​
33describe("startDesktopUpdateLaunchNotice", () => {
34 afterEach(() => {
35 vi.useRealTimers();
36 vi.unstubAllGlobals();
37 });
38​
39 it("keeps a server that is still restarting from looking idle after the app relaunches", async () => {
40 const environmentId = EnvironmentId.make("slow-machine");
41 const stop = relaunchWhileRestarting(environmentId);
42 try {
43 // Lets the registry run its cleanup of unobserved atoms before the UI mounts.
44 await vi.advanceTimersByTimeAsync(1_000);
45 expect(appAtomRegistry.get(serverEnvironment.updateStateAtom(environmentId))).toMatchObject({
46 status: "running",
47 stage: "resuming",
48 targetVersion: "9.9.9",
49 });
50 } finally {
51 await vi.advanceTimersByTimeAsync(32 * 60 * 1000);
52 stop();
53 }
54 });
55​
56 it("clears the restored state when the server never comes back", async () => {
57 const environmentId = EnvironmentId.make("lost-machine");
58 const stateAtom = serverEnvironment.updateStateAtom(environmentId);
59 const stop = relaunchWhileRestarting(environmentId);
60 // Observed like the UI does, so the registry cannot reset it by dropping the atom.
61 const release = appAtomRegistry.mount(stateAtom);
62 try {
63 await vi.advanceTimersByTimeAsync(32 * 60 * 1000);
64 expect(appAtomRegistry.get(stateAtom)).toEqual({ status: "idle" });
65 } finally {
66 release();
67 stop();
68 }
69 });
70});