|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { validatePluginConfig } from "./loader-shared.js"; |
| 3 | + |
| 4 | +const emptyObjectSchema = { |
| 5 | + type: "object", |
| 6 | + additionalProperties: false, |
| 7 | + properties: {}, |
| 8 | +} as const; |
| 9 | + |
| 10 | +function withSchemaKeyword(key: "if" | "then" | "else", value: unknown) { |
| 11 | + return { [key]: value }; |
| 12 | +} |
| 13 | + |
| 14 | +describe("validatePluginConfig empty schema classification", () => { |
| 15 | + it("validates pattern properties instead of requiring empty config", () => { |
| 16 | + const schema = { |
| 17 | + ...emptyObjectSchema, |
| 18 | + patternProperties: { "^S_": { type: "string" } }, |
| 19 | + }; |
| 20 | + |
| 21 | + expect(validatePluginConfig({ schema, value: { S_SETTING: "configured" } })).toEqual({ |
| 22 | + ok: true, |
| 23 | + value: { S_SETTING: "configured" }, |
| 24 | + }); |
| 25 | + expect(validatePluginConfig({ schema, value: { S_SETTING: 42 } })).toMatchObject({ |
| 26 | + ok: false, |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + it("validates dependent schemas instead of using the empty-config shortcut", () => { |
| 31 | + const result = validatePluginConfig({ |
| 32 | + schema: { |
| 33 | + ...emptyObjectSchema, |
| 34 | + dependentSchemas: { mode: { required: ["token"] } }, |
| 35 | + }, |
| 36 | + value: { mode: true }, |
| 37 | + }); |
| 38 | + |
| 39 | + expect(result).toMatchObject({ ok: false }); |
| 40 | + if (!result.ok) { |
| 41 | + expect(result.error.join(" ")).not.toContain("config must be empty"); |
| 42 | + } |
| 43 | + }); |
| 44 | + |
| 45 | + it.each([ |
| 46 | + { |
| 47 | + branch: "then", |
| 48 | + schema: { |
| 49 | + ...withSchemaKeyword("if", true), |
| 50 | + ...withSchemaKeyword("then", { minProperties: 1 }), |
| 51 | + }, |
| 52 | + }, |
| 53 | + { |
| 54 | + branch: "else", |
| 55 | + schema: { |
| 56 | + ...withSchemaKeyword("if", false), |
| 57 | + ...withSchemaKeyword("else", { minProperties: 1 }), |
| 58 | + }, |
| 59 | + }, |
| 60 | + ])("applies an active $branch conditional", ({ schema }) => { |
| 61 | + expect( |
| 62 | + validatePluginConfig({ schema: { ...emptyObjectSchema, ...schema }, value: {} }), |
| 63 | + ).toMatchObject({ ok: false }); |
| 64 | + }); |
| 65 | + |
| 66 | + it.each([ |
| 67 | + withSchemaKeyword("if", true), |
| 68 | + withSchemaKeyword("then", { minProperties: 1 }), |
| 69 | + withSchemaKeyword("else", { minProperties: 1 }), |
| 70 | + ])("keeps standalone conditional keywords on the empty-config path: %o", (keyword) => { |
| 71 | + expect( |
| 72 | + validatePluginConfig({ |
| 73 | + schema: { ...emptyObjectSchema, ...keyword }, |
| 74 | + value: { unexpected: true }, |
| 75 | + }), |
| 76 | + ).toEqual({ ok: false, error: ["<root>: config must be empty"] }); |
| 77 | + }); |
| 78 | +}); |
0 commit comments