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

ProviderUpdatesAction.browser.test.tsx

153 lines4.9 KB

1import { RegistryContext } from "@effect/atom-react";
2import {
3 AuthProvidersManageScope,
4 type AuthSessionState,
5 EnvironmentId,
6 ProviderDriverKind,
7 ProviderInstanceId,
8 type ServerProvider,
9} from "@t3tools/contracts";
10import { AsyncResult, Atom } from "effect/reactivity";
11import { useEffect } from "react";
12import { expect, it, vi } from "vite-plus/test";
13​
14import { act, render } from "../../test/render";
15​
16const state = vi.hoisted(() => ({
17 environments: [] as {
18 environmentId: EnvironmentId;
19 label: string;
20 connection: { phase: "connected" | "disconnected" };
21 }[],
22 providers: new Map<EnvironmentId, ReadonlyArray<ServerProvider>>(),
23 requests: [] as EnvironmentId[],
24 sessions: new Map<EnvironmentId, Atom.Writable<AsyncResult.AsyncResult<AuthSessionState>>>(),
25}));
26​
27vi.mock(import("~/state/environments"), () => ({
28 useEnvironments: (() => ({ environments: state.environments })) as never,
29 useEnvironmentProviders: () => state.providers,
30}));
31vi.mock(import("@t3tools/client-runtime/state/session"), async (original) => {
32 const actual = await original();
33 return {
34 ...actual,
35 createEnvironmentSessionAtoms: ((
36 ...args: Parameters<typeof actual.createEnvironmentSessionAtoms>
37 ) => ({
38 ...actual.createEnvironmentSessionAtoms(...args),
39 sessionStateAtom: (id: EnvironmentId) => state.sessions.get(id)!,
40 })) as typeof actual.createEnvironmentSessionAtoms,
41 };
42});
43vi.mock(import("~/state/server"), () => ({
44 serverEnvironment: { updateProvider: {} } as never,
45}));
46vi.mock(import("~/state/use-atom-command"), () => ({
47 useAtomCommand: (() =>
48 async ({ environmentId }: { environmentId: EnvironmentId }) => {
49 state.requests.push(environmentId);
50 return AsyncResult.success(undefined);
51 }) as never,
52}));
53​
54import { appAtomRegistry } from "~/rpc/atomRegistry";
55import { useProviderUpdates } from "./ProviderUpdatesAction";
56​
57const provider: ServerProvider = {
58 instanceId: ProviderInstanceId.make("codex"),
59 driver: ProviderDriverKind.make("codex"),
60 enabled: true,
61 installed: true,
62 version: "1.0.0",
63 status: "ready",
64 auth: { status: "authenticated" },
65 checkedAt: "2026-10-21T00:00:00.000Z",
66 models: [],
67 slashCommands: [],
68 skills: [],
69 versionAdvisory: {
70 status: "behind_latest",
71 currentVersion: "1.0.0",
72 latestVersion: "1.1.0",
73 updateCommand: "npm install -g @openai/codex",
74 canUpdate: true,
75 checkedAt: "2026-10-21T00:00:00.000Z",
76 message: "Update available.",
77 },
78};
79​
80function session(
81 scopes: NonNullable<AuthSessionState["scopes"]>,
82): AsyncResult.AsyncResult<AuthSessionState> {
83 return AsyncResult.success<AuthSessionState>({
84 authenticated: true,
85 scopes,
86 auth: {
87 policy: "remote-reachable",
88 bootstrapMethods: ["one-time-token"],
89 sessionMethods: ["bearer-access-token"],
90 sessionCookieName: "t3_session",
91 },
92 });
94​
95function setup() {
96 state.environments = [
97 { environmentId: EnvironmentId.make("read-only"), label: "Read only" },
98 { environmentId: EnvironmentId.make("writable"), label: "Writable" },
99 { environmentId: EnvironmentId.make("unresolved"), label: "Unresolved" },
100 ].map((environment) => ({ ...environment, connection: { phase: "connected" } }));
101 state.providers = new Map(
102 state.environments.map(({ environmentId }) => [environmentId, [provider]]),
103 );
104 state.requests = [];
105 const sessions = new Map<EnvironmentId, Atom.Writable<AsyncResult.AsyncResult<AuthSessionState>>>(
106 [
107 [state.environments[0]!.environmentId, Atom.make(session(["orchestration:read"]))],
108 [state.environments[1]!.environmentId, Atom.make(session([AuthProvidersManageScope]))],
109 [
110 state.environments[2]!.environmentId,
111 Atom.make<AsyncResult.AsyncResult<AuthSessionState>>(
112 AsyncResult.initial<AuthSessionState>(),
113 ),
114 ],
115 ],
116 );
117 state.sessions = sessions;
118 return { sessions };
120​
121it("bulk provider updates exclude connections without a confirmed management grant", async () => {
122 const fixture = setup();
123 let updates!: ReturnType<typeof useProviderUpdates>;
124 function Probe() {
125 const nextUpdates = useProviderUpdates();
126 useEffect(() => {
127 updates = nextUpdates;
128 }, [nextUpdates]);
129 return null;
130 }
131 await using _screen = await render(
132 <RegistryContext.Provider value={appAtomRegistry}>
133 <Probe />
134 </RegistryContext.Provider>,
135 );
136​
137 const writableId = state.environments[1]!.environmentId;
138 expect(updates.machines.map(({ environmentId }) => environmentId)).toEqual([writableId]);
139 await act(async () => {
140 await updates.updateAll({ quiet: true });
141 });
142 expect(state.requests).toEqual([writableId]);
143​
144 await act(() => {
145 appAtomRegistry.set(fixture.sessions.get(writableId)!, session(["orchestration:read"]));
146 });
147 expect(updates.machines).toEqual([]);
148 state.requests = [];
149 await act(async () => {
150 await updates.updateAll({ quiet: true });
151 });
152 expect(state.requests).toEqual([]);
153});