11import { describe , it , expect , beforeEach , afterEach } from 'vitest'
22import { createDatabase } from 'db0'
3+ import type { Database } from 'db0'
34import { getConnector , checkUsersTableExists , hasAnyUsers } from '../src/runtime/server/utils/db'
45import { createUsersTable } from '../src/runtime/server/utils/create-users-table'
56import type { ModuleOptions } from '../src/types'
6- import fs from 'fs'
7+ import fs from 'node: fs'
78
89describe ( 'Utils: DB' , ( ) => {
9- let db : any
10+ let db : Database
1011 let testOptions : ModuleOptions
1112
1213 beforeEach ( async ( ) => {
@@ -26,11 +27,12 @@ describe('Utils: DB', () => {
2627 } )
2728
2829 afterEach ( async ( ) => {
29- try {
30+ try {
3031 fs . unlinkSync ( './_db-utils' )
3132 fs . unlinkSync ( './_db-utils-different' )
3233 fs . unlinkSync ( './_db-utils-has-users' )
33- } catch ( error ) {
34+ }
35+ catch {
3436 // Ignore errors during cleanup
3537 }
3638 } )
@@ -68,7 +70,7 @@ describe('Utils: DB', () => {
6870 it ( 'should return true when users table exists' , async ( ) => {
6971 // Create the users table first
7072 await createUsersTable ( 'users' , testOptions )
71-
73+
7274 const exists = await checkUsersTableExists ( testOptions )
7375 expect ( exists ) . toBe ( true )
7476 } )
@@ -89,7 +91,7 @@ describe('Utils: DB', () => {
8991
9092 // Create table in new database
9193 await createUsersTable ( 'users' , differentOptions )
92-
94+
9395 // Now table exists
9496 const existsAfter = await checkUsersTableExists ( differentOptions )
9597 expect ( existsAfter ) . toBe ( true )
@@ -100,29 +102,29 @@ describe('Utils: DB', () => {
100102 it ( 'should return false when no users exist' , async ( ) => {
101103 // Create table but don't add users
102104 await createUsersTable ( 'users' , testOptions )
103-
105+
104106 const hasUsers = await hasAnyUsers ( testOptions )
105107 expect ( hasUsers ) . toBe ( false )
106108 } )
107109
108110 it ( 'should return true when users exist' , async ( ) => {
109111 // Create table and add a user
110112 await createUsersTable ( 'users' , testOptions )
111-
113+
112114 // Insert a test user
113115 await db . sql `
114116 INSERT INTO users (email, name, password, created_at, updated_at)
115117 VALUES ('test@example.com', 'Test User', 'hashedpassword', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
116118 `
117-
119+
118120 const hasUsers = await hasAnyUsers ( testOptions )
119121 expect ( hasUsers ) . toBe ( true )
120122 } )
121123
122124 it ( 'should return true with multiple users' , async ( ) => {
123125 // Create table and add multiple users
124126 await createUsersTable ( 'users' , testOptions )
125-
127+
126128 // Insert multiple test users
127129 await db . sql `
128130 INSERT INTO users (email, name, password, created_at, updated_at)
@@ -132,7 +134,7 @@ describe('Utils: DB', () => {
132134 INSERT INTO users (email, name, password, created_at, updated_at)
133135 VALUES ('user2@example.com', 'User 2', 'pass2', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
134136 `
135-
137+
136138 const hasUsers = await hasAnyUsers ( testOptions )
137139 expect ( hasUsers ) . toBe ( true )
138140 } )
@@ -155,16 +157,16 @@ describe('Utils: DB', () => {
155157
156158 // Create table and add user in different database
157159 await createUsersTable ( 'users' , differentOptions )
158-
160+
159161 // Create a new database instance for the different options
160162 const connector = await import ( 'db0/connectors/better-sqlite3' )
161163 const differentDb = createDatabase ( connector . default ( differentOptions . connector ! . options ) )
162-
164+
163165 await differentDb . sql `
164166 INSERT INTO users (email, name, password, created_at, updated_at)
165167 VALUES ('test@example.com', 'Test User', 'hashedpassword', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
166168 `
167-
169+
168170 const hasUsers = await hasAnyUsers ( differentOptions )
169171 expect ( hasUsers ) . toBe ( true )
170172 } )
@@ -178,7 +180,7 @@ describe('Utils: DB', () => {
178180
179181 // Step 2: Create table
180182 await createUsersTable ( 'users' , testOptions )
181-
183+
182184 // Step 3: Check table exists
183185 const tableExistsAfter = await checkUsersTableExists ( testOptions )
184186 expect ( tableExistsAfter ) . toBe ( true )
@@ -192,7 +194,7 @@ describe('Utils: DB', () => {
192194 INSERT INTO users (email, name, password, created_at, updated_at)
193195 VALUES ('test@example.com', 'Test User', 'hashedpassword', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
194196 `
195-
197+
196198 // Step 6: Check user exists
197199 const hasUsersAfter = await hasAnyUsers ( testOptions )
198200 expect ( hasUsersAfter ) . toBe ( true )
@@ -201,13 +203,13 @@ describe('Utils: DB', () => {
201203 it ( 'should handle multiple database operations correctly' , async ( ) => {
202204 // Create table
203205 await createUsersTable ( 'users' , testOptions )
204-
206+
205207 // Verify table exists
206208 expect ( await checkUsersTableExists ( testOptions ) ) . toBe ( true )
207-
209+
208210 // Verify no users initially
209211 expect ( await hasAnyUsers ( testOptions ) ) . toBe ( false )
210-
212+
211213 // Add users
212214 await db . sql `
213215 INSERT INTO users (email, name, password, created_at, updated_at)
@@ -217,12 +219,12 @@ describe('Utils: DB', () => {
217219 INSERT INTO users (email, name, password, created_at, updated_at)
218220 VALUES ('user2@example.com', 'User 2', 'pass2', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
219221 `
220-
222+
221223 // Verify users exist
222224 expect ( await hasAnyUsers ( testOptions ) ) . toBe ( true )
223-
225+
224226 // Verify table still exists
225227 expect ( await checkUsersTableExists ( testOptions ) ) . toBe ( true )
226228 } )
227229 } )
228- } )
230+ } )
0 commit comments