@@ -127,8 +127,17 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
127127
128128 const commands : string [ ] = [ ] ;
129129 const timeouts = new Map < string , unknown > ( ) ;
130- const control : { failCommand : string | undefined ; stateAfterStop ?: string } = {
130+ const control : {
131+ failCommand : string | undefined ;
132+ stateAfterStop ?: string ;
133+ linger : string ;
134+ enabled : boolean ;
135+ active : boolean ;
136+ } = {
131137 failCommand : undefined ,
138+ linger : "yes" ,
139+ enabled : true ,
140+ active : true ,
132141 } ;
133142 const runner = ProcessRunner . ProcessRunner . of ( {
134143 run : Effect . fn ( "test.run_boot_service_command" ) ( function * (
@@ -137,6 +146,11 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
137146 const command = `${ input . command } ${ input . args . join ( " " ) } ` ;
138147 commands . push ( command ) ;
139148 timeouts . set ( command , input . timeout ) ;
149+ const failed = command === control . failCommand ;
150+ if ( ! failed && command === "loginctl enable-linger --no-ask-password 501" )
151+ control . linger = "yes" ;
152+ if ( ! failed && command === "systemctl --user enable t3code.service" ) control . enabled = true ;
153+ if ( ! failed && command === "systemctl --user restart t3code.service" ) control . active = true ;
140154 if (
141155 control . stateAfterStop !== undefined &&
142156 ( command === "systemctl --user stop t3code.service" ||
@@ -145,9 +159,20 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
145159 yield * fs . writeFileString ( statePath , control . stateAfterStop ) . pipe ( Effect . orDie ) ;
146160 }
147161 return {
148- stdout : input . args [ 1 ] === "--version" ? "t3 v1.2.3\n" : "" ,
162+ stdout :
163+ input . args [ 1 ] === "--version"
164+ ? "t3 v1.2.3\n"
165+ : input . command === "loginctl" && input . args [ 0 ] === "show-user"
166+ ? `${ control . linger } \n`
167+ : input . args [ 1 ] === "is-enabled"
168+ ? control . enabled
169+ ? "enabled\n"
170+ : "disabled\n"
171+ : "" ,
149172 stderr : "" ,
150- code : ChildProcessSpawner . ExitCode ( command === control . failCommand ? 1 : 0 ) ,
173+ code : ChildProcessSpawner . ExitCode (
174+ failed || ( input . args [ 1 ] === "is-active" && ! control . active ) ? 1 : 0 ,
175+ ) ,
151176 timedOut : false ,
152177 stdoutTruncated : false ,
153178 stderrTruncated : false ,
@@ -182,10 +207,103 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
182207 ) ,
183208 ) ;
184209 const service = yield * makeService ( ) ;
185- return { service, makeService, fs, statePath, commands, timeouts, control } ;
210+ return { service, makeService, fs, statePath, commands, timeouts, control, runtime } ;
186211} ) ;
187212
188213it . layer ( NodeServices . layer ) ( "boot service install" , ( it ) => {
214+ it . effect (
215+ "fails before installing files or validating a runtime when lingering needs an administrator" ,
216+ ( ) =>
217+ Effect . gen ( function * ( ) {
218+ const { service, fs, statePath, commands, control, runtime } = yield * makeHarness ( ) ;
219+ const before = yield * service . status ;
220+ control . linger = "no" ;
221+ control . failCommand = "loginctl enable-linger --no-ask-password 501" ;
222+ yield * fs . remove ( runtime . sentinelPath ) ;
223+
224+ const error = yield * service . install ( ) . pipe ( Effect . flip ) ;
225+
226+ expect ( error ) . toMatchObject ( {
227+ _tag : "BootServicePrerequisiteError" ,
228+ problem : "linger-disabled" ,
229+ } ) ;
230+ expect ( error . message ) . toContain ( 'sudo loginctl enable-linger "$(id -un)"' ) ;
231+ expect ( error . message ) . toContain ( "last login session ends" ) ;
232+ expect ( yield * fs . exists ( before . unitPath ) ) . toBe ( false ) ;
233+ expect ( yield * fs . exists ( statePath ) ) . toBe ( false ) ;
234+ expect (
235+ commands . some ( ( command ) => command . startsWith ( "npm " ) || command . includes ( "--version" ) ) ,
236+ ) . toBe ( false ) ;
237+ expect (
238+ commands . some (
239+ ( command ) => command . includes ( "daemon-reload" ) || command . includes ( "restart" ) ,
240+ ) ,
241+ ) . toBe ( false ) ;
242+ expect ( yield * fs . readFileString ( before . logPath ) ) . toContain ( "[linger-disabled]" ) ;
243+ } ) ,
244+ ) ;
245+
246+ it . effect (
247+ "detects a partial install and preserves the running service when repair lacks permission" ,
248+ ( ) =>
249+ Effect . gen ( function * ( ) {
250+ const { service, fs, statePath, commands, control } = yield * makeHarness ( ) ;
251+ const plan = yield * service . install ( ) ;
252+ const before = yield * fs . readFileString ( statePath ) ;
253+ const unit = yield * fs . readFileString ( plan . unitPath ) ;
254+ control . linger = "no" ;
255+ control . failCommand = "loginctl enable-linger --no-ask-password 501" ;
256+
257+ expect ( yield * service . status ) . toMatchObject ( {
258+ current : false ,
259+ problems : [ "linger-disabled" ] ,
260+ } ) ;
261+ commands . length = 0 ;
262+ expect ( ( yield * service . install ( ) . pipe ( Effect . flip ) ) . _tag ) . toBe (
263+ "BootServicePrerequisiteError" ,
264+ ) ;
265+ expect ( yield * fs . readFileString ( statePath ) ) . toBe ( before ) ;
266+ expect ( yield * fs . readFileString ( plan . unitPath ) ) . toBe ( unit ) ;
267+ expect ( commands ) . not . toContain ( "systemctl --user stop t3code.service" ) ;
268+ } ) ,
269+ ) ;
270+
271+ it . effect ( "enables lingering before installing and repairs stopped or disabled services" , ( ) =>
272+ Effect . gen ( function * ( ) {
273+ const { service, commands, control } = yield * makeHarness ( ) ;
274+ control . linger = "no" ;
275+ yield * service . install ( ) ;
276+ expect ( control . linger ) . toBe ( "yes" ) ;
277+ expect ( commands . indexOf ( "loginctl enable-linger --no-ask-password 501" ) ) . toBeLessThan (
278+ commands . indexOf ( "systemctl --user daemon-reload" ) ,
279+ ) ;
280+
281+ control . enabled = false ;
282+ control . active = false ;
283+ expect ( yield * service . status ) . toMatchObject ( {
284+ current : false ,
285+ problems : [ "service-disabled" , "service-stopped" ] ,
286+ } ) ;
287+ yield * service . install ( ) ;
288+ expect ( ( yield * service . status ) . current ) . toBe ( true ) ;
289+ } ) ,
290+ ) ;
291+
292+ it . effect . each ( [
293+ { command : "systemctl --user show-environment" , problem : "user-manager-unavailable" } ,
294+ { command : "loginctl show-user 501 --property=Linger --value" , problem : "linger-unavailable" } ,
295+ ] ) ( "reports failed prerequisite probes without installing: $command" , ( { command, problem } ) =>
296+ Effect . gen ( function * ( ) {
297+ const { service, fs, statePath, control } = yield * makeHarness ( ) ;
298+ control . failCommand = command ;
299+ expect ( yield * service . install ( ) . pipe ( Effect . flip ) ) . toMatchObject ( {
300+ _tag : "BootServicePrerequisiteError" ,
301+ problem,
302+ } ) ;
303+ expect ( yield * fs . exists ( statePath ) ) . toBe ( false ) ;
304+ } ) ,
305+ ) ;
306+
189307 it . effect ( "installs, reports current state, and uninstalls" , ( ) =>
190308 Effect . gen ( function * ( ) {
191309 const { service, fs, statePath, commands, timeouts } = yield * makeHarness ( ) ;
@@ -286,8 +404,10 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
286404 expect ( yield * fs . readFileString ( plan . launcherPath ) ) . toBe ( launcher ) ;
287405 expect ( yield * fs . readFileString ( plan . unitPath ) ) . toBe ( unit ) ;
288406 expect (
289- commands . filter ( ( command ) =>
290- command . startsWith ( platform === "linux" ? "systemctl " : "launchctl " ) ,
407+ commands . filter (
408+ ( command ) =>
409+ command . startsWith ( platform === "linux" ? "systemctl " : "launchctl " ) &&
410+ ! command . includes ( "show-environment" ) ,
291411 ) ,
292412 ) . toEqual (
293413 platform === "linux"
@@ -351,7 +471,11 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
351471
352472 const error = yield * service . install ( ) . pipe ( Effect . flip ) ;
353473 expect ( error . _tag ) . toBe ( "BootServiceCommandError" ) ;
354- expect ( commands . filter ( ( command ) => command . startsWith ( "systemctl " ) ) ) . toEqual ( [
474+ expect (
475+ commands . filter (
476+ ( command ) => command . startsWith ( "systemctl " ) && ! command . includes ( "show-environment" ) ,
477+ ) ,
478+ ) . toEqual ( [
355479 "systemctl --user stop t3code.service" ,
356480 "systemctl --user daemon-reload" ,
357481 "systemctl --user restart t3code.service" ,
@@ -382,7 +506,11 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
382506 "BootServiceUpdatePendingError" ,
383507 ) ;
384508 expect ( serviceStateHasPendingUpdate ( yield * fs . readFileString ( statePath ) ) ) . toBe ( true ) ;
385- expect ( commands . filter ( ( command ) => command . startsWith ( "systemctl " ) ) ) . toEqual ( [
509+ expect (
510+ commands . filter (
511+ ( command ) => command . startsWith ( "systemctl " ) && ! command . includes ( "show-environment" ) ,
512+ ) ,
513+ ) . toEqual ( [
386514 "systemctl --user stop t3code.service" ,
387515 "systemctl --user restart t3code.service" ,
388516 ] ) ;
0 commit comments