@@ -11,6 +11,7 @@ import { Global } from "../../src/global"
1111import { ProjectTable } from "../../src/project/project.sql"
1212import { Project } from "../../src/project/project"
1313import { SessionTable , MessageTable , PartTable , TodoTable , PermissionTable } from "../../src/session/session.sql"
14+ import { SessionShareTable } from "../../src/share/share.sql"
1415
1516// Test fixtures
1617const fixtures = {
@@ -240,4 +241,125 @@ describe("JSON to SQLite migration", () => {
240241 const projects = db . select ( ) . from ( ProjectTable ) . all ( )
241242 expect ( projects . length ) . toBe ( 1 ) // Still only 1 due to onConflictDoNothing
242243 } )
244+
245+ test ( "migrates todos" , async ( ) => {
246+ // First create the project and session
247+ await Bun . write (
248+ path . join ( storageDir , "project" , "proj_test123abc.json" ) ,
249+ JSON . stringify ( {
250+ id : "proj_test123abc" ,
251+ worktree : "/" ,
252+ time : { created : Date . now ( ) , updated : Date . now ( ) } ,
253+ sandboxes : [ ] ,
254+ } ) ,
255+ )
256+ await Bun . write (
257+ path . join ( storageDir , "session" , "proj_test123abc" , "ses_test456def.json" ) ,
258+ JSON . stringify ( { ...fixtures . session } ) ,
259+ )
260+
261+ // Create todo file (named by sessionID, contains array of todos)
262+ await Bun . write (
263+ path . join ( storageDir , "todo" , "ses_test456def.json" ) ,
264+ JSON . stringify ( [
265+ {
266+ id : "todo_1" ,
267+ content : "First todo" ,
268+ status : "pending" ,
269+ priority : "high" ,
270+ } ,
271+ {
272+ id : "todo_2" ,
273+ content : "Second todo" ,
274+ status : "completed" ,
275+ priority : "medium" ,
276+ } ,
277+ ] ) ,
278+ )
279+
280+ const stats = await JsonMigration . run ( sqlite )
281+
282+ expect ( stats ?. todos ) . toBe ( 2 )
283+
284+ const db = drizzle ( { client : sqlite } )
285+ const todos = db . select ( ) . from ( TodoTable ) . all ( )
286+ expect ( todos . length ) . toBe ( 2 )
287+ expect ( todos [ 0 ] . id ) . toBe ( "todo_1" )
288+ expect ( todos [ 0 ] . content ) . toBe ( "First todo" )
289+ expect ( todos [ 0 ] . status ) . toBe ( "pending" )
290+ expect ( todos [ 0 ] . priority ) . toBe ( "high" )
291+ expect ( todos [ 0 ] . position ) . toBe ( 0 )
292+ expect ( todos [ 1 ] . id ) . toBe ( "todo_2" )
293+ expect ( todos [ 1 ] . position ) . toBe ( 1 )
294+ } )
295+
296+ test ( "migrates permissions" , async ( ) => {
297+ // First create the project
298+ await Bun . write (
299+ path . join ( storageDir , "project" , "proj_test123abc.json" ) ,
300+ JSON . stringify ( {
301+ id : "proj_test123abc" ,
302+ worktree : "/" ,
303+ time : { created : Date . now ( ) , updated : Date . now ( ) } ,
304+ sandboxes : [ ] ,
305+ } ) ,
306+ )
307+
308+ // Create permission file (named by projectID, contains array of rules)
309+ const permissionData = [
310+ { permission : "file.read" , pattern : "/test/file1.ts" , action : "allow" as const } ,
311+ { permission : "file.write" , pattern : "/test/file2.ts" , action : "ask" as const } ,
312+ { permission : "command.run" , pattern : "npm install" , action : "deny" as const } ,
313+ ]
314+ await Bun . write ( path . join ( storageDir , "permission" , "proj_test123abc.json" ) , JSON . stringify ( permissionData ) )
315+
316+ const stats = await JsonMigration . run ( sqlite )
317+
318+ expect ( stats ?. permissions ) . toBe ( 1 )
319+
320+ const db = drizzle ( { client : sqlite } )
321+ const permissions = db . select ( ) . from ( PermissionTable ) . all ( )
322+ expect ( permissions . length ) . toBe ( 1 )
323+ expect ( permissions [ 0 ] . project_id ) . toBe ( "proj_test123abc" )
324+ expect ( permissions [ 0 ] . data ) . toEqual ( permissionData )
325+ } )
326+
327+ test ( "migrates session shares" , async ( ) => {
328+ // First create the project and session
329+ await Bun . write (
330+ path . join ( storageDir , "project" , "proj_test123abc.json" ) ,
331+ JSON . stringify ( {
332+ id : "proj_test123abc" ,
333+ worktree : "/" ,
334+ time : { created : Date . now ( ) , updated : Date . now ( ) } ,
335+ sandboxes : [ ] ,
336+ } ) ,
337+ )
338+ await Bun . write (
339+ path . join ( storageDir , "session" , "proj_test123abc" , "ses_test456def.json" ) ,
340+ JSON . stringify ( { ...fixtures . session } ) ,
341+ )
342+
343+ // Create session share file (named by sessionID)
344+ await Bun . write (
345+ path . join ( storageDir , "session_share" , "ses_test456def.json" ) ,
346+ JSON . stringify ( {
347+ id : "share_123" ,
348+ secret : "supersecretkey" ,
349+ url : "https://share.example.com/ses_test456def" ,
350+ } ) ,
351+ )
352+
353+ const stats = await JsonMigration . run ( sqlite )
354+
355+ expect ( stats ?. shares ) . toBe ( 1 )
356+
357+ const db = drizzle ( { client : sqlite } )
358+ const shares = db . select ( ) . from ( SessionShareTable ) . all ( )
359+ expect ( shares . length ) . toBe ( 1 )
360+ expect ( shares [ 0 ] . session_id ) . toBe ( "ses_test456def" )
361+ expect ( shares [ 0 ] . id ) . toBe ( "share_123" )
362+ expect ( shares [ 0 ] . secret ) . toBe ( "supersecretkey" )
363+ expect ( shares [ 0 ] . url ) . toBe ( "https://share.example.com/ses_test456def" )
364+ } )
243365} )
0 commit comments