The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { DEFAULT_UNIFIED_SETTINGS } from "@t3tools/contracts"; |
| 2 | import * as Duration from "effect/Duration"; |
| 3 | import { describe, expect, it } from "vite-plus/test"; |
| 4 | import { |
| 5 | autoSettleDaysField, |
| 6 | type AutoSettleDaysField, |
| 7 | backgroundActivitySharedPolicySettings, |
| 8 | cancelAutoSettleDaysField, |
| 9 | commitAutoSettleDaysField, |
| 10 | editAutoSettleDaysField, |
| 11 | failAutoSettleDaysField, |
| 12 | getChangedBrowserSettingLabels, |
| 13 | hasChangedBackgroundActivitySettings, |
| 14 | syncAutoSettleDaysField, |
| 15 | } from "./SettingsPanels.logic"; |
| 16 | |
| 17 | describe("background activity settings restore", () => { |
| 18 | it("detects legacy interval values even when the structured setting is at its default", () => { |
| 19 | expect( |
| 20 | hasChangedBackgroundActivitySettings({ |
| 21 | backgroundActivity: DEFAULT_UNIFIED_SETTINGS.backgroundActivity, |
| 22 | backgroundActivityProfile: DEFAULT_UNIFIED_SETTINGS.backgroundActivityProfile, |
| 23 | automaticGitFetchInterval: Duration.seconds(45), |
| 24 | providerHealthRefreshInterval: DEFAULT_UNIFIED_SETTINGS.providerHealthRefreshInterval, |
| 25 | }), |
| 26 | ).toBe(true); |
| 27 | expect( |
| 28 | hasChangedBackgroundActivitySettings({ |
| 29 | backgroundActivity: DEFAULT_UNIFIED_SETTINGS.backgroundActivity, |
| 30 | backgroundActivityProfile: DEFAULT_UNIFIED_SETTINGS.backgroundActivityProfile, |
| 31 | automaticGitFetchInterval: DEFAULT_UNIFIED_SETTINGS.automaticGitFetchInterval, |
| 32 | providerHealthRefreshInterval: Duration.minutes(7), |
| 33 | }), |
| 34 | ).toBe(true); |
| 35 | expect(hasChangedBackgroundActivitySettings(DEFAULT_UNIFIED_SETTINGS)).toBe(false); |
| 36 | }); |
| 37 | |
| 38 | it("preserves advanced overrides when the shared policy changes", () => { |
| 39 | const automaticGitFetchInterval = Duration.seconds(42); |
| 40 | expect( |
| 41 | backgroundActivitySharedPolicySettings( |
| 42 | { |
| 43 | ...DEFAULT_UNIFIED_SETTINGS, |
| 44 | backgroundActivity: { |
| 45 | schemaVersion: 1, |
| 46 | profile: "custom", |
| 47 | baseProfile: "balanced", |
| 48 | overrides: { |
| 49 | automaticGitFetchInterval, |
| 50 | pauseWhenOnBattery: true, |
| 51 | }, |
| 52 | }, |
| 53 | }, |
| 54 | "performance", |
| 55 | ), |
| 56 | ).toEqual({ |
| 57 | schemaVersion: 1, |
| 58 | profile: "custom", |
| 59 | baseProfile: "performance", |
| 60 | overrides: { |
| 61 | automaticGitFetchInterval, |
| 62 | pauseWhenOnBattery: true, |
| 63 | }, |
| 64 | }); |
| 65 | }); |
| 66 | |
| 67 | it("materializes legacy advanced overrides before changing the shared policy", () => { |
| 68 | const automaticGitFetchInterval = Duration.seconds(42); |
| 69 | expect( |
| 70 | backgroundActivitySharedPolicySettings( |
| 71 | { |
| 72 | ...DEFAULT_UNIFIED_SETTINGS, |
| 73 | automaticGitFetchInterval, |
| 74 | }, |
| 75 | "battery-saver", |
| 76 | ), |
| 77 | ).toEqual({ |
| 78 | schemaVersion: 1, |
| 79 | profile: "custom", |
| 80 | baseProfile: "battery-saver", |
| 81 | overrides: { |
| 82 | automaticGitFetchInterval, |
| 83 | }, |
| 84 | }); |
| 85 | }); |
| 86 | }); |
| 87 | |
| 88 | describe("getChangedBrowserSettingLabels", () => { |
| 89 | it("counts a retained custom search URL so restore clears it", () => { |
| 90 | expect( |
| 91 | getChangedBrowserSettingLabels({ |
| 92 | ...DEFAULT_UNIFIED_SETTINGS, |
| 93 | browserCustomSearchUrl: "https://search.example/?q=%s", |
| 94 | }), |
| 95 | ).toEqual(["Search engine"]); |
| 96 | }); |
| 97 | }); |
| 98 | |
| 99 | describe("auto-settle days field", () => { |
| 100 | const type = (field: AutoSettleDaysField, ...drafts: string[]) => |
| 101 | drafts.reduce(editAutoSettleDaysField, field); |
| 102 | |
| 103 | it("saves only the finished edit, never a value typed on the way", () => { |
| 104 | const typed = type(autoSettleDaysField(14), "1", "", "3", "30"); |
| 105 | expect(typed.pending).toBeNull(); |
| 106 | |
| 107 | const committed = commitAutoSettleDaysField(typed); |
| 108 | expect(committed.submit).toBe(30); |
| 109 | expect(commitAutoSettleDaysField(committed.field).submit).toBeNull(); |
| 110 | |
| 111 | const confirmed = syncAutoSettleDaysField(committed.field, 30); |
| 112 | expect(confirmed).toMatchObject({ saved: 30, draft: "30", pending: null }); |
| 113 | }); |
| 114 | |
| 115 | it.each(["", "0", "91", "1.5", "abc"])("restores the saved value for %j", (draft) => { |
| 116 | const committed = commitAutoSettleDaysField(type(autoSettleDaysField(14), draft)); |
| 117 | expect(committed.submit).toBeNull(); |
| 118 | expect(committed.field.draft).toBe("14"); |
| 119 | }); |
| 120 | |
| 121 | it("a rejected save falls back to the saved value and can be retried", () => { |
| 122 | const submitted = commitAutoSettleDaysField(type(autoSettleDaysField(14), "30")).field; |
| 123 | expect(failAutoSettleDaysField(submitted, submitted.revision).draft).toBe("14"); |
| 124 | |
| 125 | const rejectedWhileCleared = failAutoSettleDaysField(type(submitted, ""), submitted.revision); |
| 126 | expect(rejectedWhileCleared.draft).toBe(""); |
| 127 | expect(commitAutoSettleDaysField(rejectedWhileCleared).field.draft).toBe("14"); |
| 128 | expect(commitAutoSettleDaysField(type(rejectedWhileCleared, "30")).submit).toBe(30); |
| 129 | }); |
| 130 | |
| 131 | it("retyping a value that saved on only some environments sends it again", () => { |
| 132 | const submitted = commitAutoSettleDaysField(type(autoSettleDaysField(14), "30")).field; |
| 133 | const partlySaved = failAutoSettleDaysField( |
| 134 | syncAutoSettleDaysField(submitted, 30), |
| 135 | submitted.revision, |
| 136 | ); |
| 137 | expect(commitAutoSettleDaysField(partlySaved).submit).toBeNull(); |
| 138 | expect(commitAutoSettleDaysField(type(partlySaved, "3", "30")).submit).toBe(30); |
| 139 | }); |
| 140 | |
| 141 | it("returning to the saved value while another save is in flight saves it again", () => { |
| 142 | const submitted = commitAutoSettleDaysField(type(autoSettleDaysField(14), "30")).field; |
| 143 | const back = commitAutoSettleDaysField(type(submitted, "14")); |
| 144 | expect(back.submit).toBe(14); |
| 145 | expect(failAutoSettleDaysField(back.field, submitted.revision)).toBe(back.field); |
| 146 | |
| 147 | const settled = syncAutoSettleDaysField(back.field, 14); |
| 148 | expect(settled.pending).toBeNull(); |
| 149 | expect(syncAutoSettleDaysField(settled, 14)).toBe(settled); |
| 150 | expect(syncAutoSettleDaysField(settled, 20).draft).toBe("20"); |
| 151 | }); |
| 152 | |
| 153 | it("keeps a later save in flight when an earlier one is confirmed", () => { |
| 154 | const first = commitAutoSettleDaysField(type(autoSettleDaysField(14), "30")).field; |
| 155 | const second = commitAutoSettleDaysField(type(first, "60")).field; |
| 156 | const firstConfirmed = syncAutoSettleDaysField(second, 30); |
| 157 | expect(firstConfirmed).toMatchObject({ saved: 30, draft: "60", pending: 60 }); |
| 158 | expect(failAutoSettleDaysField(firstConfirmed, second.revision)).toMatchObject({ |
| 159 | draft: "30", |
| 160 | pending: null, |
| 161 | }); |
| 162 | }); |
| 163 | |
| 164 | it("a cancelled edit restores the saved value and saves nothing", () => { |
| 165 | const cancelled = cancelAutoSettleDaysField(type(autoSettleDaysField(14), "1")); |
| 166 | expect(cancelled).toMatchObject({ draft: "14", dirty: false }); |
| 167 | expect(commitAutoSettleDaysField(cancelled).submit).toBeNull(); |
| 168 | }); |
| 169 | |
| 170 | it("keeps a draft in progress when the saved value changes underneath it", () => { |
| 171 | const typing = type(autoSettleDaysField(14), "4"); |
| 172 | expect(syncAutoSettleDaysField(typing, 30)).toMatchObject({ saved: 30, draft: "4" }); |
| 173 | }); |
| 174 | }); |