import { expect, it } from 'vitest' import { diagnose, renderToSvg } from './index.js' import { DiagramError, normalize } from './normalize.js' import { FlatDiagramSchema, toJsonSchema } from './schema.js' const orders = { id: 'orders', columns: [ { name: 'id', type: 'uuid', primaryKey: true }, { name: 'user_id', type: 'uuid', foreignKey: true }, { name: '주문 금액', type: 'numeric(12, 2)' }, { name: 'created_at' }, ], } it('keeps table columns and connections in both input forms', () => { const edges = [{ from: 'api', to: 'orders' }] const nested = { groups: [{ id: 'db', children: [orders] }], nodes: [{ id: 'api' }], edges } const flat = { nodes: [{ id: 'db' }, { ...orders, parent: 'db' }, { id: 'api' }], edges } expect(normalize(nested)).toEqual(normalize(FlatDiagramSchema.parse(flat))) expect(normalize(nested).nodes.find(({ id }) => id === 'orders')?.columns).toEqual(orders.columns) expect(JSON.stringify(toJsonSchema('flat'))).toContain('"columns"') expect(JSON.stringify(toJsonSchema('input'))).toContain('"columns"') }) it.each([ { columns: [] }, { columns: [{ name: ' ' }] }, { columns: [{ name: 'first\nlast' }] }, { columns: [{ name: 'id', type: 42 }] }, ])('rejects invalid column lists (%j)', ({ columns }) => { expect(() => normalize({ nodes: [{ id: 'orders', columns }] })).toThrow(DiagramError) }) it('reports the duplicate column at its original path', () => { expect(() => normalize({ groups: [{ id: 'db', children: [{ ...orders, columns: [{ name: 'id' }, { name: 'id' }] }] }], }), ).toThrow( expect.objectContaining({ issue: expect.objectContaining({ code: 'duplicate-column', path: ['groups', 0, 'children', 0, 'columns', 1, 'name'], }), }), ) }) it('rejects columns on both declared and inferred groups', () => { expect(() => normalize({ groups: [orders] })).toThrow(/cannot have table columns/) expect(() => normalize({ nodes: [orders, { id: 'items', parent: 'orders' }] })).toThrow( /cannot have table columns/, ) }) it('draws table text in column order and routes edges between regular nodes and tables', async () => { const svg = await renderToSvg( { shape: 'card', nodes: [ { id: 'api' }, orders, { id: 'users', label: 'Users\npublic.users', columns: [ { name: 'id', type: 'CHARACTER VARYING(128)', primaryKey: true, foreignKey: true }, ], }, ], edges: [ { from: 'api', to: 'orders' }, { from: 'orders', to: 'users' }, ], }, { onLayout(ir, root) { expect(diagnose(ir, root).diagnostics).toEqual([]) }, }, ) expect(svg).toMatch( />orders<.*>id<.*>PK<.*>user_id<.*>FK<.*>주문 금액<.*>numeric\(12, 2\)<.*>created_atpublic.users<') expect(svg).toContain('>PK, FK<') expect(svg).toContain('>CHARACTER VARYING(128)<') expect(svg).toContain('data-edge-id="api->orders"') expect(svg).toContain('data-edge-id="orders->users"') })