@fragno-dev/test 0.0.0-canary-20251030115355

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,641 @@
1
+ import { describe, expect, it, afterEach } from "vitest";
2
+ import { column, idColumn, schema } from "@fragno-dev/db/schema";
3
+ import { defineFragmentWithDatabase } from "@fragno-dev/db/fragment";
4
+ import { createDatabaseFragmentForTest } from "./index";
5
+ import { unlinkSync, existsSync } from "node:fs";
6
+ import { defineRoute, defineRoutes } from "@fragno-dev/core";
7
+ import { z } from "zod";
8
+
9
+ // Test schema with multiple versions
10
+ const testSchema = schema((s) => {
11
+ return s
12
+ .addTable("users", (t) => {
13
+ return t
14
+ .addColumn("id", idColumn())
15
+ .addColumn("name", column("string"))
16
+ .addColumn("email", column("string"))
17
+ .createIndex("idx_users_all", ["id"]); // Index for querying
18
+ })
19
+ .alterTable("users", (t) => {
20
+ return t.addColumn("age", column("integer").nullable());
21
+ });
22
+ });
23
+
24
+ // Test fragment definition
25
+ const testFragmentDef = defineFragmentWithDatabase<{}>("test-fragment")
26
+ .withDatabase(testSchema)
27
+ .withServices(({ orm }) => {
28
+ return {
29
+ createUser: async (data: { name: string; email: string; age?: number | null }) => {
30
+ const id = await orm.create("users", data);
31
+ return { ...data, id: id.valueOf() };
32
+ },
33
+ getUsers: async () => {
34
+ const users = await orm.find("users", (b) =>
35
+ b.whereIndex("idx_users_all", (eb) => eb("id", "!=", "")),
36
+ );
37
+ return users.map((u) => ({ ...u, id: u.id.valueOf() }));
38
+ },
39
+ };
40
+ });
41
+
42
+ describe("createDatabaseFragmentForTest", () => {
43
+ describe("databasePath option", () => {
44
+ const testDbPath = "./test-fragno.pglite";
45
+
46
+ afterEach(() => {
47
+ // Clean up test database files
48
+ if (existsSync(testDbPath)) {
49
+ try {
50
+ unlinkSync(testDbPath);
51
+ } catch {
52
+ // Ignore cleanup errors
53
+ }
54
+ }
55
+ });
56
+
57
+ it("should use in-memory database by default", async () => {
58
+ const { fragment } = await createDatabaseFragmentForTest(testFragmentDef, {
59
+ adapter: { type: "kysely-sqlite" },
60
+ });
61
+
62
+ // Should be able to create and query users
63
+ const user = await fragment.services.createUser({
64
+ name: "Test User",
65
+ email: "test@example.com",
66
+ age: 25,
67
+ });
68
+
69
+ expect(user).toMatchObject({
70
+ id: expect.any(String),
71
+ name: "Test User",
72
+ email: "test@example.com",
73
+ age: 25,
74
+ });
75
+
76
+ const users = await fragment.services.getUsers();
77
+ expect(users).toHaveLength(1);
78
+ expect(users[0]).toMatchObject(user);
79
+ });
80
+
81
+ it("should create database at specified path", async () => {
82
+ const { fragment } = await createDatabaseFragmentForTest(testFragmentDef, {
83
+ adapter: { type: "kysely-sqlite", databasePath: testDbPath },
84
+ });
85
+
86
+ // Create a user
87
+ await fragment.services.createUser({
88
+ name: "Persisted User",
89
+ email: "persisted@example.com",
90
+ age: 30,
91
+ });
92
+
93
+ // Verify data exists in this instance
94
+ const users = await fragment.services.getUsers();
95
+ expect(users).toHaveLength(1);
96
+ expect(users[0]).toMatchObject({
97
+ name: "Persisted User",
98
+ email: "persisted@example.com",
99
+ age: 30,
100
+ });
101
+ });
102
+ });
103
+
104
+ describe("migrateToVersion option", () => {
105
+ it("should migrate to latest version by default", async () => {
106
+ const { fragment } = await createDatabaseFragmentForTest(testFragmentDef, {
107
+ adapter: { type: "kysely-sqlite" },
108
+ });
109
+
110
+ // Should have the 'age' column from version 2
111
+ const user = await fragment.services.createUser({
112
+ name: "Test User",
113
+ email: "test@example.com",
114
+ age: 25,
115
+ });
116
+
117
+ expect(user).toMatchObject({
118
+ id: expect.any(String),
119
+ name: "Test User",
120
+ email: "test@example.com",
121
+ age: 25,
122
+ });
123
+ });
124
+
125
+ it("should migrate to specific version when specified", async () => {
126
+ // Migrate to version 1 (before 'age' column was added)
127
+ const { test } = await createDatabaseFragmentForTest(testFragmentDef, {
128
+ adapter: { type: "kysely-sqlite" },
129
+ migrateToVersion: 1,
130
+ });
131
+
132
+ // Query the database directly to check schema
133
+ // In version 1, we should be able to insert without the age column
134
+ const tableName = "users_test-fragment-db";
135
+ await test.kysely
136
+ .insertInto(tableName)
137
+ .values({
138
+ id: "test-id-1",
139
+ name: "V1 User",
140
+ email: "v1@example.com",
141
+ })
142
+ .execute();
143
+
144
+ const result = await test.kysely.selectFrom(tableName).selectAll().execute();
145
+
146
+ expect(result).toHaveLength(1);
147
+ expect(result[0]).toMatchObject({
148
+ id: "test-id-1",
149
+ name: "V1 User",
150
+ email: "v1@example.com",
151
+ });
152
+ // In version 1, the age column should not exist
153
+ expect(result[0]).not.toHaveProperty("age");
154
+ });
155
+
156
+ it("should allow creating user with age when migrated to version 2", async () => {
157
+ // Explicitly migrate to version 2
158
+ const { fragment, test } = await createDatabaseFragmentForTest(testFragmentDef, {
159
+ adapter: { type: "kysely-sqlite" },
160
+ migrateToVersion: 2,
161
+ });
162
+
163
+ // Should be able to use age column
164
+ const user = await fragment.services.createUser({
165
+ name: "V2 User",
166
+ email: "v2@example.com",
167
+ age: 35,
168
+ });
169
+
170
+ expect(user).toMatchObject({
171
+ id: expect.any(String),
172
+ name: "V2 User",
173
+ email: "v2@example.com",
174
+ age: 35,
175
+ });
176
+
177
+ const tableName = "users_test-fragment-db";
178
+ const result = await test.kysely.selectFrom(tableName).selectAll().execute();
179
+
180
+ expect(result).toHaveLength(1);
181
+ expect(result[0]).toMatchObject({
182
+ name: "V2 User",
183
+ email: "v2@example.com",
184
+ age: 35,
185
+ });
186
+ });
187
+ });
188
+
189
+ describe("combined options", () => {
190
+ const testDbPath = "./test-combined.pglite";
191
+
192
+ afterEach(() => {
193
+ if (existsSync(testDbPath)) {
194
+ try {
195
+ unlinkSync(testDbPath);
196
+ } catch {
197
+ // Ignore cleanup errors
198
+ }
199
+ }
200
+ });
201
+
202
+ it("should work with both databasePath and migrateToVersion", async () => {
203
+ const { fragment } = await createDatabaseFragmentForTest(testFragmentDef, {
204
+ adapter: { type: "kysely-sqlite", databasePath: testDbPath },
205
+ migrateToVersion: 2,
206
+ });
207
+
208
+ // Create user at version 2 (with age support)
209
+ const user = await fragment.services.createUser({
210
+ name: "Combined Test",
211
+ email: "combined@example.com",
212
+ age: 40,
213
+ });
214
+
215
+ expect(user).toMatchObject({
216
+ id: expect.any(String),
217
+ name: "Combined Test",
218
+ email: "combined@example.com",
219
+ age: 40,
220
+ });
221
+
222
+ const users = await fragment.services.getUsers();
223
+ expect(users).toHaveLength(1);
224
+ expect(users[0]).toMatchObject({
225
+ name: "Combined Test",
226
+ email: "combined@example.com",
227
+ age: 40,
228
+ });
229
+ });
230
+ });
231
+
232
+ describe("fragment initialization", () => {
233
+ it("should provide kysely instance", async () => {
234
+ const { test } = await createDatabaseFragmentForTest(testFragmentDef, {
235
+ adapter: { type: "kysely-sqlite" },
236
+ });
237
+
238
+ expect(test.kysely).toBeDefined();
239
+ expect(typeof test.kysely.selectFrom).toBe("function");
240
+ });
241
+
242
+ it("should provide adapter instance", async () => {
243
+ const { test } = await createDatabaseFragmentForTest(testFragmentDef, {
244
+ adapter: { type: "kysely-sqlite" },
245
+ });
246
+
247
+ expect(test.adapter).toBeDefined();
248
+ expect(typeof test.adapter.createMigrationEngine).toBe("function");
249
+ });
250
+
251
+ it("should have all standard fragment test properties", async () => {
252
+ const { fragment } = await createDatabaseFragmentForTest(testFragmentDef, {
253
+ adapter: { type: "kysely-sqlite" },
254
+ });
255
+
256
+ expect(fragment.services).toBeDefined();
257
+ expect(fragment.initRoutes).toBeDefined();
258
+ expect(fragment.handler).toBeDefined();
259
+ });
260
+
261
+ it("should throw error for non-database fragment", async () => {
262
+ // Create a fragment without database
263
+ const nonDbFragment = {
264
+ definition: {
265
+ name: "non-db-fragment",
266
+ additionalContext: {},
267
+ },
268
+ $requiredOptions: {},
269
+ };
270
+
271
+ await expect(
272
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
273
+ createDatabaseFragmentForTest(nonDbFragment as any, {
274
+ adapter: { type: "kysely-sqlite" },
275
+ }),
276
+ ).rejects.toThrow("Fragment 'non-db-fragment' does not have a database schema");
277
+ });
278
+ });
279
+
280
+ describe("route handling with defineRoutes", () => {
281
+ it("should handle route factory with multiple routes", async () => {
282
+ const { fragment } = await createDatabaseFragmentForTest(testFragmentDef, {
283
+ adapter: { type: "kysely-sqlite" },
284
+ });
285
+
286
+ type Config = {};
287
+ type Deps = {};
288
+ type Services = {
289
+ createUser: (data: { name: string; email: string; age?: number | null }) => Promise<{
290
+ name: string;
291
+ email: string;
292
+ age?: number | null;
293
+ id: string;
294
+ }>;
295
+ getUsers: () => Promise<{ name: string; email: string; age: number | null; id: string }[]>;
296
+ };
297
+
298
+ const routeFactory = defineRoutes<Config, Deps, Services>().create(({ services }) => [
299
+ defineRoute({
300
+ method: "POST",
301
+ path: "/users",
302
+ inputSchema: z.object({
303
+ name: z.string(),
304
+ email: z.string(),
305
+ age: z.number().nullable().optional(),
306
+ }),
307
+ outputSchema: z.object({
308
+ id: z.string(),
309
+ name: z.string(),
310
+ email: z.string(),
311
+ age: z.number().nullable().optional(),
312
+ }),
313
+ handler: async ({ input }, { json }) => {
314
+ if (input) {
315
+ const data = await input.valid();
316
+ const user = await services.createUser(data);
317
+ return json(user);
318
+ }
319
+ return json({ id: "", name: "", email: "", age: null });
320
+ },
321
+ }),
322
+ defineRoute({
323
+ method: "GET",
324
+ path: "/users",
325
+ outputSchema: z.array(
326
+ z.object({
327
+ id: z.string(),
328
+ name: z.string(),
329
+ email: z.string(),
330
+ age: z.number().nullable(),
331
+ }),
332
+ ),
333
+ handler: async (_ctx, { json }) => {
334
+ const users = await services.getUsers();
335
+ return json(users);
336
+ },
337
+ }),
338
+ ]);
339
+
340
+ const routes = [routeFactory] as const;
341
+ const [createUserRoute, getUsersRoute] = fragment.initRoutes(routes);
342
+
343
+ // Test creating a user
344
+ const createResponse = await fragment.handler(createUserRoute, {
345
+ body: { name: "John Doe", email: "john@example.com", age: 30 },
346
+ });
347
+
348
+ expect(createResponse.type).toBe("json");
349
+ if (createResponse.type === "json") {
350
+ expect(createResponse.data).toMatchObject({
351
+ id: expect.any(String),
352
+ name: "John Doe",
353
+ email: "john@example.com",
354
+ age: 30,
355
+ });
356
+ }
357
+
358
+ // Test getting users
359
+ const getUsersResponse = await fragment.handler(getUsersRoute);
360
+
361
+ expect(getUsersResponse.type).toBe("json");
362
+ if (getUsersResponse.type === "json") {
363
+ expect(getUsersResponse.data).toHaveLength(1);
364
+ expect(getUsersResponse.data[0]).toMatchObject({
365
+ id: expect.any(String),
366
+ name: "John Doe",
367
+ email: "john@example.com",
368
+ age: 30,
369
+ });
370
+ }
371
+ });
372
+ });
373
+
374
+ describe("resetDatabase", () => {
375
+ const adapters = [
376
+ { name: "Kysely SQLite", adapter: { type: "kysely-sqlite" as const } },
377
+ { name: "Kysely PGLite", adapter: { type: "kysely-pglite" as const } },
378
+ { name: "Drizzle PGLite", adapter: { type: "drizzle-pglite" as const } },
379
+ ];
380
+
381
+ for (const { name, adapter } of adapters) {
382
+ describe(name, () => {
383
+ it("should clear all data and recreate a fresh database", async () => {
384
+ // Don't destructure so we can access the updated fragment through getters after reset
385
+ const result = await createDatabaseFragmentForTest(testFragmentDef, {
386
+ adapter,
387
+ });
388
+
389
+ // Create some users
390
+ await result.services.createUser({
391
+ name: "User 1",
392
+ email: "user1@example.com",
393
+ age: 25,
394
+ });
395
+ await result.services.createUser({
396
+ name: "User 2",
397
+ email: "user2@example.com",
398
+ age: 30,
399
+ });
400
+
401
+ // Verify users exist
402
+ let users = await result.services.getUsers();
403
+ expect(users).toHaveLength(2);
404
+
405
+ // Reset the database
406
+ await result.test.resetDatabase();
407
+
408
+ // Verify database is empty (accessing through result to get updated fragment)
409
+ users = await result.services.getUsers();
410
+ expect(users).toHaveLength(0);
411
+
412
+ // Verify we can still create new users after reset
413
+ const newUser = await result.services.createUser({
414
+ name: "User After Reset",
415
+ email: "after@example.com",
416
+ age: 35,
417
+ });
418
+
419
+ expect(newUser).toMatchObject({
420
+ id: expect.any(String),
421
+ name: "User After Reset",
422
+ email: "after@example.com",
423
+ age: 35,
424
+ });
425
+
426
+ users = await result.services.getUsers();
427
+ expect(users).toHaveLength(1);
428
+ expect(users[0]).toMatchObject(newUser);
429
+
430
+ // Cleanup
431
+ await result.test.cleanup();
432
+ }, 10000);
433
+ });
434
+ }
435
+ });
436
+
437
+ describe("db property access", () => {
438
+ const adapters = [
439
+ { name: "Kysely SQLite", adapter: { type: "kysely-sqlite" as const } },
440
+ { name: "Kysely PGLite", adapter: { type: "kysely-pglite" as const } },
441
+ { name: "Drizzle PGLite", adapter: { type: "drizzle-pglite" as const } },
442
+ ];
443
+
444
+ for (const { name, adapter } of adapters) {
445
+ describe(name, () => {
446
+ it("should expose db property for direct ORM queries", async () => {
447
+ const { test } = await createDatabaseFragmentForTest(testFragmentDef, {
448
+ adapter,
449
+ });
450
+
451
+ // Test creating a record directly using test.db
452
+ const userId = await test.db.create("users", {
453
+ name: "Direct DB User",
454
+ email: "direct@example.com",
455
+ age: 28,
456
+ });
457
+
458
+ expect(userId).toBeDefined();
459
+ expect(typeof userId.valueOf()).toBe("string");
460
+
461
+ // Test finding records using test.db
462
+ const users = await test.db.find("users", (b) =>
463
+ b.whereIndex("idx_users_all", (eb) => eb("id", "=", userId)),
464
+ );
465
+
466
+ expect(users).toHaveLength(1);
467
+ expect(users[0]).toMatchObject({
468
+ id: userId,
469
+ name: "Direct DB User",
470
+ email: "direct@example.com",
471
+ age: 28,
472
+ });
473
+
474
+ // Test findFirst using test.db
475
+ const user = await test.db.findFirst("users", (b) =>
476
+ b.whereIndex("idx_users_all", (eb) => eb("id", "=", userId)),
477
+ );
478
+
479
+ expect(user).toMatchObject({
480
+ id: userId,
481
+ name: "Direct DB User",
482
+ email: "direct@example.com",
483
+ age: 28,
484
+ });
485
+
486
+ // Cleanup
487
+ await test.cleanup();
488
+ }, 10000);
489
+
490
+ it("should maintain db property after resetDatabase", async () => {
491
+ const result = await createDatabaseFragmentForTest(testFragmentDef, {
492
+ adapter,
493
+ });
494
+
495
+ // Create initial data using test.db
496
+ await result.test.db.create("users", {
497
+ name: "Before Reset",
498
+ email: "before@example.com",
499
+ age: 25,
500
+ });
501
+
502
+ // Verify db works before reset
503
+ expect(result.test.db).toBeDefined();
504
+ expect(typeof result.test.db.create).toBe("function");
505
+
506
+ // Verify data exists
507
+ let users = await result.test.db.find("users");
508
+ expect(users).toHaveLength(1);
509
+
510
+ // Reset database
511
+ await result.test.resetDatabase();
512
+
513
+ // Verify database was actually reset (no data)
514
+ users = await result.test.db.find("users");
515
+ expect(users).toHaveLength(0);
516
+
517
+ // Verify we can still use the ORM after reset
518
+ const newUserId = await result.test.db.create("users", {
519
+ name: "After Reset",
520
+ email: "after@example.com",
521
+ age: 30,
522
+ });
523
+
524
+ expect(newUserId).toBeDefined();
525
+
526
+ const newUsers = await result.test.db.find("users");
527
+ expect(newUsers).toHaveLength(1);
528
+ expect(newUsers[0]).toMatchObject({
529
+ name: "After Reset",
530
+ email: "after@example.com",
531
+ age: 30,
532
+ });
533
+
534
+ // Cleanup
535
+ await result.test.cleanup();
536
+ }, 10000);
537
+ });
538
+ }
539
+ });
540
+
541
+ describe("multiple adapters with auth-like schema", () => {
542
+ // Simplified auth schema for testing
543
+ const authSchema = schema((s) => {
544
+ return s
545
+ .addTable("user", (t) => {
546
+ return t
547
+ .addColumn("id", idColumn())
548
+ .addColumn("email", column("string"))
549
+ .addColumn("passwordHash", column("string"))
550
+ .createIndex("idx_user_email", ["email"]);
551
+ })
552
+ .addTable("session", (t) => {
553
+ return t
554
+ .addColumn("id", idColumn())
555
+ .addColumn("userId", column("string"))
556
+ .addColumn("expiresAt", column("timestamp"))
557
+ .createIndex("idx_session_user", ["userId"]);
558
+ });
559
+ });
560
+
561
+ const authFragmentDef = defineFragmentWithDatabase<{}>("auth-test")
562
+ .withDatabase(authSchema)
563
+ .withServices(({ orm }) => {
564
+ return {
565
+ createUser: async (email: string, passwordHash: string) => {
566
+ const id = await orm.create("user", { email, passwordHash });
567
+ return { id: id.valueOf(), email, passwordHash };
568
+ },
569
+ createSession: async (userId: string) => {
570
+ const expiresAt = new Date();
571
+ expiresAt.setDate(expiresAt.getDate() + 30);
572
+ const id = await orm.create("session", { userId, expiresAt });
573
+ return { id: id.valueOf(), userId, expiresAt };
574
+ },
575
+ getUserByEmail: async (email: string) => {
576
+ const user = await orm.findFirst("user", (b) =>
577
+ b.whereIndex("idx_user_email", (eb) => eb("email", "=", email)),
578
+ );
579
+ if (!user) {
580
+ return null;
581
+ }
582
+ return { id: user.id.valueOf(), email: user.email, passwordHash: user.passwordHash };
583
+ },
584
+ };
585
+ });
586
+
587
+ const adapters = [
588
+ { name: "Kysely SQLite", adapter: { type: "kysely-sqlite" as const } },
589
+ { name: "Kysely PGLite", adapter: { type: "kysely-pglite" as const } },
590
+ { name: "Drizzle PGLite", adapter: { type: "drizzle-pglite" as const } },
591
+ ];
592
+
593
+ for (const { name, adapter } of adapters) {
594
+ describe(name, () => {
595
+ it("should create user and session", async () => {
596
+ const { fragment, test } = await createDatabaseFragmentForTest(authFragmentDef, {
597
+ adapter,
598
+ });
599
+
600
+ // Create a user
601
+ const user = await fragment.services.createUser("test@test.com", "hashed-password");
602
+ expect(user).toMatchObject({
603
+ id: expect.any(String),
604
+ email: "test@test.com",
605
+ passwordHash: "hashed-password",
606
+ });
607
+
608
+ // Create a session for the user
609
+ const session = await fragment.services.createSession(user.id);
610
+ expect(session).toMatchObject({
611
+ id: expect.any(String),
612
+ userId: user.id,
613
+ expiresAt: expect.any(Date),
614
+ });
615
+
616
+ // Find user by email
617
+ const foundUser = await fragment.services.getUserByEmail("test@test.com");
618
+ expect(foundUser).toMatchObject({
619
+ id: user.id,
620
+ email: "test@test.com",
621
+ passwordHash: "hashed-password",
622
+ });
623
+
624
+ // Cleanup
625
+ await test.cleanup();
626
+ }, 10000);
627
+
628
+ it("should return null when user not found", async () => {
629
+ const { fragment, test } = await createDatabaseFragmentForTest(authFragmentDef, {
630
+ adapter,
631
+ });
632
+
633
+ const notFound = await fragment.services.getUserByEmail("nonexistent@test.com");
634
+ expect(notFound).toBeNull();
635
+
636
+ await test.cleanup();
637
+ }, 10000);
638
+ });
639
+ }
640
+ });
641
+ });