|
1 | 1 | import { afterEach, beforeEach, describe, expect, test } from "bun:test"; |
2 | | -import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs"; |
| 2 | +import { |
| 3 | + existsSync, |
| 4 | + mkdirSync, |
| 5 | + readFileSync, |
| 6 | + rmSync, |
| 7 | + writeFileSync, |
| 8 | +} from "node:fs"; |
3 | 9 | import { tmpdir } from "node:os"; |
4 | 10 | import { join } from "node:path"; |
5 | 11 | import { |
@@ -286,4 +292,80 @@ describe("verify-config", () => { |
286 | 292 | expect(stdout).toContain("✓ Project config:"); |
287 | 293 | }); |
288 | 294 | }); |
| 295 | + |
| 296 | + describe("schema auto-add", () => { |
| 297 | + function readProjectConfig(): Record<string, unknown> { |
| 298 | + return JSON.parse(readFileSync(projectConfigPath, "utf-8")); |
| 299 | + } |
| 300 | + |
| 301 | + function readUserConfig(): Record<string, unknown> { |
| 302 | + return JSON.parse(readFileSync(userConfigPath, "utf-8")); |
| 303 | + } |
| 304 | + |
| 305 | + test("adds $schema to valid project config missing it", () => { |
| 306 | + writeProjectConfig('{"version": 1}'); |
| 307 | + runMain(); |
| 308 | + const config = readProjectConfig(); |
| 309 | + expect(config.$schema).toBe( |
| 310 | + "https://raw.githubusercontent.com/kenryu42/claude-code-safety-net/main/assets/cc-safety-net.schema.json", |
| 311 | + ); |
| 312 | + }); |
| 313 | + |
| 314 | + test("adds $schema to valid user config missing it", () => { |
| 315 | + writeUserConfig('{"version": 1}'); |
| 316 | + runMain(); |
| 317 | + const config = readUserConfig(); |
| 318 | + expect(config.$schema).toBe( |
| 319 | + "https://raw.githubusercontent.com/kenryu42/claude-code-safety-net/main/assets/cc-safety-net.schema.json", |
| 320 | + ); |
| 321 | + }); |
| 322 | + |
| 323 | + test("does not modify config that already has $schema", () => { |
| 324 | + const originalConfig = { |
| 325 | + $schema: |
| 326 | + "https://raw.githubusercontent.com/kenryu42/claude-code-safety-net/main/assets/cc-safety-net.schema.json", |
| 327 | + version: 1, |
| 328 | + }; |
| 329 | + writeProjectConfig(JSON.stringify(originalConfig, null, 2)); |
| 330 | + runMain(); |
| 331 | + const config = readProjectConfig(); |
| 332 | + expect(config).toEqual(originalConfig); |
| 333 | + }); |
| 334 | + |
| 335 | + test("preserves existing rules when adding $schema", () => { |
| 336 | + const originalConfig = { |
| 337 | + version: 1, |
| 338 | + rules: [ |
| 339 | + { |
| 340 | + name: "block-foo", |
| 341 | + command: "foo", |
| 342 | + block_args: ["-x"], |
| 343 | + reason: "Blocked", |
| 344 | + }, |
| 345 | + ], |
| 346 | + }; |
| 347 | + writeProjectConfig(JSON.stringify(originalConfig)); |
| 348 | + runMain(); |
| 349 | + const config = readProjectConfig(); |
| 350 | + expect(config.$schema).toBe( |
| 351 | + "https://raw.githubusercontent.com/kenryu42/claude-code-safety-net/main/assets/cc-safety-net.schema.json", |
| 352 | + ); |
| 353 | + expect(config.version).toBe(1); |
| 354 | + expect(config.rules).toEqual(originalConfig.rules); |
| 355 | + }); |
| 356 | + |
| 357 | + test("does not add $schema to invalid config", () => { |
| 358 | + writeProjectConfig('{"version": 2}'); |
| 359 | + runMain(); |
| 360 | + const config = readProjectConfig(); |
| 361 | + expect(config.$schema).toBeUndefined(); |
| 362 | + }); |
| 363 | + |
| 364 | + test("prints message when $schema is added", () => { |
| 365 | + writeProjectConfig('{"version": 1}'); |
| 366 | + runMain(); |
| 367 | + const output = getStdout(); |
| 368 | + expect(output).toContain("Added $schema"); |
| 369 | + }); |
| 370 | + }); |
289 | 371 | }); |
0 commit comments