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

CodexSetupSection.logic.test.ts

121 lines4.0 KB

1import type { ProviderAuthState, ProviderInstallState } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 settleAuthorizationPage,
6 settleHandoffAuthorizationPage,
7 settleProviderInstall,
8} from "./CodexSetupSection.logic";
9​
10const install = (overrides: Partial<ProviderInstallState>) =>
11 ({
12 driver: "codex",
13 operationId: "op-1",
14 phase: "downloading",
15 downloadedBytes: 0,
16 totalBytes: null,
17 version: "0.2.0",
18 installedVersion: "0.1.0",
19 canRemove: true,
20 message: null,
21 ...overrides,
22 }) as ProviderInstallState;
23​
24const auth = (overrides: Partial<ProviderAuthState>) =>
25 ({
26 instanceId: "codex",
27 phase: "starting",
28 flowId: "flow-1",
29 authorizationUrl: null,
30 expiresAt: null,
31 message: null,
32 ...overrides,
33 }) as ProviderAuthState;
34​
35describe("settleProviderInstall", () => {
36 it("ignores the previous runtime while the update it started has not been pushed yet", () => {
37 const started = install({ operationId: "op-2", phase: "downloading" });
38 const previous = install({ operationId: "op-1", phase: "succeeded" });
39 expect(settleProviderInstall(started, previous)).toBeUndefined();
40 expect(settleProviderInstall(started, null)).toBeUndefined();
41 expect(settleProviderInstall(started, { ...started, phase: "extracting" })).toBeUndefined();
42 });
43​
44 it("settles on the started operation's terminal phase", () => {
45 const started = install({ operationId: "op-2" });
46 expect(
47 settleProviderInstall(
48 started,
49 install({ operationId: "op-2", phase: "succeeded", installedVersion: "0.2.0" }),
50 ),
51 ).toBe(true);
52 expect(settleProviderInstall(started, install({ operationId: "op-2", phase: "failed" }))).toBe(
53 false,
54 );
55 expect(
56 settleProviderInstall(started, install({ operationId: "op-2", phase: "cancelled" })),
57 ).toBe(false);
58 });
59​
60 it("settles immediately when start reused an existing runtime", () => {
61 expect(settleProviderInstall(install({ operationId: null, phase: "succeeded" }), null)).toBe(
62 true,
63 );
64 expect(
65 settleProviderInstall(
66 install({ operationId: null, phase: "idle", installedVersion: null }),
67 null,
68 ),
69 ).toBe(false);
70 });
71});
72​
73describe("settleAuthorizationPage", () => {
74 it("waits for its own flow to reach the browser step", () => {
75 expect(settleAuthorizationPage(null, "flow-1")).toBeUndefined();
76 expect(settleAuthorizationPage(auth({ phase: "starting" }), "flow-1")).toBeUndefined();
77 expect(
78 settleAuthorizationPage(auth({ phase: "waiting", authorizationUrl: "https://a" }), "flow-1"),
79 ).toBe("https://a");
80 expect(
81 settleAuthorizationPage(
82 auth({
83 phase: "waiting",
84 authorizationUrl: "https://a",
85 interaction: {
86 type: "browser",
87 id: "interaction-1",
88 url: "https://b",
89 requiresConsent: false,
90 } as ProviderAuthState["interaction"],
91 }),
92 "flow-1",
93 ),
94 ).toBe("https://b");
95 });
96​
97 it("ignores a flow another client started", () => {
98 expect(
99 settleAuthorizationPage(
100 auth({ flowId: "flow-2", phase: "waiting", authorizationUrl: "https://a" }),
101 "flow-1",
102 ),
103 ).toBeUndefined();
104 });
105​
106 it("gives up when its flow ends before a page is available", () => {
107 expect(settleAuthorizationPage(auth({ phase: "failed" }), "flow-1")).toBeNull();
108 expect(settleAuthorizationPage(auth({ phase: "cancelled" }), "flow-1")).toBeNull();
109 expect(settleAuthorizationPage(auth({ phase: "succeeded" }), "flow-1")).toBeNull();
110 });
111​
112 it("follows a handoff until the primary environment finishes", () => {
113 expect(
114 settleHandoffAuthorizationPage({
115 phase: "auth",
116 state: auth({ flowId: "primary-flow", phase: "waiting", authorizationUrl: "https://a" }),
117 }),
118 ).toBe("https://a");
119 expect(settleHandoffAuthorizationPage({ phase: "finished", profile: {} as never })).toBeNull();
120 });
121});