|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { schemaFingerprint } from '@/lib/table/schema-fingerprint' |
| 3 | +import type { TableSchema } from '@/lib/table/types' |
| 4 | + |
| 5 | +function schema(columns: Array<{ id?: string; name: string }>): TableSchema { |
| 6 | + return { columns: columns.map((c) => ({ ...c, type: 'string' })) } as TableSchema |
| 7 | +} |
| 8 | + |
| 9 | +describe('schemaFingerprint', () => { |
| 10 | + it('is stable for the same column shape', () => { |
| 11 | + const a = schemaFingerprint(schema([{ id: 'col_1', name: 'email' }])) |
| 12 | + const b = schemaFingerprint(schema([{ id: 'col_1', name: 'email' }])) |
| 13 | + expect(a).toBe(b) |
| 14 | + expect(a).toMatch(/^[0-9a-f]{12}$/) |
| 15 | + }) |
| 16 | + |
| 17 | + it('changes on rename, add, and reorder', () => { |
| 18 | + const base = schemaFingerprint( |
| 19 | + schema([ |
| 20 | + { id: 'col_1', name: 'email' }, |
| 21 | + { id: 'col_2', name: 'age' }, |
| 22 | + ]) |
| 23 | + ) |
| 24 | + const renamed = schemaFingerprint( |
| 25 | + schema([ |
| 26 | + { id: 'col_1', name: 'contact' }, |
| 27 | + { id: 'col_2', name: 'age' }, |
| 28 | + ]) |
| 29 | + ) |
| 30 | + const added = schemaFingerprint( |
| 31 | + schema([ |
| 32 | + { id: 'col_1', name: 'email' }, |
| 33 | + { id: 'col_2', name: 'age' }, |
| 34 | + { id: 'col_3', name: 'city' }, |
| 35 | + ]) |
| 36 | + ) |
| 37 | + const reordered = schemaFingerprint( |
| 38 | + schema([ |
| 39 | + { id: 'col_2', name: 'age' }, |
| 40 | + { id: 'col_1', name: 'email' }, |
| 41 | + ]) |
| 42 | + ) |
| 43 | + expect(new Set([base, renamed, added, reordered]).size).toBe(4) |
| 44 | + }) |
| 45 | + |
| 46 | + it('keys legacy columns without ids by name (getColumnId fallback)', () => { |
| 47 | + const legacy = schemaFingerprint(schema([{ name: 'email' }])) |
| 48 | + const withId = schemaFingerprint(schema([{ id: 'col_1', name: 'email' }])) |
| 49 | + expect(legacy).not.toBe(withId) |
| 50 | + }) |
| 51 | + |
| 52 | + it('changes on a pure metadata.columnOrder reorder of the RAW schema', () => { |
| 53 | + // The user-visible order lives in metadata.columnOrder and is written by a |
| 54 | + // metadata-only update (no schema write, no rows_version bump) — the hash |
| 55 | + // is the ONLY signal such a reorder can move, so it must move. |
| 56 | + const raw = schema([ |
| 57 | + { id: 'col_1', name: 'email' }, |
| 58 | + { id: 'col_2', name: 'age' }, |
| 59 | + ]) |
| 60 | + const unordered = schemaFingerprint(raw, null) |
| 61 | + const reordered = schemaFingerprint(raw, { columnOrder: ['col_2', 'col_1'] }) |
| 62 | + expect(reordered).not.toBe(unordered) |
| 63 | + |
| 64 | + // Raw schema + metadata order must hash identically to an already |
| 65 | + // order-applied schema (getTableById/listTables output) without metadata — |
| 66 | + // otherwise the same table carries two hashes across call sites. |
| 67 | + const applied = schemaFingerprint( |
| 68 | + schema([ |
| 69 | + { id: 'col_2', name: 'age' }, |
| 70 | + { id: 'col_1', name: 'email' }, |
| 71 | + ]) |
| 72 | + ) |
| 73 | + expect(reordered).toBe(applied) |
| 74 | + }) |
| 75 | + |
| 76 | + it('matches the golden hash (pins the storage-key format across refactors)', () => { |
| 77 | + // Snapshot-cache storage keys embed this hash |
| 78 | + // (table-snapshots/{ws}/{tableId}/v{version}-{hash}.csv). Changing the |
| 79 | + // hashed shape orphans every cached CSV and emits a one-time '~table' |
| 80 | + // delta for every table in every live chat — if this assertion fails, |
| 81 | + // that blast radius is intentional and reviewed, or the change is wrong. |
| 82 | + const golden = schemaFingerprint( |
| 83 | + schema([ |
| 84 | + { id: 'col_1', name: 'email' }, |
| 85 | + { id: 'col_2', name: 'age' }, |
| 86 | + ]) |
| 87 | + ) |
| 88 | + expect(golden).toBe('f49aa06b1b7c') |
| 89 | + }) |
| 90 | +}) |
0 commit comments