The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | AuthProvidersManageScope, |
| 3 | ProjectId, |
| 4 | ProviderInstanceId, |
| 5 | type ServerSettings, |
| 6 | } from "@t3tools/contracts"; |
| 7 | import { describe, expect, it } from "vite-plus/test"; |
| 8 | |
| 9 | import { |
| 10 | buildProviderInstanceOverridePatch, |
| 11 | resolveRemoteOperateAccess, |
| 12 | } from "./ProviderSettingsPanel.logic"; |
| 13 | |
| 14 | describe("buildProviderInstanceOverridePatch", () => { |
| 15 | const projectId = ProjectId.make("project-a"); |
| 16 | const instanceId = ProviderInstanceId.make("codex_work"); |
| 17 | |
| 18 | it("writes the same choice to every checkout of the project", () => { |
| 19 | const siblingId = ProjectId.make("project-a-worktree"); |
| 20 | const settings: Pick<ServerSettings, "projectSettingsOverrides"> = { |
| 21 | projectSettingsOverrides: { |
| 22 | [siblingId]: { newWorktreesStartFromOrigin: true }, |
| 23 | }, |
| 24 | }; |
| 25 | |
| 26 | expect( |
| 27 | buildProviderInstanceOverridePatch(settings, [projectId, siblingId], instanceId, false), |
| 28 | ).toEqual({ |
| 29 | [projectId]: { providerInstances: { [instanceId]: { enabled: false } } }, |
| 30 | [siblingId]: { |
| 31 | newWorktreesStartFromOrigin: true, |
| 32 | providerInstances: { [instanceId]: { enabled: false } }, |
| 33 | }, |
| 34 | }); |
| 35 | }); |
| 36 | |
| 37 | it("resets the only override by clearing the whole project entry", () => { |
| 38 | const settings: Pick<ServerSettings, "projectSettingsOverrides"> = { |
| 39 | projectSettingsOverrides: { |
| 40 | [projectId]: { providerInstances: { [instanceId]: { enabled: true } } }, |
| 41 | }, |
| 42 | }; |
| 43 | |
| 44 | expect( |
| 45 | buildProviderInstanceOverridePatch(settings, [projectId], instanceId, undefined), |
| 46 | ).toEqual({ [projectId]: null }); |
| 47 | }); |
| 48 | |
| 49 | it("sends providerInstances: {} explicitly when clearing the last instance override leaves other keys", () => { |
| 50 | const settings: Pick<ServerSettings, "projectSettingsOverrides"> = { |
| 51 | projectSettingsOverrides: { |
| 52 | [projectId]: { |
| 53 | newWorktreesStartFromOrigin: true, |
| 54 | providerInstances: { [instanceId]: { enabled: true } }, |
| 55 | }, |
| 56 | }, |
| 57 | }; |
| 58 | |
| 59 | expect( |
| 60 | buildProviderInstanceOverridePatch(settings, [projectId], instanceId, undefined), |
| 61 | ).toEqual({ |
| 62 | [projectId]: { newWorktreesStartFromOrigin: true, providerInstances: {} }, |
| 63 | }); |
| 64 | }); |
| 65 | }); |
| 66 | |
| 67 | describe("remote operate access", () => { |
| 68 | it("does not treat the old orchestration grant as provider management", () => { |
| 69 | expect( |
| 70 | resolveRemoteOperateAccess({ |
| 71 | session: { |
| 72 | authenticated: true, |
| 73 | scopes: ["orchestration:operate"], |
| 74 | auth: { serverUpdateScope: "environment:maintain" }, |
| 75 | }, |
| 76 | isPending: false, |
| 77 | hasError: false, |
| 78 | }), |
| 79 | ).toBe("denied"); |
| 80 | }); |
| 81 | |
| 82 | it("accepts the orchestration grant from a server that predates providers:manage", () => { |
| 83 | expect( |
| 84 | resolveRemoteOperateAccess({ |
| 85 | session: { authenticated: true, scopes: ["orchestration:operate"], auth: {} }, |
| 86 | isPending: false, |
| 87 | hasError: false, |
| 88 | }), |
| 89 | ).toBe("granted"); |
| 90 | }); |
| 91 | it("derives access from the environment session's granted scopes", () => { |
| 92 | expect( |
| 93 | resolveRemoteOperateAccess({ |
| 94 | session: { authenticated: true, scopes: [AuthProvidersManageScope] }, |
| 95 | isPending: false, |
| 96 | hasError: false, |
| 97 | }), |
| 98 | ).toBe("granted"); |
| 99 | expect( |
| 100 | resolveRemoteOperateAccess({ |
| 101 | session: { authenticated: true, scopes: ["orchestration:read"] }, |
| 102 | isPending: false, |
| 103 | hasError: false, |
| 104 | }), |
| 105 | ).toBe("denied"); |
| 106 | expect( |
| 107 | resolveRemoteOperateAccess({ |
| 108 | session: { authenticated: false }, |
| 109 | isPending: false, |
| 110 | hasError: false, |
| 111 | }), |
| 112 | ).toBe("denied"); |
| 113 | }); |
| 114 | }); |