11import type { FileSystem } from "@opencode/core/filesystem"
22import { describe , expect } from "bun:test"
3+ import { $ } from "bun"
4+ import { eq } from "drizzle-orm"
35import path from "path"
46import { chmod , mkdir , readdir , rm } from "fs/promises"
57import { Cause , Context , Deferred , Duration , Effect , Exit , Fiber , Layer , LayerMap , Queue } from "effect"
@@ -14,6 +16,7 @@ import { Location } from "@opencode/core/location"
1416import { LocationServiceMap } from "@opencode/core/location-service-map"
1517import type { LocationServices } from "@opencode/core/location-services"
1618import { Project } from "@opencode/core/project"
19+ import { ProjectTable } from "@opencode/core/project/sql"
1720import { AbsolutePath } from "@opencode/core/schema"
1821import { Session } from "@opencode/core/session"
1922import { SessionEvent } from "@opencode/core/session/event"
@@ -87,7 +90,7 @@ const itWithUnavailableDestination = testEffect(
8790 ) ,
8891)
8992const itWithExecution = testEffect (
90- AppNodeBuilder . build ( LayerNode . group ( [ Session . node , SessionExecution . node ] ) , [
93+ AppNodeBuilder . build ( LayerNode . group ( [ Session . node , SessionExecution . node , Database . node ] ) , [
9194 Global . node . replace ( tempGlobalLayer ) ,
9295 offlineModels ,
9396 ] ) ,
@@ -144,6 +147,50 @@ const sourceProbe = (options: { execution?: boolean } = {}) =>
144147 } )
145148
146149describe ( "Session.move" , ( ) => {
150+ itWithExecution . live (
151+ "does not repoint global when moving from a missing empty repository" ,
152+ ( ) =>
153+ Effect . gen ( function * ( ) {
154+ const tmp = yield * tmpdirScoped ( )
155+ const source = AbsolutePath . make ( path . join ( tmp . path , "source" ) )
156+ const destination = AbsolutePath . make ( path . join ( tmp . path , "destination" ) )
157+ yield * Effect . promise ( async ( ) => {
158+ await Promise . all ( [ mkdir ( source ) , mkdir ( destination ) ] )
159+ await $ `git init -q` . cwd ( source )
160+ await $ `git init -q` . cwd ( destination )
161+ } )
162+ const sessions = yield * Session . Service
163+ const execution = yield * SessionExecution . Service
164+ const db = ( yield * Database . Service ) . db
165+ const created = yield * sessions . create ( { location : Location . Ref . make ( { directory : source } ) } )
166+ const remaining = yield * sessions . create ( { location : Location . Ref . make ( { directory : source } ) } )
167+ // Match the reported existing database, where global already points at the source.
168+ yield * db . update ( ProjectTable ) . set ( { worktree : source } ) . where ( eq ( ProjectTable . id , Project . ID . global ) ) . run ( )
169+ const before = yield * db . select ( ) . from ( ProjectTable ) . where ( eq ( ProjectTable . id , Project . ID . global ) ) . get ( )
170+ const other = yield * sessions . create ( { location : Location . Ref . make ( { directory : destination } ) } )
171+
172+ yield * Effect . promise ( ( ) => rm ( source , { recursive : true } ) )
173+
174+ yield * sessions . move ( { sessionID : created . id , directory : destination } )
175+ yield * execution . awaitIdle ( created . id )
176+
177+ expect ( yield * sessions . get ( created . id ) ) . toMatchObject ( {
178+ projectID : Project . ID . global ,
179+ location : { directory : destination } ,
180+ } )
181+ expect ( ( yield * sessions . list ( { directory : source } ) ) . data . map ( ( item ) => item . id ) ) . toEqual ( [ remaining . id ] )
182+ expect ( ( yield * sessions . list ( { directory : destination } ) ) . data . map ( ( item ) => item . id ) ) . toEqual (
183+ expect . arrayContaining ( [ created . id , other . id ] ) ,
184+ )
185+ expect (
186+ ( yield * db . select ( ) . from ( ProjectTable ) . where ( eq ( ProjectTable . id , Project . ID . global ) ) . get ( ) ) ?. worktree ,
187+ ) . toBe ( before ?. worktree )
188+ yield * sessions . remove ( remaining . id )
189+ expect ( ( yield * sessions . get ( created . id ) ) . location . directory ) . toBe ( destination )
190+ } ) ,
191+ { timeout : 120_000 } ,
192+ )
193+
147194 itWithInstance . live ( "moves through the bound service without depending on the Session facade" , ( ) =>
148195 Effect . gen ( function * ( ) {
149196 const tmp = yield * tmpdirScoped ( )
0 commit comments