import { describe, expect, it } from 'vitest'; import { FieldSchema, RowFieldsSchema, RowSchema, TableSchema, ViewSchema, } from '../../src/domain/schemas.js'; describe('FieldSchema', () => { it('valide un field minimal', () => { const r = FieldSchema.parse({ id: 1, name: 'nom', type: 'text' }); expect(r.primary).toBe(false); expect(r.options).toBeUndefined(); }); it('rejette name vide', () => { expect(() => FieldSchema.parse({ id: 1, name: '', type: 'text' })).toThrow(); }); it('accepte type custom (Baserow expose tout)', () => { const r = FieldSchema.parse({ id: 1, name: 'x', type: 'rollup' }); expect(r.type).toBe('rollup'); }); it('options accept Record', () => { const r = FieldSchema.parse({ id: 1, name: 'statut', type: 'single_select', options: { select_options: [{ id: 1 }] }, }); expect(r.options).toEqual({ select_options: [{ id: 1 }] }); }); }); describe('ViewSchema', () => { it('valide une view grid', () => { const r = ViewSchema.parse({ id: 1, name: 'Tous', type: 'grid', tableId: 5 }); expect(r.type).toBe('grid'); }); it('accepte un type custom', () => { const r = ViewSchema.parse({ id: 1, name: 'X', type: 'weird', tableId: 5 }); expect(r.type).toBe('weird'); }); it('rejette tableId negatif', () => { expect(() => ViewSchema.parse({ id: 1, name: 'X', type: 'grid', tableId: 0 })).toThrow(); }); }); describe('TableSchema', () => { it('valide une table minimale', () => { const r = TableSchema.parse({ id: 1, name: 'Personne', databaseId: 5 }); expect(r.orderIndex).toBe(0); }); it('rejette id <= 0', () => { expect(() => TableSchema.parse({ id: 0, name: 'x', databaseId: 1 })).toThrow(); }); }); describe('RowSchema', () => { it('valide une row avec fields opaques', () => { const r = RowSchema.parse({ id: 1, tableId: 5, fields: { nom: 'x', heures: 40 }, }); expect(r.fields.heures).toBe(40); }); it('id 0 est accepte (NEW row temp client-side)', () => { const r = RowSchema.parse({ id: 0, tableId: 1, fields: {} }); expect(r.id).toBe(0); }); }); describe('RowFieldsSchema', () => { it('accepte n importe quel record', () => { expect(RowFieldsSchema.parse({ a: 1, b: 'x', c: null })).toEqual({ a: 1, b: 'x', c: null }); }); it('rejette un non-objet', () => { expect(() => RowFieldsSchema.parse([1, 2])).toThrow(); expect(() => RowFieldsSchema.parse('foo')).toThrow(); }); it('accepte un objet vide (PATCH partiel possible)', () => { expect(RowFieldsSchema.parse({})).toEqual({}); }); it('accepte des valeurs nested arbitraires (link_row, select, formula result)', () => { const v = { link: [{ id: 1, value: 'X' }], select: { id: 5, value: 'actif', color: 'green' }, formula_result: 42.5, tags: ['a', 'b'], }; expect(RowFieldsSchema.parse(v)).toEqual(v); }); });