Open History to see the last commit
1import {
2 AuthProvidersManageScope,
3 ProjectId,
4 ProviderInstanceId,
5 type ServerSettings,
6} from "@t3tools/contracts";
7import { describe, expect, it } from "vite-plus/test";
8
9import {
10 buildProviderInstanceOverridePatch,
11 resolvePrimaryOperateAccess,
12 resolveRemoteOperateAccess,
13} from "./ProviderSettingsPanel.logic";
14
15describe("primary operate access", () => {
16 it("denies unauthenticated sessions and sessions without the operate scope", () => {
17 expect(
18 resolvePrimaryOperateAccess({
19 isPrimary: true,
20 hasDesktopBridge: false,
21 session: { authenticated: false },
22 isPending: false,
23 hasError: false,
24 }),
25 ).toBe("denied");
26 expect(
27 resolvePrimaryOperateAccess({
28 isPrimary: true,
29 hasDesktopBridge: false,
30 session: { authenticated: true, scopes: ["orchestration:read"] },
31 isPending: false,
32 hasError: false,
33 }),
34 ).toBe("denied");
35 expect(
36 resolvePrimaryOperateAccess({
37 isPrimary: true,
38 hasDesktopBridge: false,
39 session: null,
40 isPending: false,
41 hasError: false,
42 }),
43 ).toBe("denied");
44 });
45});
46
47describe("buildProviderInstanceOverridePatch", () => {
48 const projectId = ProjectId.make("project-a");
49 const instanceId = ProviderInstanceId.make("codex_work");
50
51 it("writes the same choice to every checkout of the project", () => {
52 const siblingId = ProjectId.make("project-a-worktree");
53 const settings: Pick<ServerSettings, "projectSettingsOverrides"> = {
54 projectSettingsOverrides: {
55 [siblingId]: { newWorktreesStartFromOrigin: true },
56 },
57 };
58
59 expect(
60 buildProviderInstanceOverridePatch(settings, [projectId, siblingId], instanceId, false),
61 ).toEqual({
62 [projectId]: { providerInstances: { [instanceId]: { enabled: false } } },
63 [siblingId]: {
64 newWorktreesStartFromOrigin: true,
65 providerInstances: { [instanceId]: { enabled: false } },
66 },
67 });
68 });
69
70 it("resets the only override by clearing the whole project entry", () => {
71 const settings: Pick<ServerSettings, "projectSettingsOverrides"> = {
72 projectSettingsOverrides: {
73 [projectId]: { providerInstances: { [instanceId]: { enabled: true } } },
74 },
75 };
76
77 expect(
78 buildProviderInstanceOverridePatch(settings, [projectId], instanceId, undefined),
79 ).toEqual({ [projectId]: null });
80 });
81
82 it("sends providerInstances: {} explicitly when clearing the last instance override leaves other keys", () => {
83 const settings: Pick<ServerSettings, "projectSettingsOverrides"> = {
84 projectSettingsOverrides: {
85 [projectId]: {
86 newWorktreesStartFromOrigin: true,
87 providerInstances: { [instanceId]: { enabled: true } },
88 },
89 },
90 };
91
92 expect(
93 buildProviderInstanceOverridePatch(settings, [projectId], instanceId, undefined),
94 ).toEqual({
95 [projectId]: { newWorktreesStartFromOrigin: true, providerInstances: {} },
96 });
97 });
98});
99
100describe("remote operate access", () => {
101 it("does not treat the old orchestration grant as provider management", () => {
102 expect(
103 resolveRemoteOperateAccess({
104 session: {
105 authenticated: true,
106 scopes: ["orchestration:operate"],
107 auth: { serverUpdateScope: "environment:maintain" },
108 },
109 isPending: false,
110 hasError: false,
111 }),
112 ).toBe("denied");
113 });
114
115 it("accepts the orchestration grant from a server that predates providers:manage", () => {
116 expect(
117 resolveRemoteOperateAccess({
118 session: { authenticated: true, scopes: ["orchestration:operate"], auth: {} },
119 isPending: false,
120 hasError: false,
121 }),
122 ).toBe("granted");
123 });
124 it("derives access from the environment session's granted scopes", () => {
125 expect(
126 resolveRemoteOperateAccess({
127 session: { authenticated: true, scopes: [AuthProvidersManageScope] },
128 isPending: false,
129 hasError: false,
130 }),
131 ).toBe("granted");
132 expect(
133 resolveRemoteOperateAccess({
134 session: { authenticated: true, scopes: ["orchestration:read"] },
135 isPending: false,
136 hasError: false,
137 }),
138 ).toBe("denied");
139 expect(
140 resolveRemoteOperateAccess({
141 session: { authenticated: false },
142 isPending: false,
143 hasError: false,
144 }),
145 ).toBe("denied");
146 });
147});