@@ -3,6 +3,7 @@ import * as NodeChildProcess from "node:child_process";
33import * as NodeServices from "@effect/platform-node/NodeServices" ;
44import { expect , it } from "@effect/vitest" ;
55import * as HostProcess from "@t3tools/shared/HostProcess" ;
6+ import * as Clock from "effect/Clock" ;
67import * as ConfigProvider from "effect/ConfigProvider" ;
78import * as Duration from "effect/Duration" ;
89import * as Effect from "effect/Effect" ;
@@ -139,12 +140,20 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
139140 const control : {
140141 failCommand : string | undefined ;
141142 stateAfterStop ?: string ;
142- linger : string ;
143+ /** `undefined` makes `loginctl show-user` fail, as when logind does not know the user. */
144+ linger : string | undefined ;
145+ managerReachable : boolean ;
146+ /** The user bus is found only through `XDG_RUNTIME_DIR`, as without a login session. */
147+ busNeedsRuntimeDir : boolean ;
148+ lingerStartsManager : boolean ;
143149 enabled : boolean ;
144150 active : boolean ;
145151 } = {
146152 failCommand : undefined ,
147153 linger : "yes" ,
154+ managerReachable : true ,
155+ busNeedsRuntimeDir : false ,
156+ lingerStartsManager : true ,
148157 enabled : true ,
149158 active : true ,
150159 } ;
@@ -157,18 +166,24 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
157166 invocations . push ( input ) ;
158167 timeouts . set ( command , input . timeout ) ;
159168 const failed = command === control . failCommand ;
160- if ( ! failed && command === "loginctl enable-linger --no-ask-password 501" )
169+ if ( ! failed && command . startsWith ( "loginctl enable-linger --no-ask-password " ) ) {
161170 control . linger = "yes" ;
171+ control . managerReachable ||= control . lingerStartsManager ;
172+ }
162173 if ( ! failed && command === "systemctl --user enable t3code.service" ) control . enabled = true ;
163174 if ( ! failed && command === "systemctl --user restart t3code.service" ) control . active = true ;
175+ // The manager loads units only from its own unit directory.
176+ const unitOutsideManager =
177+ command === "systemctl --user enable t3code.service" &&
178+ ! ( yield * fs . exists ( path . join ( managerUnitDir , "t3code.service" ) ) ) ;
164179 // A timed-out stop may still stop the service.
165180 if ( command === "systemctl --user stop t3code.service" ) control . active = false ;
166181 if (
167182 control . stateAfterStop !== undefined &&
168183 ( command === "systemctl --user stop t3code.service" ||
169184 command . startsWith ( "launchctl bootout --wait " ) )
170185 ) {
171- yield * fs . writeFileString ( statePath , control . stateAfterStop ) . pipe ( Effect . orDie ) ;
186+ yield * fs . writeFileString ( statePath , control . stateAfterStop ) ;
172187 }
173188 return {
174189 stdout :
@@ -186,7 +201,7 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
186201 : input . args [ 0 ] === "--version"
187202 ? `t3 v${ / v e r s i o n s \/ ( [ ^ / ] + ) \/ / . exec ( input . command ) ?. [ 1 ] ?? "1.2.3" } \n`
188203 : input . command === "loginctl" && input . args [ 0 ] === "show-user"
189- ? `${ control . linger } \n`
204+ ? `${ control . linger ?? "" } \n`
190205 : input . args [ 1 ] === "is-enabled"
191206 ? control . enabled
192207 ? "enabled\n"
@@ -195,6 +210,13 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
195210 stderr : "" ,
196211 code : ChildProcessSpawner . ExitCode (
197212 failed ||
213+ unitOutsideManager ||
214+ ( ( command === "systemctl --user show-environment" || input . command === "busctl" ) &&
215+ ( ! control . managerReachable ||
216+ ( control . busNeedsRuntimeDir && input . env ?. XDG_RUNTIME_DIR === undefined ) ) ) ||
217+ ( input . command === "loginctl" &&
218+ input . args [ 0 ] === "show-user" &&
219+ control . linger === undefined ) ||
198220 ( input . args [ 1 ] === "is-active" && ! control . active ) ||
199221 ( input . args [ 1 ] === "is-enabled" && ! control . enabled )
200222 ? 1
@@ -206,14 +228,15 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
206228 stdoutInvalidUtf8 : false ,
207229 stderrInvalidUtf8 : false ,
208230 } ;
209- } ) ,
231+ } , Effect . orDie ) ,
210232 } ) ;
211233 const makeService = (
212234 environmentPath : string | undefined = installerPath ,
213235 cliVersion = "1.2.3" ,
214236 serviceBaseDir = baseDir ,
215237 insideService = false ,
216238 hostExecPath = "/usr/bin/t3" ,
239+ underSudo = false ,
217240 ) =>
218241 Effect . gen ( function * ( ) {
219242 const paths = pinnedRuntimePaths ( path , serviceBaseDir , cliVersion , platform ) ;
@@ -231,7 +254,7 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
231254 Effect . provide (
232255 Layer . mergeAll (
233256 Layer . succeed ( HostProcess . Platform , platform ) ,
234- Layer . succeed ( HostProcess . UserId , 501 ) ,
257+ Layer . succeed ( HostProcess . UserId , underSudo ? 0 : 501 ) ,
235258 Layer . succeed ( HostProcess . ExecutablePath , hostExecPath ) ,
236259 Layer . succeed (
237260 HttpClient . HttpClient ,
@@ -246,6 +269,7 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
246269 ? { }
247270 : { PATH : environmentPath } ) ,
248271 ...( insideService ? { T3_BOOT_SERVICE_UNIT : "t3code.service" } : { } ) ,
272+ ...( underSudo ? { SUDO_USER : "julius" } : { } ) ,
249273 } ,
250274 } ) ,
251275 ) ,
@@ -345,6 +369,140 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
345369 } ) ,
346370 ) ;
347371
372+ // regression: #29881
373+ it . effect . each ( [
374+ { logind : "has lingering disabled" , linger : "no" } ,
375+ { logind : "does not know the user" , linger : undefined } ,
376+ ] ) (
377+ "enables lingering to bring up an unreachable user manager when logind $logind" ,
378+ ( { linger } ) =>
379+ Effect . gen ( function * ( ) {
380+ const { service, commands, control } = yield * makeHarness ( ) ;
381+ control . linger = linger ;
382+ control . managerReachable = false ;
383+
384+ yield * service . install ( ) ;
385+
386+ expect ( commands ) . toContain ( "loginctl enable-linger --no-ask-password 501" ) ;
387+ expect ( ( yield * service . status ) . current ) . toBe ( true ) ;
388+ } ) ,
389+ ) ;
390+
391+ it . effect ( "does not reach a running root user manager under sudo" , ( ) =>
392+ Effect . gen ( function * ( ) {
393+ const { makeService, commands, control } = yield * makeHarness ( ) ;
394+ control . busNeedsRuntimeDir = true ;
395+ const service = yield * makeService ( undefined , "1.2.3" , undefined , false , undefined , true ) ;
396+
397+ expect ( yield * service . install ( ) . pipe ( Effect . flip ) ) . toMatchObject ( {
398+ _tag : "BootServicePrerequisiteError" ,
399+ problem : "user-manager-unavailable" ,
400+ } ) ;
401+ expect ( commands ) . not . toContain ( "systemctl --user enable t3code.service" ) ;
402+ } ) ,
403+ ) ;
404+
405+ it . effect ( "does not enable lingering for root under sudo" , ( ) =>
406+ Effect . gen ( function * ( ) {
407+ const { makeService, commands, control } = yield * makeHarness ( ) ;
408+ control . linger = undefined ;
409+ control . managerReachable = false ;
410+ const service = yield * makeService ( undefined , "1.2.3" , undefined , false , undefined , true ) ;
411+
412+ expect ( yield * service . install ( ) . pipe ( Effect . flip ) ) . toMatchObject ( {
413+ _tag : "BootServicePrerequisiteError" ,
414+ problem : "user-manager-unavailable" ,
415+ } ) ;
416+ expect ( commands . filter ( ( command ) => command . includes ( "enable-linger" ) ) ) . toEqual ( [ ] ) ;
417+ } ) ,
418+ ) ;
419+
420+ // regression: #29881
421+ it . effect ( "installs into the unit directory of the user manager that lingering starts" , ( ) =>
422+ Effect . gen ( function * ( ) {
423+ const path = yield * Path . Path ;
424+ const fs = yield * FileSystem . FileSystem ;
425+ const shellConfigHome = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-xdg-config-" } ) ;
426+ const { makeService, home, control } = yield * makeHarness (
427+ "linux" ,
428+ undefined ,
429+ shellConfigHome ,
430+ "" ,
431+ ) ;
432+ control . linger = "no" ;
433+ control . managerReachable = false ;
434+ const service = yield * makeService ( ) ;
435+
436+ const plan = yield * service . install ( ) ;
437+
438+ const unitPath = path . join ( home , ".config" , "systemd" , "user" , "t3code.service" ) ;
439+ expect ( plan . unitPath ) . toBe ( unitPath ) ;
440+ expect ( yield * service . status ) . toMatchObject ( { current : true , unitPath } ) ;
441+ } ) ,
442+ ) ;
443+
444+ // regression: #29881
445+ it . effect . each ( [
446+ { manager : "starts after three probes" , upAfterPauses : 3 , pauses : 3 } ,
447+ { manager : "never starts" , upAfterPauses : undefined , pauses : 10 } ,
448+ ] ) (
449+ "waits a bounded time for the user manager lingering starts when it $manager" ,
450+ ( { upAfterPauses, pauses } ) =>
451+ Effect . gen ( function * ( ) {
452+ const { service, fs, control } = yield * makeHarness ( ) ;
453+ const { unitPath } = yield * service . status ;
454+ control . linger = "no" ;
455+ control . managerReachable = false ;
456+ control . lingerStartsManager = false ;
457+ const clock = yield * Clock . Clock ;
458+ const waited : number [ ] = [ ] ;
459+
460+ const result = yield * service . install ( ) . pipe (
461+ Effect . provideService ( Clock . Clock , {
462+ ...clock ,
463+ sleep : ( duration ) =>
464+ Effect . sync ( ( ) => {
465+ if ( waited . push ( Duration . toMillis ( duration ) ) === upAfterPauses ) {
466+ control . managerReachable = true ;
467+ }
468+ } ) ,
469+ } ) ,
470+ Effect . result ,
471+ ) ;
472+
473+ expect ( waited ) . toEqual ( Array . from ( { length : pauses } , ( ) => 500 ) ) ;
474+ if ( upAfterPauses === undefined ) {
475+ expect ( result ) . toMatchObject ( {
476+ failure : { _tag : "BootServicePrerequisiteError" , problem : "user-manager-unavailable" } ,
477+ } ) ;
478+ expect ( yield * fs . exists ( unitPath ) ) . toBe ( false ) ;
479+ } else {
480+ expect ( result . _tag ) . toBe ( "Success" ) ;
481+ expect ( ( yield * service . status ) . current ) . toBe ( true ) ;
482+ }
483+ } ) ,
484+ ) ;
485+
486+ // regression: #29881
487+ it . effect ( "points systemd user commands at the user's runtime directory when none is set" , ( ) =>
488+ Effect . gen ( function * ( ) {
489+ const { service, invocations, makeService } = yield * makeHarness ( ) ;
490+ yield * service . install ( ) ;
491+ const inside = yield * makeService ( undefined , "1.2.4" , undefined , true ) ;
492+ yield * inside . install ( { allowDowngrade : true } ) ;
493+
494+ const systemdCalls = invocations . filter ( ( { command } ) =>
495+ [ "systemctl" , "busctl" , "systemd-run" , "loginctl" ] . includes ( command ) ,
496+ ) ;
497+ expect ( new Set ( systemdCalls . map ( ( { command } ) => command ) ) . size ) . toBe ( 4 ) ;
498+ expect (
499+ systemdCalls
500+ . filter ( ( { env } ) => env ?. XDG_RUNTIME_DIR !== "/run/user/501" )
501+ . map ( ( { command, args } ) => `${ command } ${ args . join ( " " ) } ` ) ,
502+ ) . toEqual ( [ ] ) ;
503+ } ) ,
504+ ) ;
505+
348506 it . effect . each ( [
349507 { command : "systemctl --user show-environment" , problem : "user-manager-unavailable" } ,
350508 { command : "loginctl show-user 501 --property=Linger --value" , problem : "linger-unavailable" } ,
0 commit comments