import type { JsonValue } from '@internal/utils/json'; import { describe, expectTypeOf, test } from 'vitest'; import { Collection } from '../src/collection'; import { createMockRuntime, getTestContext } from './helpers'; describe('Collection result types are simplified', () => { const runtime = createMockRuntime(); const context = getTestContext(); test('default Row is a plain object', () => { const users = new Collection({ runtime, context }, 'User', { namespaceId: 'public' }); type UserRow = Awaited>; expectTypeOf>().toEqualTypeOf<{ id: number; name: string; email: string; invitedById: number | null; address: JsonValue | null; }>(); }); test('select() produces a plain object', () => { const users = new Collection({ runtime, context }, 'User', { namespaceId: 'public' }); const selected = users.select('id', 'email'); type SelectedRow = Awaited>; expectTypeOf>().toEqualTypeOf<{ id: number; email: string; }>(); }); test('include() produces a plain object with nested relation', () => { const users = new Collection({ runtime, context }, 'User', { namespaceId: 'public' }); const withPosts = users.include('posts'); type WithPostsRow = Awaited>; expectTypeOf>().toEqualTypeOf<{ id: number; name: string; email: string; invitedById: number | null; address: JsonValue | null; posts: { id: number; title: string; userId: number; views: number; embedding: number[] | null; }[]; }>(); }); test('select().include() produces a plain object', () => { const users = new Collection({ runtime, context }, 'User', { namespaceId: 'public' }); const selected = users.select('name').include('posts'); type Row = Awaited>; expectTypeOf>().toEqualTypeOf<{ name: string; posts: { id: number; title: string; userId: number; views: number; embedding: number[] | null; }[]; }>(); }); test('include() with non-nullable to-one relation', () => { const posts = new Collection({ runtime, context }, 'Post', { namespaceId: 'public' }); const withAuthor = posts.include('author'); type Row = Awaited>; type AuthorField = NonNullable['author']; expectTypeOf().toEqualTypeOf<{ id: number; name: string; email: string; invitedById: number | null; address: JsonValue | null; }>(); }); test('chained include() produces a plain object', () => { const users = new Collection({ runtime, context }, 'User', { namespaceId: 'public' }); const withPostsAndInviter = users.include('posts').include('invitedBy'); type Row = Awaited>; expectTypeOf>().toEqualTypeOf<{ id: number; name: string; email: string; invitedById: number | null; address: JsonValue | null; posts: { id: number; title: string; userId: number; views: number; embedding: number[] | null; }[]; invitedBy: { id: number; name: string; email: string; invitedById: number | null; address: JsonValue | null; } | null; }>(); }); test('include() with count refinement', () => { const users = new Collection({ runtime, context }, 'User', { namespaceId: 'public' }); const withPostCount = users.include('posts', (posts) => posts.count()); type Row = Awaited>; expectTypeOf['posts']>().toEqualTypeOf(); }); });