import { assertType, describe, test } from 'vitest'; import type { QueryFields } from '../src/types/fields.js'; import type { CollectionA, CollectionC, TestSchema } from './schema.js'; describe('Test QueryFields', () => { type CollectionAFields = QueryFields; type CollectionCFields = QueryFields; test('error situations', () => { // all these should fail // @ts-expect-error assertType(undefined); // @ts-expect-error assertType({}); // @ts-expect-error assertType(['not-a-field']); // @ts-expect-error assertType([{ m2o: ['not-a-field'] }]); // @ts-expect-error assertType([{ m2m: [{ not_a_field: ['not-a-field'] }] }]); }); test('first level fields', () => { assertType([]); assertType(['*']); assertType(['id', 'string_field']); assertType(['*', { m2o: ['*'] }]); }); test('function fields', () => { // array functions assertType(['count(m2m)', 'count(o2m)', 'count(m2a)']); assertType(['*', { m2o: ['count(json_field)', 'count(csv_field)'] }]); // date functions assertType([ 'hour(dt_field)', 'minute(dt_field)', 'second(dt_field)', 'day(dt_field)', 'week(dt_field)', 'weekday(dt_field)', 'month(dt_field)', 'year(dt_field)', ]); }); test('nested fields', () => { assertType([ { m2a: ['collection', { item: { collection_b: ['id', 'json_field'], collection_c: ['*'] } }], m2m: [{ collection_b_id: ['*'] }], m2o: ['id', 'csv_field'], }, ]); }); });