| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | import type { Setting } from "@g1t/contracts"; |
| 4 | import { settingOverride } from "./setting-overrides.ts"; |
| 5 | |
| 6 | const workspace: Setting = { |
| 7 | id: "w", |
| 8 | name: "TOKEN", |
| 9 | kind: "secret", |
| 10 | scope: "workspace", |
| 11 | value: null, |
| 12 | updatedAt: "", |
| 13 | environments: [], |
| 14 | availableTo: ["workflows", "deployments"], |
| 15 | projects: [], |
| 16 | }; |
| 17 | const project: Setting = { ...workspace, id: "p", scope: "project" }; |
| 18 | |
| 19 | test("full overrides remain visible on both scopes", () => { |
| 20 | assert.equal(settingOverride(workspace, [workspace, project]), "Overridden by project"); |
| 21 | assert.match(settingOverride(project, [workspace, project])!, /^Overrides workspace/); |
| 22 | }); |
| 23 | |
| 24 | test("environment-restricted project keys still suppress workspace fallback", () => { |
| 25 | assert.equal(settingOverride(workspace, [workspace, { ...project, environments: ["production"] }]), "Overridden by project"); |
| 26 | assert.equal( |
| 27 | settingOverride({ ...workspace, environments: ["production"] }, [workspace, { ...project, environments: ["preview"] }]), |
| 28 | "Overridden by project", |
| 29 | ); |
| 30 | }); |
| 31 | |
| 32 | test("only shared readers are overridden", () => { |
| 33 | assert.equal(settingOverride(workspace, [workspace, { ...project, availableTo: ["workflows"] }]), "Partially overridden: Workflows"); |
| 34 | assert.equal( |
| 35 | settingOverride(workspace, [workspace, { ...project, availableTo: ["workflows"] }, { ...project, availableTo: ["deployments"] }]), |
| 36 | "Overridden by project", |
| 37 | ); |
| 38 | }); |
| 39 | |
| 40 | test("different names, kinds and readers do not override", () => { |
| 41 | const scoped = { ...workspace, availableTo: ["workflows"] }; |
| 42 | for (const other of [ |
| 43 | { ...project, name: "OTHER" }, |
| 44 | { ...project, kind: "variable" }, |
| 45 | { ...project, availableTo: ["deployments"] }, |
| 46 | ]) { |
| 47 | assert.equal(settingOverride(scoped, [scoped, other]), null); |
| 48 | } |
| 49 | }); |