|
| 1 | +import { describe, it } from 'node:test' |
| 2 | +import * as assert from 'node:assert/strict' |
| 3 | +import { PatchManifestSchema, PatchRecordSchema } from './manifest-schema.js' |
| 4 | + |
| 5 | +describe('PatchManifestSchema', () => { |
| 6 | + it('should validate a well-formed manifest', () => { |
| 7 | + const manifest = { |
| 8 | + patches: { |
| 9 | + 'npm:simplehttpserver@0.0.6': { |
| 10 | + uuid: '550e8400-e29b-41d4-a716-446655440000', |
| 11 | + exportedAt: '2024-01-01T00:00:00Z', |
| 12 | + files: { |
| 13 | + 'node_modules/simplehttpserver/index.js': { |
| 14 | + beforeHash: 'abc123', |
| 15 | + afterHash: 'def456', |
| 16 | + }, |
| 17 | + }, |
| 18 | + vulnerabilities: { |
| 19 | + 'GHSA-jrhj-2j3q-xf3v': { |
| 20 | + cves: ['CVE-2024-0001'], |
| 21 | + summary: 'Path traversal vulnerability', |
| 22 | + severity: 'high', |
| 23 | + description: 'Allows reading arbitrary files', |
| 24 | + }, |
| 25 | + }, |
| 26 | + description: 'Fix path traversal', |
| 27 | + license: 'MIT', |
| 28 | + tier: 'free', |
| 29 | + }, |
| 30 | + }, |
| 31 | + } |
| 32 | + |
| 33 | + const result = PatchManifestSchema.safeParse(manifest) |
| 34 | + assert.ok(result.success, 'Valid manifest should parse successfully') |
| 35 | + assert.equal( |
| 36 | + Object.keys(result.data.patches).length, |
| 37 | + 1, |
| 38 | + 'Should have one patch entry', |
| 39 | + ) |
| 40 | + }) |
| 41 | + |
| 42 | + it('should validate a manifest with multiple patches', () => { |
| 43 | + const manifest = { |
| 44 | + patches: { |
| 45 | + 'npm:pkg-a@1.0.0': { |
| 46 | + uuid: '550e8400-e29b-41d4-a716-446655440001', |
| 47 | + exportedAt: '2024-01-01T00:00:00Z', |
| 48 | + files: { |
| 49 | + 'node_modules/pkg-a/lib/index.js': { |
| 50 | + beforeHash: 'aaa', |
| 51 | + afterHash: 'bbb', |
| 52 | + }, |
| 53 | + }, |
| 54 | + vulnerabilities: {}, |
| 55 | + description: 'Patch A', |
| 56 | + license: 'MIT', |
| 57 | + tier: 'free', |
| 58 | + }, |
| 59 | + 'npm:pkg-b@2.0.0': { |
| 60 | + uuid: '550e8400-e29b-41d4-a716-446655440002', |
| 61 | + exportedAt: '2024-02-01T00:00:00Z', |
| 62 | + files: { |
| 63 | + 'node_modules/pkg-b/src/main.js': { |
| 64 | + beforeHash: 'ccc', |
| 65 | + afterHash: 'ddd', |
| 66 | + }, |
| 67 | + }, |
| 68 | + vulnerabilities: { |
| 69 | + 'GHSA-xxxx-yyyy-zzzz': { |
| 70 | + cves: [], |
| 71 | + summary: 'Some vuln', |
| 72 | + severity: 'medium', |
| 73 | + description: 'A medium severity vulnerability', |
| 74 | + }, |
| 75 | + }, |
| 76 | + description: 'Patch B', |
| 77 | + license: 'Apache-2.0', |
| 78 | + tier: 'paid', |
| 79 | + }, |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + const result = PatchManifestSchema.safeParse(manifest) |
| 84 | + assert.ok(result.success, 'Multi-patch manifest should parse successfully') |
| 85 | + assert.equal(Object.keys(result.data.patches).length, 2) |
| 86 | + }) |
| 87 | + |
| 88 | + it('should validate an empty manifest', () => { |
| 89 | + const manifest = { patches: {} } |
| 90 | + const result = PatchManifestSchema.safeParse(manifest) |
| 91 | + assert.ok(result.success, 'Empty patches should be valid') |
| 92 | + }) |
| 93 | + |
| 94 | + it('should reject a manifest missing the patches field', () => { |
| 95 | + const result = PatchManifestSchema.safeParse({}) |
| 96 | + assert.ok(!result.success, 'Missing patches should fail') |
| 97 | + }) |
| 98 | + |
| 99 | + it('should reject a manifest with invalid patch record', () => { |
| 100 | + const manifest = { |
| 101 | + patches: { |
| 102 | + 'npm:bad@1.0.0': { |
| 103 | + // missing uuid, exportedAt, files, vulnerabilities, description, license, tier |
| 104 | + }, |
| 105 | + }, |
| 106 | + } |
| 107 | + const result = PatchManifestSchema.safeParse(manifest) |
| 108 | + assert.ok(!result.success, 'Invalid patch record should fail') |
| 109 | + }) |
| 110 | + |
| 111 | + it('should reject a patch with invalid uuid', () => { |
| 112 | + const record = { |
| 113 | + uuid: 'not-a-valid-uuid', |
| 114 | + exportedAt: '2024-01-01T00:00:00Z', |
| 115 | + files: {}, |
| 116 | + vulnerabilities: {}, |
| 117 | + description: 'Test', |
| 118 | + license: 'MIT', |
| 119 | + tier: 'free', |
| 120 | + } |
| 121 | + const result = PatchRecordSchema.safeParse(record) |
| 122 | + assert.ok(!result.success, 'Invalid UUID should fail') |
| 123 | + }) |
| 124 | + |
| 125 | + it('should reject non-object input', () => { |
| 126 | + assert.ok(!PatchManifestSchema.safeParse(null).success) |
| 127 | + assert.ok(!PatchManifestSchema.safeParse('string').success) |
| 128 | + assert.ok(!PatchManifestSchema.safeParse(42).success) |
| 129 | + assert.ok(!PatchManifestSchema.safeParse([]).success) |
| 130 | + }) |
| 131 | +}) |
0 commit comments