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

environment-maintenance.test.ts

154 lines5.4 KB

1import { AuthOrchestrationOperateScope, ServerProvider } from "@t3tools/contracts";
2import * as Schema from "effect/Schema";
3import { afterEach, describe, expect, it, vi } from "vite-plus/test";
4​
5import {
6 canMaintainEnvironment,
7 canUpdateEnvironmentProvider,
8 findEnvironmentUpdate,
9 supportsEnvironmentUpdate,
10} from "./environment-maintenance";
11​
12const provider = Schema.decodeUnknownSync(ServerProvider)({
13 instanceId: "codex-personal",
14 driver: "codex",
15 enabled: true,
16 installed: true,
17 version: "1.0.0",
18 status: "ready",
19 auth: { status: "authenticated" },
20 checkedAt: "2026-09-23T00:00:00.000Z",
21 models: [],
22 versionAdvisory: {
23 status: "behind_latest",
24 currentVersion: "1.0.0",
25 latestVersion: "1.1.0",
26 canUpdate: true,
27 updateCommand: null,
28 checkedAt: null,
29 message: null,
30 },
31});
32​
33afterEach(() => vi.unstubAllGlobals());
34​
35describe("environment maintenance access", () => {
36 it("requires a connected authenticated session with operate permission", () => {
37 const session = {
38 authenticated: true,
39 auth: {
40 policy: "remote-reachable" as const,
41 bootstrapMethods: [],
42 sessionMethods: [],
43 sessionCookieName: "session",
44 },
45 scopes: [AuthOrchestrationOperateScope],
46 };
47 expect(canMaintainEnvironment(session, true)).toBe(true);
48 expect(canMaintainEnvironment(session, false)).toBe(false);
49 expect(canMaintainEnvironment({ ...session, authenticated: false }, true)).toBe(false);
50 expect(canMaintainEnvironment({ ...session, scopes: [] }, true)).toBe(false);
51 const { scopes: _, ...legacy } = session;
52 expect(canMaintainEnvironment(legacy, true)).toBe(false);
53 expect(canMaintainEnvironment(null, true)).toBe(false);
54 });
55​
56 it("requires remote desktop update support for desktop hosts", () => {
57 expect(supportsEnvironmentUpdate({})).toBe(false);
58 expect(supportsEnvironmentUpdate({ serverSelfUpdate: "respawn" })).toBe(true);
59 expect(supportsEnvironmentUpdate({ serverSelfUpdate: "desktop-managed" })).toBe(false);
60 expect(
61 supportsEnvironmentUpdate({ serverSelfUpdate: "desktop-managed", desktopAppUpdate: true }),
62 ).toBe(true);
63 });
64​
65 it("excludes unavailable, manual, busy, and incompatible provider updates", () => {
66 expect(canUpdateEnvironmentProvider(provider)).toBe(true);
67 expect(canUpdateEnvironmentProvider({ ...provider, installed: false })).toBe(false);
68 expect(canUpdateEnvironmentProvider({ ...provider, availability: "unavailable" })).toBe(false);
69 expect(canUpdateEnvironmentProvider({ ...provider, versionAdvisory: undefined })).toBe(false);
70 for (const latestVersionStatus of ["broken", "unsupported"] as const) {
71 expect(
72 canUpdateEnvironmentProvider({
73 ...provider,
74 compatibilityAdvisory: {
75 status: "supported",
76 latestVersionStatus,
77 message: null,
78 recommendedVersion: null,
79 recommendedRange: null,
80 },
81 }),
82 ).toBe(false);
83 }
84 for (const status of ["queued", "running"] as const) {
85 expect(
86 canUpdateEnvironmentProvider({
87 ...provider,
88 updateState: {
89 status,
90 startedAt: null,
91 finishedAt: null,
92 message: null,
93 output: null,
94 },
95 }),
96 ).toBe(false);
97 }
98 expect(
99 canUpdateEnvironmentProvider({
100 ...provider,
101 versionAdvisory: {
102 ...provider.versionAdvisory!,
103 canUpdate: false,
104 },
105 }),
106 ).toBe(false);
107 });
108});
109​
110describe("environment release checks", () => {
111 const signal = new AbortController().signal;
112​
113 it("keeps stable hosts on stable releases and ignores drafts", async () => {
114 vi.stubGlobal(
115 "fetch",
116 vi
117 .fn()
118 .mockImplementation(async () =>
119 Response.json([
120 { tag_name: "v2.0.0-nightly.20260923.1" },
121 { tag_name: "v1.2.0", draft: true },
122 { tag_name: "v1.1.0" },
123 ]),
124 ),
125 );
126 expect(await findEnvironmentUpdate("1.0.0", signal)).toBe("1.1.0");
127 expect(await findEnvironmentUpdate("1.1.0", signal)).toBeNull();
128 expect(await findEnvironmentUpdate("1.3.0", signal)).toBeNull();
129 });
130​
131 it("walks release pages to find the host's channel", async () => {
132 const fetchMock = vi
133 .fn()
134 .mockResolvedValueOnce(
135 Response.json(Array.from({ length: 100 }, () => ({ tag_name: "v2.0.0" }))),
136 )
137 .mockResolvedValueOnce(Response.json([{ tag_name: "v1.0.0-preview.20260923.2" }]));
138 vi.stubGlobal("fetch", fetchMock);
139 expect(await findEnvironmentUpdate("1.0.0-preview.20260923.1", signal)).toBe(
140 "1.0.0-preview.20260923.2",
141 );
142 expect(fetchMock.mock.calls[1]?.[0]).toContain("page=2");
143 expect(fetchMock.mock.calls[1]?.[1]).toEqual({ signal });
144 });
145​
146 it("reports failed checks instead of claiming the server is current", async () => {
147 vi.stubGlobal("fetch", vi.fn().mockResolvedValue(new Response(null, { status: 403 })));
148 await expect(findEnvironmentUpdate("1.0.0", signal)).rejects.toThrow("403");
149 vi.stubGlobal("fetch", vi.fn().mockResolvedValue(Response.json([])));
150 await expect(findEnvironmentUpdate("1.0.0", signal)).rejects.toThrow("No stable release");
151 vi.stubGlobal("fetch", vi.fn().mockResolvedValue(Response.json({ error: "invalid" })));
152 await expect(findEnvironmentUpdate("1.0.0", signal)).rejects.toThrow();
153 });
154});