|
| 1 | +/** |
| 2 | + * Unit tests for built-in presets |
| 3 | + * Validates that all preset YAML files conform to the schema |
| 4 | + * and contain no host/auth (security requirement) |
| 5 | + */ |
| 6 | + |
| 7 | +import * as fs from "fs"; |
| 8 | +import * as path from "path"; |
| 9 | +import * as yaml from "yaml"; |
| 10 | +import { PresetSchema, Preset } from "../../../src/profiles/types"; |
| 11 | + |
| 12 | +// Mock logger to suppress output during tests |
| 13 | +jest.mock("../../../src/logger", () => ({ |
| 14 | + logger: { |
| 15 | + info: jest.fn(), |
| 16 | + warn: jest.fn(), |
| 17 | + error: jest.fn(), |
| 18 | + debug: jest.fn(), |
| 19 | + }, |
| 20 | +})); |
| 21 | + |
| 22 | +describe("Built-in Presets", () => { |
| 23 | + const builtinDir = path.join(__dirname, "../../../src/profiles/builtin"); |
| 24 | + |
| 25 | + // Get all yaml files in the builtin directory |
| 26 | + const presetFiles = fs.readdirSync(builtinDir).filter(f => f.endsWith(".yaml")); |
| 27 | + |
| 28 | + describe("all presets should be valid", () => { |
| 29 | + it.each(presetFiles)("%s should conform to PresetSchema", filename => { |
| 30 | + const filepath = path.join(builtinDir, filename); |
| 31 | + const content = fs.readFileSync(filepath, "utf8"); |
| 32 | + const parsed = yaml.parse(content); |
| 33 | + |
| 34 | + const result = PresetSchema.safeParse(parsed); |
| 35 | + if (!result.success) { |
| 36 | + console.error(`Validation errors for ${filename}:`, result.error.issues); |
| 37 | + } |
| 38 | + expect(result.success).toBe(true); |
| 39 | + }); |
| 40 | + |
| 41 | + it.each(presetFiles)("%s should have a description", filename => { |
| 42 | + const filepath = path.join(builtinDir, filename); |
| 43 | + const content = fs.readFileSync(filepath, "utf8"); |
| 44 | + const parsed = yaml.parse(content) as Preset; |
| 45 | + |
| 46 | + expect(parsed.description).toBeDefined(); |
| 47 | + expect(parsed.description?.length).toBeGreaterThan(0); |
| 48 | + }); |
| 49 | + |
| 50 | + it.each(presetFiles)("%s should NOT contain host (security)", filename => { |
| 51 | + const filepath = path.join(builtinDir, filename); |
| 52 | + const content = fs.readFileSync(filepath, "utf8"); |
| 53 | + const parsed = yaml.parse(content); |
| 54 | + |
| 55 | + // Presets must NEVER contain host/auth |
| 56 | + expect(parsed.host).toBeUndefined(); |
| 57 | + }); |
| 58 | + |
| 59 | + it.each(presetFiles)("%s should NOT contain auth (security)", filename => { |
| 60 | + const filepath = path.join(builtinDir, filename); |
| 61 | + const content = fs.readFileSync(filepath, "utf8"); |
| 62 | + const parsed = yaml.parse(content); |
| 63 | + |
| 64 | + // Presets must NEVER contain host/auth |
| 65 | + expect(parsed.auth).toBeUndefined(); |
| 66 | + }); |
| 67 | + |
| 68 | + it.each(presetFiles)("%s should have valid denied_tools_regex if present", filename => { |
| 69 | + const filepath = path.join(builtinDir, filename); |
| 70 | + const content = fs.readFileSync(filepath, "utf8"); |
| 71 | + const parsed = yaml.parse(content) as Preset; |
| 72 | + |
| 73 | + if (parsed.denied_tools_regex) { |
| 74 | + expect(() => new RegExp(parsed.denied_tools_regex!)).not.toThrow(); |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + it.each(presetFiles)("%s should have valid denied_actions format if present", filename => { |
| 79 | + const filepath = path.join(builtinDir, filename); |
| 80 | + const content = fs.readFileSync(filepath, "utf8"); |
| 81 | + const parsed = yaml.parse(content) as Preset; |
| 82 | + |
| 83 | + if (parsed.denied_actions) { |
| 84 | + for (const action of parsed.denied_actions) { |
| 85 | + // Format: tool:action |
| 86 | + expect(action).toMatch(/^[a-z_]+:[a-z_]+$/); |
| 87 | + } |
| 88 | + } |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe("specific preset validations", () => { |
| 93 | + it("readonly.yaml should have read_only: true", () => { |
| 94 | + const filepath = path.join(builtinDir, "readonly.yaml"); |
| 95 | + const content = fs.readFileSync(filepath, "utf8"); |
| 96 | + const parsed = yaml.parse(content) as Preset; |
| 97 | + |
| 98 | + expect(parsed.read_only).toBe(true); |
| 99 | + }); |
| 100 | + |
| 101 | + it("admin.yaml should have all features enabled", () => { |
| 102 | + const filepath = path.join(builtinDir, "admin.yaml"); |
| 103 | + const content = fs.readFileSync(filepath, "utf8"); |
| 104 | + const parsed = yaml.parse(content) as Preset; |
| 105 | + |
| 106 | + expect(parsed.features?.wiki).toBe(true); |
| 107 | + expect(parsed.features?.pipelines).toBe(true); |
| 108 | + expect(parsed.features?.variables).toBe(true); |
| 109 | + expect(parsed.features?.webhooks).toBe(true); |
| 110 | + expect(parsed.features?.integrations).toBe(true); |
| 111 | + }); |
| 112 | + |
| 113 | + it("junior-dev.yaml should have pipelines disabled", () => { |
| 114 | + const filepath = path.join(builtinDir, "junior-dev.yaml"); |
| 115 | + const content = fs.readFileSync(filepath, "utf8"); |
| 116 | + const parsed = yaml.parse(content) as Preset; |
| 117 | + |
| 118 | + expect(parsed.features?.pipelines).toBe(false); |
| 119 | + expect(parsed.features?.variables).toBe(false); |
| 120 | + }); |
| 121 | + |
| 122 | + it("devops.yaml should have pipelines and variables enabled", () => { |
| 123 | + const filepath = path.join(builtinDir, "devops.yaml"); |
| 124 | + const content = fs.readFileSync(filepath, "utf8"); |
| 125 | + const parsed = yaml.parse(content) as Preset; |
| 126 | + |
| 127 | + expect(parsed.features?.pipelines).toBe(true); |
| 128 | + expect(parsed.features?.variables).toBe(true); |
| 129 | + expect(parsed.features?.webhooks).toBe(true); |
| 130 | + }); |
| 131 | + |
| 132 | + it("pm.yaml should have files disabled", () => { |
| 133 | + const filepath = path.join(builtinDir, "pm.yaml"); |
| 134 | + const content = fs.readFileSync(filepath, "utf8"); |
| 135 | + const parsed = yaml.parse(content) as Preset; |
| 136 | + |
| 137 | + expect(parsed.features?.files).toBe(false); |
| 138 | + expect(parsed.features?.pipelines).toBe(false); |
| 139 | + }); |
| 140 | + |
| 141 | + it("ci.yaml should have minimal features for automation", () => { |
| 142 | + const filepath = path.join(builtinDir, "ci.yaml"); |
| 143 | + const content = fs.readFileSync(filepath, "utf8"); |
| 144 | + const parsed = yaml.parse(content) as Preset; |
| 145 | + |
| 146 | + expect(parsed.features?.pipelines).toBe(true); |
| 147 | + expect(parsed.features?.files).toBe(true); |
| 148 | + expect(parsed.features?.variables).toBe(true); |
| 149 | + expect(parsed.features?.wiki).toBe(false); |
| 150 | + expect(parsed.features?.workitems).toBe(false); |
| 151 | + }); |
| 152 | + |
| 153 | + it("gitlab-com.yaml should have rate-limit friendly restrictions", () => { |
| 154 | + const filepath = path.join(builtinDir, "gitlab-com.yaml"); |
| 155 | + const content = fs.readFileSync(filepath, "utf8"); |
| 156 | + const parsed = yaml.parse(content) as Preset; |
| 157 | + |
| 158 | + expect(parsed.features?.webhooks).toBe(false); |
| 159 | + expect(parsed.features?.integrations).toBe(false); |
| 160 | + expect(parsed.denied_actions).toBeDefined(); |
| 161 | + expect(parsed.denied_actions?.length).toBeGreaterThan(0); |
| 162 | + }); |
| 163 | + }); |
| 164 | +}); |
0 commit comments