|
| 1 | +import { describe, expect, test } from 'bun:test'; |
| 2 | +import { canonical, fingerprint } from './sync'; |
| 3 | + |
| 4 | +/** Minimal spec shape the fingerprint reads from. Extra `info` fields (version, license, contact.url) are included where a case needs them. */ |
| 5 | +function spec(overrides: { |
| 6 | + title?: string; |
| 7 | + description?: string; |
| 8 | + contact?: { name?: string; email?: string; url?: string }; |
| 9 | + version?: string; |
| 10 | + paths?: Record<string, unknown>; |
| 11 | + components?: Record<string, unknown>; |
| 12 | +}) { |
| 13 | + return { |
| 14 | + info: { |
| 15 | + title: overrides.title ?? 'Forecast API', |
| 16 | + description: |
| 17 | + overrides.description ?? 'Merges transits into one timeline.', |
| 18 | + contact: overrides.contact, |
| 19 | + version: overrides.version, |
| 20 | + }, |
| 21 | + paths: overrides.paths ?? { '/forecast': { get: {} } }, |
| 22 | + components: overrides.components ?? { schemas: {} }, |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +describe('fingerprint', () => { |
| 27 | + test('is stable when nothing changes', () => { |
| 28 | + expect(fingerprint(spec({}))).toBe(fingerprint(spec({}))); |
| 29 | + }); |
| 30 | + |
| 31 | + test('changes when only info.description changes and paths/components are identical', () => { |
| 32 | + const before = spec({ description: '14 domains, 209+ endpoints.' }); |
| 33 | + const after = spec({ description: '18 domains, 258+ endpoints.' }); |
| 34 | + expect(fingerprint(before)).not.toBe(fingerprint(after)); |
| 35 | + }); |
| 36 | + |
| 37 | + test('changes when only info.title changes', () => { |
| 38 | + const before = spec({ title: 'Forecast API' }); |
| 39 | + const after = spec({ title: 'Forecast API v2' }); |
| 40 | + expect(fingerprint(before)).not.toBe(fingerprint(after)); |
| 41 | + }); |
| 42 | + |
| 43 | + test('changes when only contact.name or contact.email changes', () => { |
| 44 | + const before = spec({ contact: { name: 'RoxyAPI Support' } }); |
| 45 | + const after = spec({ contact: { name: 'Someone Else' } }); |
| 46 | + expect(fingerprint(before)).not.toBe(fingerprint(after)); |
| 47 | + |
| 48 | + const beforeEmail = spec({ contact: { name: 'RoxyAPI Support' } }); |
| 49 | + const afterEmail = spec({ |
| 50 | + contact: { name: 'RoxyAPI Support', email: 'support@roxyapi.com' }, |
| 51 | + }); |
| 52 | + expect(fingerprint(beforeEmail)).not.toBe(fingerprint(afterEmail)); |
| 53 | + }); |
| 54 | + |
| 55 | + test('does not change for info fields the collection never surfaces (version, contact.url)', () => { |
| 56 | + const before = spec({ |
| 57 | + version: '1.0.0', |
| 58 | + contact: { url: 'https://roxyapi.com/contact' }, |
| 59 | + }); |
| 60 | + const after = spec({ |
| 61 | + version: '2.0.0', |
| 62 | + contact: { url: 'https://roxyapi.com/other' }, |
| 63 | + }); |
| 64 | + expect(fingerprint(before)).toBe(fingerprint(after)); |
| 65 | + }); |
| 66 | + |
| 67 | + test('changes when paths or components change', () => { |
| 68 | + const before = spec({ paths: { '/forecast': { get: {} } } }); |
| 69 | + const after = spec({ |
| 70 | + paths: { '/forecast': { get: {} }, '/forecast/digest': { get: {} } }, |
| 71 | + }); |
| 72 | + expect(fingerprint(before)).not.toBe(fingerprint(after)); |
| 73 | + |
| 74 | + const beforeSchema = spec({ components: { schemas: { Forecast: {} } } }); |
| 75 | + const afterSchema = spec({ |
| 76 | + components: { schemas: { Forecast: {}, Digest: {} } }, |
| 77 | + }); |
| 78 | + expect(fingerprint(beforeSchema)).not.toBe(fingerprint(afterSchema)); |
| 79 | + }); |
| 80 | + |
| 81 | + test('is insensitive to key order, matching the canonical semantic diff', () => { |
| 82 | + const a = { |
| 83 | + info: { title: 'X', description: 'd' }, |
| 84 | + paths: { '/a': {}, '/b': {} }, |
| 85 | + components: { schemas: {} }, |
| 86 | + }; |
| 87 | + const b = { |
| 88 | + info: { description: 'd', title: 'X' }, |
| 89 | + components: { schemas: {} }, |
| 90 | + paths: { '/b': {}, '/a': {} }, |
| 91 | + }; |
| 92 | + expect(fingerprint(a)).toBe(fingerprint(b)); |
| 93 | + }); |
| 94 | +}); |
| 95 | + |
| 96 | +describe('canonical', () => { |
| 97 | + test('sorts object keys recursively so JSON.stringify output is order independent', () => { |
| 98 | + const a = canonical({ b: 1, a: { d: 2, c: 3 } }); |
| 99 | + const b = canonical({ a: { c: 3, d: 2 }, b: 1 }); |
| 100 | + expect(JSON.stringify(a)).toBe(JSON.stringify(b)); |
| 101 | + }); |
| 102 | +}); |
0 commit comments