@@ -253,14 +253,14 @@ describe("a datamate_manager write that races the link", () => {
253253 calls = [ ]
254254 api . isConfigured = async ( ) => true
255255 api . getDatamate = async ( ) => ( { id : "5" , name : "ops" } )
256- api . getCredentials = async ( ) => ( { } )
256+ api . getCredentials = async ( ) => ( { tenant : "acme" } )
257257 api . buildMcpConfig = ( ) => ( { type : "remote" , url : "https://mcpserver.example.invalid/sse" } )
258- api . createDatamate = async ( ) => {
259- calls . push ( " createDatamate" )
258+ api . createDatamate = async ( _payload : unknown , creds : { tenant ?: string } ) => {
259+ calls . push ( ` createDatamate ${ creds ?. tenant } ` )
260260 return { id : "77" , name : "ops" }
261261 }
262- api . deleteDatamate = async ( id : string ) => {
263- calls . push ( `deleteDatamate ${ id } ` )
262+ api . deleteDatamate = async ( id : string , creds : { tenant ?: string } ) => {
263+ calls . push ( `deleteDatamate ${ id } ${ creds ?. tenant } ` )
264264 }
265265 } )
266266 afterEach ( ( ) => Object . assign ( api , saved ) )
@@ -280,14 +280,14 @@ describe("a datamate_manager write that races the link", () => {
280280 return { atCall, resume }
281281 }
282282
283- /** `datamate` entries in any config file the tool could write. */
284- async function sharedKeyOnDisk ( dir : string ) : Promise < string [ ] > {
283+ /** Files with a `datamate` or `datamate-<name>` entry, of those the tool could write. */
284+ async function datamateEntriesOnDisk ( dir : string ) : Promise < string [ ] > {
285285 const found : string [ ] = [ ]
286286 for ( const sub of [ "" , ".altimate-code" , ".opencode" ] ) {
287287 for ( const name of await fs . readdir ( path . join ( dir , sub ) ) . catch ( ( ) => [ ] as string [ ] ) ) {
288288 if ( ! / \. j s o n c ? $ / . test ( name ) ) continue
289289 const text = await fs . readFile ( path . join ( dir , sub , name ) , "utf8" )
290- if ( / " d a t a m a t e " \s * : / . test ( text ) ) found . push ( path . join ( sub , name ) )
290+ if ( / " d a t a m a t e (?: - [ ^ " ] + ) ? " \s * : / . test ( text ) ) found . push ( path . join ( sub , name ) )
291291 }
292292 }
293293 return found
@@ -299,7 +299,7 @@ describe("a datamate_manager write that races the link", () => {
299299 // Linked before the datamate is created: nothing is created.
300300 [ "create, linked before the POST" , { operation : "create" , name : "ops" } , "isConfigured" , [ ] ] ,
301301 // Linked while it is being created: it is deleted again.
302- [ "create, linked during the POST" , { operation : "create" , name : "ops" } , "createDatamate" , [ "createDatamate" , "deleteDatamate 77" ] ] ,
302+ [ "create, linked during the POST" , { operation : "create" , name : "ops" } , "createDatamate" , [ "createDatamate acme " , "deleteDatamate 77 acme " ] ] ,
303303 ]
304304 for ( const [ label , args , pausedAt , expectedCalls ] of rows ) {
305305 test ( `${ label } : started unlinked, refused once a boundary links the project, and nothing is left written` , async ( ) => {
@@ -338,12 +338,75 @@ describe("a datamate_manager write that races the link", () => {
338338 }
339339 expect ( mcpCalls ) . toEqual ( [ ] )
340340 expect ( calls ) . toEqual ( expectedCalls )
341- expect ( await sharedKeyOnDisk ( tmp . path ) ) . toEqual ( [ ] )
341+ expect ( await datamateEntriesOnDisk ( tmp . path ) ) . toEqual ( [ ] )
342342 // The workspace engine is still the one connected.
343343 expect ( await syncInternals . mcp ! . status ( ) ) . toHaveProperty ( DATAMATE_KEY )
344344 } )
345345 }
346346
347+ test ( "a link that lands during a standalone server's connection takes the entry back out" , async ( ) => {
348+ await using tmp = await tmpdir ( )
349+ const state : State = { link : "unlinked" }
350+ arrange ( tmp . path , state )
351+ await beforeTurn ( "ses_a" )
352+ let connected ! : ( ) => void
353+ const connecting = new Promise < void > ( ( resolve ) => ( connected = resolve ) )
354+ let reached ! : ( ) => void
355+ const atConnect = new Promise < void > ( ( resolve ) => ( reached = resolve ) )
356+ const removed : string [ ] = [ ]
357+ const add = spyOn ( MCP , "add" ) . mockImplementation ( async ( ) => {
358+ reached ( )
359+ await connecting
360+ return { } as Awaited < ReturnType < typeof MCP . add > >
361+ } )
362+ const remove = spyOn ( MCP , "remove" ) . mockImplementation ( async ( name ) => {
363+ removed . push ( name )
364+ } )
365+ try {
366+ const result = await Instance . provide ( {
367+ directory : tmp . path ,
368+ fn : async ( ) => {
369+ const call = ( await initTool ( DatamateManagerTool ) ) . execute ( { operation : "add" , datamate_id : "5" } , ctx as any )
370+ await atConnect
371+ state . link = "linked"
372+ await beforeTurn ( "ses_b" )
373+ connected ( )
374+ return call
375+ } ,
376+ } )
377+ expect ( result . title ) . toBe ( "Datamate add: off in a project linked to a workspace" )
378+ expect ( removed ) . toEqual ( [ "datamate-ops" ] )
379+ expect ( await datamateEntriesOnDisk ( tmp . path ) ) . toEqual ( [ ] )
380+ } finally {
381+ connected ( )
382+ add . mockRestore ( )
383+ remove . mockRestore ( )
384+ }
385+ } )
386+
387+ test ( "a raced create is deleted with the account it was created in, after a credential switch" , async ( ) => {
388+ await using tmp = await tmpdir ( )
389+ const state : State = { link : "unlinked" }
390+ arrange ( tmp . path , state )
391+ await beforeTurn ( "ses_a" )
392+ const { atCall, resume } = pauseAt ( "createDatamate" )
393+ const result = await Instance . provide ( {
394+ directory : tmp . path ,
395+ fn : async ( ) => {
396+ const call = ( await initTool ( DatamateManagerTool ) ) . execute ( { operation : "create" , name : "ops" } , ctx as any )
397+ await atCall
398+ // The user signs into another tenant, and the project is linked, while the POST runs.
399+ api . getCredentials = async ( ) => ( { tenant : "globex" } )
400+ state . link = "linked"
401+ await beforeTurn ( "ses_b" )
402+ resume ( )
403+ return call
404+ } ,
405+ } )
406+ expect ( result . title ) . toBe ( "Datamate create: off in a project linked to a workspace" )
407+ expect ( calls ) . toEqual ( [ "createDatamate acme" , "deleteDatamate 77 acme" ] )
408+ } )
409+
347410 test ( "a standalone server's connection does not hold up the directory's turn boundaries" , async ( ) => {
348411 await using tmp = await tmpdir ( )
349412 arrange ( tmp . path , { link : "unlinked" } )
0 commit comments