11import { describe , expect , it , vi } from "vite-plus/test" ;
22import { EnvironmentId , ThreadId , type TerminalSummary } from "@t3tools/contracts" ;
33import { selectRunningSubprocessTerminalIds } from "@t3tools/client-runtime/state/terminal" ;
4- import { AtomRegistry } from "effect/reactivity" ;
4+ import * as Cause from "effect/Cause" ;
5+ import * as Option from "effect/Option" ;
6+ import { AsyncResult , AtomRegistry } from "effect/reactivity" ;
57
6- import { attachedTerminalSessionAtom , selectKnownTerminalSessions } from "./terminalSessions" ;
8+ import {
9+ attachedTerminalSessionAtom ,
10+ knownTerminalSessionsAtom ,
11+ selectKnownTerminalSessions ,
12+ } from "./terminalSessions" ;
713
814const attachCalls = vi . hoisted ( ( ) => [ ] as unknown [ ] ) ;
15+ const metadataAtom = await vi . hoisted ( async ( ) => {
16+ const { AsyncResult, Atom } = await import ( "effect/reactivity" ) ;
17+ return Atom . make < AsyncResult . AsyncResult < ReadonlyArray < TerminalSummary > > > (
18+ AsyncResult . initial ( true ) ,
19+ ) ;
20+ } ) ;
921vi . mock ( "./terminal" , async ( ) => {
1022 const { AsyncResult, Atom } = await import ( "effect/reactivity" ) ;
1123 return {
@@ -14,7 +26,7 @@ vi.mock("./terminal", async () => {
1426 attachCalls . push ( request ) ;
1527 return Atom . make ( AsyncResult . initial ( true ) ) ;
1628 } ,
17- metadata : ( ) => Atom . make ( AsyncResult . initial ( true ) ) ,
29+ metadata : ( ) => metadataAtom ,
1830 } ,
1931 } ;
2032} ) ;
@@ -100,6 +112,37 @@ describe("selectKnownTerminalSessions", () => {
100112 } ) ;
101113} ) ;
102114
115+ describe ( "knownTerminalSessionsAtom" , ( ) => {
116+ const metadataResult = ( ...terminals : ReadonlyArray < TerminalSummary > ) =>
117+ AsyncResult . success < ReadonlyArray < TerminalSummary > > ( terminals ) ;
118+
119+ it ( "notifies only when the thread's own terminals change" , ( ) => {
120+ const registry = AtomRegistry . make ( ) ;
121+ const sessions = knownTerminalSessionsAtom ( environmentA , threadA ) ;
122+ expect ( registry . get ( sessions ) ) . toBeNull ( ) ;
123+
124+ const first = summary ( threadA , "terminal-1" ) ;
125+ const other = summary ( threadB , "terminal-1" ) ;
126+ registry . set ( metadataAtom , metadataResult ( first ) ) ;
127+ const notified : unknown [ ] = [ ] ;
128+ const unsubscribe = registry . subscribe ( sessions , ( value ) => notified . push ( value ) ) ;
129+ const before = registry . get ( sessions ) ;
130+
131+ registry . set ( metadataAtom , metadataResult ( first , other ) ) ;
132+ registry . set ( metadataAtom , metadataResult ( first , { ...other , label : "Renamed" } ) ) ;
133+ expect ( notified ) . toEqual ( [ ] ) ;
134+ expect ( registry . get ( sessions ) ) . toBe ( before ) ;
135+
136+ registry . set ( metadataAtom , metadataResult ( { ...first , label : "Renamed" } , other ) ) ;
137+ expect ( notified ) . toHaveLength ( 1 ) ;
138+
139+ const previousSuccess = Option . some ( metadataResult ( first ) ) ;
140+ registry . set ( metadataAtom , AsyncResult . failure ( Cause . die ( "unavailable" ) , { previousSuccess } ) ) ;
141+ expect ( registry . get ( sessions ) ) . toBeNull ( ) ;
142+ unsubscribe ( ) ;
143+ } ) ;
144+ } ) ;
145+
103146describe ( "attachedTerminalSessionAtom" , ( ) => {
104147 it ( "attaches with the launch input it was keyed by" , ( ) => {
105148 attachCalls . length = 0 ;
0 commit comments