|
| 1 | +/** |
| 2 | + * Tests for `check-cs-config.ts`. |
| 3 | + */ |
| 4 | + |
| 5 | +import * as assert from "node:assert/strict"; |
| 6 | +import { describe, it } from "node:test"; |
| 7 | + |
| 8 | +import type { UserConfig } from "../src/config/db-config"; |
| 9 | + |
| 10 | +import { checkConfiguration } from "./check-cs-config"; |
| 11 | + |
| 12 | +describe("checkConfiguration", async () => { |
| 13 | + await it("passes when actual and expected configs match", () => { |
| 14 | + const actual: UserConfig = { name: "test-config", paths: ["src"] }; |
| 15 | + const expected = JSON.stringify(actual); |
| 16 | + assert.doesNotThrow(() => checkConfiguration(actual, expected)); |
| 17 | + }); |
| 18 | + |
| 19 | + await it("passes when queries arrays match after sorting", () => { |
| 20 | + const actual: UserConfig = { paths: ["b", "a", "c"] }; |
| 21 | + const expected = JSON.stringify(actual); |
| 22 | + assert.doesNotThrow(() => checkConfiguration(actual, expected)); |
| 23 | + }); |
| 24 | + |
| 25 | + await it("throws when actual config does not match expected", () => { |
| 26 | + const actual: UserConfig = { name: "actual-name" }; |
| 27 | + const expected = JSON.stringify({ |
| 28 | + name: "expected-name", |
| 29 | + } satisfies UserConfig); |
| 30 | + assert.throws(() => checkConfiguration(actual, expected), { |
| 31 | + message: /Expected configuration does not match actual configuration/, |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + await it("throws when expected contents are empty", () => { |
| 36 | + assert.throws(() => checkConfiguration({}, ""), { |
| 37 | + message: /No expected configuration provided/, |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + await it("throws when expected contents are only whitespace", () => { |
| 42 | + assert.throws(() => checkConfiguration({}, " "), { |
| 43 | + message: /No expected configuration provided/, |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + await it("passes with complex config", () => { |
| 48 | + const actual: UserConfig = { |
| 49 | + name: "complex", |
| 50 | + "disable-default-queries": true, |
| 51 | + paths: ["src", "lib"], |
| 52 | + "paths-ignore": ["test"], |
| 53 | + "threat-models": ["remote"], |
| 54 | + }; |
| 55 | + const expected = JSON.stringify(actual); |
| 56 | + assert.doesNotThrow(() => checkConfiguration(actual, expected)); |
| 57 | + }); |
| 58 | + |
| 59 | + await it("trims whitespace from expected contents before parsing", () => { |
| 60 | + const actual: UserConfig = { name: "trimmed" }; |
| 61 | + const expected = ` ${JSON.stringify(actual)} `; |
| 62 | + assert.doesNotThrow(() => checkConfiguration(actual, expected)); |
| 63 | + }); |
| 64 | + |
| 65 | + await it("passes when both configs are empty objects", () => { |
| 66 | + assert.doesNotThrow(() => checkConfiguration({}, "{}")); |
| 67 | + }); |
| 68 | +}); |
0 commit comments