The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { |
| 2 | DEFAULT_SERVER_SETTINGS, |
| 3 | type EnvironmentId, |
| 4 | type ProjectId, |
| 5 | type ServerSettings, |
| 6 | } from "@t3tools/contracts"; |
| 7 | import { describe, expect, it } from "vite-plus/test"; |
| 8 | |
| 9 | import type { SettingsTarget } from "./settings-environment-filter"; |
| 10 | import { |
| 11 | managedSettingReason, |
| 12 | planMobileScopedSettingsClear, |
| 13 | planMobileScopedSettingsPatch, |
| 14 | resolveMobileSettingsTargets, |
| 15 | uniformMobileSetting, |
| 16 | } from "./settings-scoped-server"; |
| 17 | |
| 18 | const firstId = "first" as EnvironmentId; |
| 19 | const secondId = "second" as EnvironmentId; |
| 20 | const firstProject = "first-project" as ProjectId; |
| 21 | const secondProject = "second-project" as ProjectId; |
| 22 | |
| 23 | describe("mobile usage-limit settings across environments", () => { |
| 24 | it.each(["autoResumeLimitedThreads", "snoozeLimitedThreads"] as const)( |
| 25 | "shows %s as mixed and can enable it everywhere without changing other settings", |
| 26 | (key) => { |
| 27 | const targets = resolveMobileSettingsTargets( |
| 28 | [ |
| 29 | environment(firstId, { ...DEFAULT_SERVER_SETTINGS, [key]: true }), |
| 30 | environment(secondId, { ...DEFAULT_SERVER_SETTINGS, [key]: false }), |
| 31 | ], |
| 32 | null, |
| 33 | ); |
| 34 | |
| 35 | expect(uniformMobileSetting(targets, key)).toBeNull(); |
| 36 | const writes = planMobileScopedSettingsPatch(targets, false, { [key]: true }); |
| 37 | expect(writes).toEqual([ |
| 38 | { environmentId: firstId, patch: { [key]: true } }, |
| 39 | { environmentId: secondId, patch: { [key]: true } }, |
| 40 | ]); |
| 41 | const updatedTargets = targets.map((target) => ({ |
| 42 | ...target, |
| 43 | settings: { |
| 44 | ...target.settings, |
| 45 | [key]: |
| 46 | writes.find((write) => write.environmentId === target.environment.environmentId)?.patch[ |
| 47 | key |
| 48 | ] ?? target.settings[key], |
| 49 | }, |
| 50 | })); |
| 51 | expect(uniformMobileSetting(updatedTargets, key)).toBe(true); |
| 52 | expect(uniformMobileSetting(targets.slice(0, 1), key)).toBe(true); |
| 53 | expect(uniformMobileSetting(targets.slice(1), key)).toBe(false); |
| 54 | expect(uniformMobileSetting([], key)).toBeNull(); |
| 55 | }, |
| 56 | ); |
| 57 | }); |
| 58 | |
| 59 | function environment(environmentId: EnvironmentId, settings: ServerSettings): SettingsTarget { |
| 60 | return { |
| 61 | environmentId, |
| 62 | serverConfig: { |
| 63 | settings, |
| 64 | environment: { capabilities: { projectSettingsOverrides: true } }, |
| 65 | }, |
| 66 | } as SettingsTarget; |
| 67 | } |
| 68 | |
| 69 | describe("mobile project settings scope", () => { |
| 70 | it("edits each checkout's own override without changing either environment default", () => { |
| 71 | const firstSettings: ServerSettings = { |
| 72 | ...DEFAULT_SERVER_SETTINGS, |
| 73 | responseStreamingMode: "paragraph", |
| 74 | projectSettingsOverrides: { [firstProject]: { defaultAutoPull: true } }, |
| 75 | }; |
| 76 | const secondSettings: ServerSettings = { |
| 77 | ...DEFAULT_SERVER_SETTINGS, |
| 78 | responseStreamingMode: "turn", |
| 79 | projectSettingsOverrides: {}, |
| 80 | }; |
| 81 | const targets = resolveMobileSettingsTargets( |
| 82 | [environment(firstId, firstSettings), environment(secondId, secondSettings)], |
| 83 | [ |
| 84 | { environmentId: firstId, id: firstProject }, |
| 85 | { environmentId: secondId, id: secondProject }, |
| 86 | ], |
| 87 | ); |
| 88 | |
| 89 | const writes = planMobileScopedSettingsPatch(targets, true, { |
| 90 | responseStreamingMode: "turn", |
| 91 | }); |
| 92 | expect(writes).toEqual([ |
| 93 | { |
| 94 | environmentId: firstId, |
| 95 | patch: { |
| 96 | projectSettingsOverrides: { |
| 97 | [firstProject]: { defaultAutoPull: true, responseStreamingMode: "turn" }, |
| 98 | }, |
| 99 | }, |
| 100 | }, |
| 101 | { |
| 102 | environmentId: secondId, |
| 103 | patch: { projectSettingsOverrides: { [secondProject]: { responseStreamingMode: "turn" } } }, |
| 104 | }, |
| 105 | ]); |
| 106 | expect(firstSettings.responseStreamingMode).toBe("paragraph"); |
| 107 | expect(secondSettings.responseStreamingMode).toBe("turn"); |
| 108 | }); |
| 109 | |
| 110 | it("removes a project override when a picker sends null for a key that cannot store it", () => { |
| 111 | const settings: ServerSettings = { |
| 112 | ...DEFAULT_SERVER_SETTINGS, |
| 113 | projectSettingsOverrides: { |
| 114 | [firstProject]: { defaultThreadEnvMode: "worktree", defaultAutoPull: true }, |
| 115 | }, |
| 116 | }; |
| 117 | const targets = resolveMobileSettingsTargets( |
| 118 | [environment(firstId, settings)], |
| 119 | [{ environmentId: firstId, id: firstProject }], |
| 120 | ); |
| 121 | expect(planMobileScopedSettingsPatch(targets, true, { defaultThreadEnvMode: null })).toEqual([ |
| 122 | { |
| 123 | environmentId: firstId, |
| 124 | patch: { projectSettingsOverrides: { [firstProject]: { defaultAutoPull: true } } }, |
| 125 | }, |
| 126 | ]); |
| 127 | expect(planMobileScopedSettingsPatch(targets, true, { defaultModelSelection: null })).toEqual([ |
| 128 | { |
| 129 | environmentId: firstId, |
| 130 | patch: { |
| 131 | projectSettingsOverrides: { |
| 132 | [firstProject]: { |
| 133 | defaultThreadEnvMode: "worktree", |
| 134 | defaultAutoPull: true, |
| 135 | defaultModelSelection: null, |
| 136 | }, |
| 137 | }, |
| 138 | }, |
| 139 | }, |
| 140 | ]); |
| 141 | }); |
| 142 | |
| 143 | it("resets only the selected page's override and rejects environment-wide writes", () => { |
| 144 | const settings: ServerSettings = { |
| 145 | ...DEFAULT_SERVER_SETTINGS, |
| 146 | projectSettingsOverrides: { |
| 147 | [firstProject]: { defaultAutoPull: true, responseStreamingMode: "turn" }, |
| 148 | }, |
| 149 | }; |
| 150 | const targets = resolveMobileSettingsTargets( |
| 151 | [environment(firstId, settings)], |
| 152 | [{ environmentId: firstId, id: firstProject }], |
| 153 | ); |
| 154 | |
| 155 | expect(planMobileScopedSettingsClear(targets, ["responseStreamingMode"])).toEqual([ |
| 156 | { |
| 157 | environmentId: firstId, |
| 158 | patch: { projectSettingsOverrides: { [firstProject]: { defaultAutoPull: true } } }, |
| 159 | }, |
| 160 | ]); |
| 161 | expect( |
| 162 | planMobileScopedSettingsPatch(targets, true, { enableProviderUpdateChecks: false }), |
| 163 | ).toEqual([]); |
| 164 | }); |
| 165 | }); |
| 166 | |
| 167 | describe("managed settings across environments", () => { |
| 168 | it("names the manager when any selected environment locks the setting", () => { |
| 169 | const managed = { |
| 170 | ...environment(secondId, DEFAULT_SERVER_SETTINGS), |
| 171 | serverConfig: { |
| 172 | ...environment(secondId, DEFAULT_SERVER_SETTINGS).serverConfig, |
| 173 | managedPolicy: { organization: "Example Corp", lockedSettings: ["storageCleanup"] }, |
| 174 | }, |
| 175 | } as SettingsTarget; |
| 176 | const targets = resolveMobileSettingsTargets( |
| 177 | [environment(firstId, DEFAULT_SERVER_SETTINGS), managed], |
| 178 | null, |
| 179 | ); |
| 180 | |
| 181 | expect(managedSettingReason(targets, ["storageCleanup.logsAfterDays"])).toBe( |
| 182 | "Managed by Example Corp.", |
| 183 | ); |
| 184 | expect(managedSettingReason(targets, ["defaultAutoPull", "storageCleanup"])).toBe( |
| 185 | "Managed by Example Corp.", |
| 186 | ); |
| 187 | expect(managedSettingReason(targets, ["defaultAutoPull"])).toBeUndefined(); |
| 188 | expect(managedSettingReason(targets.slice(0, 1), ["storageCleanup"])).toBeUndefined(); |
| 189 | }); |
| 190 | }); |