11import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection" ;
22import { EnvironmentId , type KeybindingRule , type ServerConfig } from "@t3tools/contracts" ;
33import { compileResolvedKeybindingsConfig } from "@t3tools/shared/keybindings" ;
4- import { Atom , AtomRegistry } from "effect/unstable/ reactivity" ;
4+ import { Atom , AtomRegistry } from "effect/reactivity" ;
55import { afterEach , describe , expect , it } from "vite-plus/test" ;
66
77import { resolveShortcutCommand } from "../keybindings" ;
@@ -53,7 +53,6 @@ function makeHarness(input: {
5353 primaryConfigAtom,
5454 environmentsAtom,
5555 keybindings : ( ) => registry . get ( keybindingsAtom ) ,
56- /** The command Cmd+N runs on macOS. */
5756 cmdN : ( ) =>
5857 resolveShortcutCommand (
5958 { key : "n" , metaKey : true , ctrlKey : false , shiftKey : false , altKey : false } ,
@@ -69,109 +68,90 @@ afterEach(() => {
6968} ) ;
7069
7170describe ( "createShortcutKeybindingsAtom" , ( ) => {
72- it ( "follows the primary environment when it has a config" , ( ) => {
73- const harness = makeHarness ( { primaryEnvironmentId : PRIMARY , primaryConfig : config ( SWAPPED ) } ) ;
74- expect ( harness . cmdN ( ) ) . toBe ( "chat.newLocal" ) ;
75- } ) ;
76-
77- it ( "keeps the defaults while the primary config is loading" , ( ) => {
78- const harness = makeHarness ( {
79- primaryEnvironmentId : PRIMARY ,
80- environments : [ [ B , environment ( "connected" , config ( SWAPPED ) ) ] ] ,
81- } ) ;
71+ it ( "updates shortcuts from the first connected, configured environment without a primary" , ( ) => {
72+ const harness = makeHarness ( { } ) ;
73+ const aConfig = config ( STALE ) ;
74+ const bConfig = config ( SWAPPED ) ;
75+ const edited = config ( [ { key : "mod+n" , command : "sidebar.toggle" } ] ) ;
8276 expect ( harness . cmdN ( ) ) . toBe ( "chat.new" ) ;
83- } ) ;
8477
85- it ( "follows the connected environment without a primary" , ( ) => {
86- const harness = makeHarness ( { environments : [ [ B , environment ( "connected" , config ( SWAPPED ) ) ] ] } ) ;
87- expect ( harness . cmdN ( ) ) . toBe ( "chat.newLocal" ) ;
78+ // One mounted consumer follows config loading, edits, disconnects and reconnects.
79+ for ( const [ a , b , command ] of [
80+ [ environment ( "offline" , aConfig ) , environment ( "connected" , bConfig ) , "chat.newLocal" ] ,
81+ [ environment ( "connected" , null ) , environment ( "connected" , bConfig ) , "chat.newLocal" ] ,
82+ [
83+ environment ( "connected" , aConfig ) ,
84+ environment ( "connected" , bConfig ) ,
85+ "chat.newWithoutProject" ,
86+ ] ,
87+ [ environment ( "connected" , edited ) , environment ( "connected" , bConfig ) , "sidebar.toggle" ] ,
88+ [ environment ( "offline" , edited ) , environment ( "connected" , bConfig ) , "chat.newLocal" ] ,
89+ [ environment ( "offline" , edited ) , environment ( "offline" , bConfig ) , "chat.new" ] ,
90+ [ environment ( "reconnecting" , edited ) , environment ( "offline" , bConfig ) , "chat.new" ] ,
91+ [
92+ environment ( "connected" , aConfig ) ,
93+ environment ( "offline" , bConfig ) ,
94+ "chat.newWithoutProject" ,
95+ ] ,
96+ ] as const ) {
97+ harness . registry . set (
98+ harness . environmentsAtom ,
99+ new Map ( [
100+ [ A , a ] ,
101+ [ B , b ] ,
102+ ] ) ,
103+ ) ;
104+ expect ( harness . cmdN ( ) ) . toBe ( command ) ;
105+ }
88106 } ) ;
89107
90- it ( "skips an offline environment's cached config, as Settings does " , ( ) => {
108+ it ( "uses the primary config or defaults until the primary is removed " , ( ) => {
91109 const harness = makeHarness ( {
92- environments : [
93- [ A , environment ( "offline" , config ( STALE ) ) ] ,
94- [ B , environment ( "connected" , config ( SWAPPED ) ) ] ,
95- ] ,
110+ environments : [ [ B , environment ( "connected" , config ( SWAPPED ) ) ] ] ,
96111 } ) ;
97112 expect ( harness . cmdN ( ) ) . toBe ( "chat.newLocal" ) ;
98- } ) ;
99113
100- it ( "keeps the defaults when no environment is connected" , ( ) => {
101- const harness = makeHarness ( {
102- environments : [ [ A , environment ( "offline" , config ( STALE ) ) ] ] ,
103- } ) ;
114+ harness . registry . set ( harness . primaryEnvironmentIdAtom , PRIMARY ) ;
104115 expect ( harness . cmdN ( ) ) . toBe ( "chat.new" ) ;
105- } ) ;
106116
107- it ( "follows edits to the connected environment's keybindings" , ( ) => {
108- const harness = makeHarness ( { environments : [ [ B , environment ( "connected" , config ( [ ] ) ) ] ] } ) ;
117+ harness . registry . set ( harness . primaryConfigAtom , config ( STALE ) ) ;
118+ expect ( harness . cmdN ( ) ) . toBe ( "chat.newWithoutProject" ) ;
119+
120+ harness . registry . set ( harness . primaryConfigAtom , null ) ;
109121 expect ( harness . cmdN ( ) ) . toBe ( "chat.new" ) ;
110122
111- harness . registry . set (
112- harness . environmentsAtom ,
113- new Map ( [ [ B , environment ( "connected" , config ( SWAPPED ) ) ] ] ) ,
114- ) ;
123+ harness . registry . set ( harness . primaryEnvironmentIdAtom , null ) ;
115124 expect ( harness . cmdN ( ) ) . toBe ( "chat.newLocal" ) ;
116125 } ) ;
117126
118- it ( "keeps the same bindings when an unrelated config update arrives" , ( ) => {
119- const remote = config ( SWAPPED ) ;
120- const harness = makeHarness ( {
121- environments : [
122- [ A , environment ( "offline" , config ( [ ] ) ) ] ,
123- [ B , environment ( "connected" , remote ) ] ,
124- ] ,
125- } ) ;
126- const before = harness . keybindings ( ) ;
127-
128- // A provider status update on B and a reconnect attempt on A.
129- harness . registry . set (
130- harness . environmentsAtom ,
131- new Map ( [
132- [ A , environment ( "reconnecting" , config ( [ ] ) ) ] ,
133- [ B , environment ( "connected" , { ...remote , providers : [ ] } ) ] ,
134- ] ) ,
135- ) ;
136- expect ( harness . keybindings ( ) ) . toBe ( before ) ;
137- } ) ;
127+ it . each ( [ false , true ] ) (
128+ "preserves the bindings array through unrelated updates (primary: %s)" ,
129+ ( hasPrimary ) => {
130+ const selected = config ( SWAPPED ) ;
131+ const harness = makeHarness ( {
132+ primaryEnvironmentId : hasPrimary ? PRIMARY : null ,
133+ primaryConfig : hasPrimary ? selected : null ,
134+ environments : [
135+ [ A , environment ( "connected" , selected ) ] ,
136+ [ B , environment ( "connected" , config ( STALE ) ) ] ,
137+ ] ,
138+ } ) ;
139+ expect ( harness . cmdN ( ) ) . toBe ( "chat.newLocal" ) ;
140+ const before = harness . keybindings ( ) ;
138141
139- it ( "follows connection and primary changes without a reload" , ( ) => {
140- const aConfig = config ( STALE ) ;
141- const bConfig = config ( SWAPPED ) ;
142- const harness = makeHarness ( {
143- environments : [
144- [ A , environment ( "connected" , aConfig ) ] ,
145- [ B , environment ( "connected" , bConfig ) ] ,
146- ] ,
147- } ) ;
148- const setEnvironments = ( a : EnvironmentConnectionPhase , b : EnvironmentConnectionPhase ) =>
149142 harness . registry . set (
150143 harness . environmentsAtom ,
151144 new Map ( [
152- [ A , environment ( a , aConfig ) ] ,
153- [ B , environment ( b , bConfig ) ] ,
145+ [ A , environment ( "connected" , { ... selected , providers : [ ] } ) ] ,
146+ [ B , environment ( "connected" , config ( [ ] ) ) ] ,
154147 ] ) ,
155148 ) ;
149+ expect ( harness . keybindings ( ) ) . toBe ( before ) ;
156150
157- // Both connected: the first in catalog order wins.
158- expect ( harness . cmdN ( ) ) . toBe ( "chat.newWithoutProject" ) ;
159-
160- setEnvironments ( "offline" , "connected" ) ;
161- expect ( harness . cmdN ( ) ) . toBe ( "chat.newLocal" ) ;
162-
163- setEnvironments ( "offline" , "offline" ) ;
164- expect ( harness . cmdN ( ) ) . toBe ( "chat.new" ) ;
165-
166- // A primary appears while its config is loading; B is back but must not leak in.
167- setEnvironments ( "offline" , "connected" ) ;
168- harness . registry . set ( harness . primaryEnvironmentIdAtom , PRIMARY ) ;
169- expect ( harness . cmdN ( ) ) . toBe ( "chat.new" ) ;
170-
171- harness . registry . set (
172- harness . primaryConfigAtom ,
173- config ( [ { key : "mod+n" , command : "sidebar.toggle" } ] ) ,
174- ) ;
175- expect ( harness . cmdN ( ) ) . toBe ( "sidebar.toggle" ) ;
176- } ) ;
151+ if ( hasPrimary ) {
152+ harness . registry . set ( harness . primaryConfigAtom , { ...selected , providers : [ ] } ) ;
153+ expect ( harness . keybindings ( ) ) . toBe ( before ) ;
154+ }
155+ } ,
156+ ) ;
177157} ) ;
0 commit comments