11import { describe , expectTypeOf , it } from "vitest"
2+ import { z } from "zod"
23import { createCollection } from "../src/index"
34import { localOnlyCollectionOptions } from "../src/local-only"
45import type { LocalOnlyCollectionUtils } from "../src/local-only"
56import type { Collection } from "../src/index"
7+ import type { Query } from "../src/query/builder"
68
79interface TestItem extends Record < string , unknown > {
810 id : number
@@ -33,7 +35,7 @@ describe(`LocalOnly Collection Types`, () => {
3335 expectTypeOf ( options ) . toHaveProperty ( `getKey` )
3436
3537 // Test that getKey returns the correct type
36- expectTypeOf ( options . getKey ) . toMatchTypeOf < ( item : TestItem ) => number > ( )
38+ expectTypeOf ( options . getKey ) . toExtend < ( item : TestItem ) => number > ( )
3739 } )
3840
3941 it ( `should be compatible with createCollection` , ( ) => {
@@ -56,7 +58,7 @@ describe(`LocalOnly Collection Types`, () => {
5658 > ( options )
5759
5860 // Test that the collection has the expected type
59- expectTypeOf ( collection ) . toMatchTypeOf <
61+ expectTypeOf ( collection ) . toExtend <
6062 Collection < TestItem , number , LocalOnlyCollectionUtils >
6163 > ( )
6264 } )
@@ -82,7 +84,7 @@ describe(`LocalOnly Collection Types`, () => {
8284 LocalOnlyCollectionUtils
8385 > ( options )
8486
85- expectTypeOf ( collection ) . toMatchTypeOf <
87+ expectTypeOf ( collection ) . toExtend <
8688 Collection < TestItem , number , LocalOnlyCollectionUtils >
8789 > ( )
8890 } )
@@ -106,7 +108,7 @@ describe(`LocalOnly Collection Types`, () => {
106108 LocalOnlyCollectionUtils
107109 > ( options )
108110
109- expectTypeOf ( collection ) . toMatchTypeOf <
111+ expectTypeOf ( collection ) . toExtend <
110112 Collection < TestItem , number , LocalOnlyCollectionUtils >
111113 > ( )
112114 } )
@@ -129,9 +131,130 @@ describe(`LocalOnly Collection Types`, () => {
129131 LocalOnlyCollectionUtils
130132 > ( options )
131133
132- expectTypeOf ( collection ) . toMatchTypeOf <
134+ expectTypeOf ( collection ) . toExtend <
133135 Collection < TestItem , string , LocalOnlyCollectionUtils >
134136 > ( )
135- expectTypeOf ( options . getKey ) . toMatchTypeOf < ( item : TestItem ) => string > ( )
137+ expectTypeOf ( options . getKey ) . toExtend < ( item : TestItem ) => string > ( )
138+ } )
139+
140+ it ( `should work with schema and infer correct types` , ( ) => {
141+ const testSchema = z . object ( {
142+ id : z . string ( ) ,
143+ entityId : z . string ( ) ,
144+ value : z . string ( ) ,
145+ } )
146+
147+ const config = {
148+ id : `test-with-schema` ,
149+ getKey : ( item : any ) => item . id ,
150+ schema : testSchema ,
151+ }
152+
153+ const options = localOnlyCollectionOptions ( config )
154+ const collection = createCollection ( options )
155+
156+ // Test that the collection has the correct inferred type from schema
157+ expectTypeOf ( collection ) . toExtend <
158+ Collection <
159+ {
160+ id : string
161+ entityId : string
162+ value : string
163+ } ,
164+ string ,
165+ LocalOnlyCollectionUtils
166+ >
167+ > ( )
168+ } )
169+
170+ it ( `should work with schema and query builder type inference (bug report reproduction)` , ( ) => {
171+ const testSchema = z . object ( {
172+ id : z . string ( ) ,
173+ entityId : z . string ( ) ,
174+ value : z . string ( ) ,
175+ createdAt : z . date ( ) ,
176+ } )
177+
178+ const config = {
179+ id : `test-with-schema-query` ,
180+ getKey : ( item : any ) => item . id ,
181+ schema : testSchema ,
182+ }
183+
184+ const options = localOnlyCollectionOptions ( config )
185+ const collection = createCollection ( options )
186+
187+ // This should work without type errors - the query builder should infer the correct type
188+ const query = ( q : InstanceType < typeof Query > ) =>
189+ q
190+ . from ( { bookmark : collection } )
191+ . orderBy ( ( { bookmark } ) => bookmark . createdAt , `desc` )
192+
193+ // Test that the collection has the correct inferred type from schema
194+ expectTypeOf ( collection ) . toExtend <
195+ Collection <
196+ {
197+ id : string
198+ entityId : string
199+ value : string
200+ createdAt : Date
201+ } ,
202+ string ,
203+ LocalOnlyCollectionUtils
204+ >
205+ > ( )
206+
207+ // Test that the query builder can access the createdAt property
208+ expectTypeOf ( query ) . toBeFunction ( )
209+ } )
210+
211+ it ( `should reproduce exact bug report scenario` , ( ) => {
212+ // This reproduces the exact scenario from the bug report
213+ const selectUrlSchema = z . object ( {
214+ id : z . string ( ) ,
215+ url : z . string ( ) ,
216+ title : z . string ( ) ,
217+ createdAt : z . date ( ) ,
218+ } )
219+
220+ const initialData = [
221+ {
222+ id : `1` ,
223+ url : `https://example.com` ,
224+ title : `Example` ,
225+ createdAt : new Date ( ) ,
226+ } ,
227+ ]
228+
229+ const bookmarkCollection = createCollection (
230+ localOnlyCollectionOptions ( {
231+ initialData,
232+ getKey : ( url : any ) => url . id ,
233+ schema : selectUrlSchema ,
234+ } )
235+ )
236+
237+ // This should work without type errors - the query builder should infer the correct type
238+ const query = ( q : InstanceType < typeof Query > ) =>
239+ q
240+ . from ( { bookmark : bookmarkCollection } )
241+ . orderBy ( ( { bookmark } ) => bookmark . createdAt , `desc` )
242+
243+ // Test that the collection has the correct inferred type from schema
244+ expectTypeOf ( bookmarkCollection ) . toExtend <
245+ Collection <
246+ {
247+ id : string
248+ url : string
249+ title : string
250+ createdAt : Date
251+ } ,
252+ string ,
253+ LocalOnlyCollectionUtils
254+ >
255+ > ( )
256+
257+ // Test that the query builder can access the createdAt property
258+ expectTypeOf ( query ) . toBeFunction ( )
136259 } )
137260} )
0 commit comments