| 1 | import { DEFAULT_SERVER_SETTINGS, EnvironmentId, ProjectId } from "@t3tools/contracts"; |
| 2 | import { resolveProjectSettings } from "@t3tools/shared/projectSettings"; |
| 3 | import { describe, expect, it } from "vite-plus/test"; |
| 4 | |
| 5 | import { settingInheritanceLayers } from "./SettingInheritance"; |
| 6 | |
| 7 | const environmentId = EnvironmentId.make("laptop"); |
| 8 | const projectId = ProjectId.make("project"); |
| 9 | |
| 10 | describe("settingInheritanceLayers", () => { |
| 11 | it("marks the built-in default effective when nothing is set", () => { |
| 12 | const resolved = resolveProjectSettings(DEFAULT_SERVER_SETTINGS, null); |
| 13 | const layers = settingInheritanceLayers( |
| 14 | { environmentId, label: "Laptop", projectId: null, ...resolved }, |
| 15 | DEFAULT_SERVER_SETTINGS, |
| 16 | "defaultAutoPull", |
| 17 | ); |
| 18 | expect(layers.map((layer) => [layer.label, layer.value, layer.effective])).toEqual([ |
| 19 | ["Laptop", "Inherits", false], |
| 20 | ["Default", "Off", true], |
| 21 | ]); |
| 22 | }); |
| 23 | |
| 24 | it("walks project override, environment value, then built-in default", () => { |
| 25 | const settings = { |
| 26 | ...DEFAULT_SERVER_SETTINGS, |
| 27 | defaultAutoPull: true, |
| 28 | projectSettingsOverrides: { [projectId]: { defaultAutoPull: false } }, |
| 29 | }; |
| 30 | const resolved = resolveProjectSettings(settings, projectId); |
| 31 | const layers = settingInheritanceLayers( |
| 32 | { environmentId, label: "Laptop", projectId, ...resolved }, |
| 33 | settings, |
| 34 | "defaultAutoPull", |
| 35 | ); |
| 36 | expect(layers.map((layer) => [layer.label, layer.value, layer.effective])).toEqual([ |
| 37 | ["Project", "Off", true], |
| 38 | ["Laptop", "On", false], |
| 39 | ["Default", "Off", false], |
| 40 | ]); |
| 41 | const inherited = settingInheritanceLayers( |
| 42 | { |
| 43 | environmentId, |
| 44 | label: "Laptop", |
| 45 | projectId, |
| 46 | ...resolveProjectSettings(settings, ProjectId.make("other")), |
| 47 | }, |
| 48 | settings, |
| 49 | "defaultAutoPull", |
| 50 | ); |
| 51 | expect(inherited.map((layer) => [layer.value, layer.effective])).toEqual([ |
| 52 | ["Inherits", false], |
| 53 | ["On", true], |
| 54 | ["Off", false], |
| 55 | ]); |
| 56 | }); |
| 57 | |
| 58 | it("shows the checkout's t3.json as a layer for file-backed keys", () => { |
| 59 | const file = { defaultThreadEnvMode: "worktree" as const }; |
| 60 | const fromFile = settingInheritanceLayers( |
| 61 | { |
| 62 | environmentId, |
| 63 | label: "Laptop", |
| 64 | projectId, |
| 65 | ...resolveProjectSettings(DEFAULT_SERVER_SETTINGS, projectId, null, file), |
| 66 | }, |
| 67 | DEFAULT_SERVER_SETTINGS, |
| 68 | "defaultThreadEnvMode", |
| 69 | ); |
| 70 | expect(fromFile.map((layer) => [layer.label, layer.value, layer.effective])).toEqual([ |
| 71 | ["Project", "Inherits", false], |
| 72 | ["Laptop", "Inherits", false], |
| 73 | ["t3.json", "New worktree", true], |
| 74 | ["Default", "Current checkout", false], |
| 75 | ]); |
| 76 | const settings = { ...DEFAULT_SERVER_SETTINGS, defaultThreadEnvMode: "local" as const }; |
| 77 | const fromEnvironment = settingInheritanceLayers( |
| 78 | { |
| 79 | environmentId, |
| 80 | label: "Laptop", |
| 81 | projectId, |
| 82 | ...resolveProjectSettings(settings, projectId, null, file), |
| 83 | }, |
| 84 | settings, |
| 85 | "defaultThreadEnvMode", |
| 86 | ); |
| 87 | expect(fromEnvironment.map((layer) => [layer.value, layer.effective])).toEqual([ |
| 88 | ["Inherits", false], |
| 89 | ["Current checkout", true], |
| 90 | ["Inherits", false], |
| 91 | ["Current checkout", false], |
| 92 | ]); |
| 93 | }); |
| 94 | }); |