@@ -4,11 +4,94 @@ import * as HostProcess from "@t3tools/shared/HostProcess";
44import * as Effect from "effect/Effect" ;
55import * as FileSystem from "effect/FileSystem" ;
66import * as Layer from "effect/Layer" ;
7+ import * as PlatformError from "effect/PlatformError" ;
8+ import * as Sink from "effect/Sink" ;
9+ import * as Stream from "effect/Stream" ;
710import * as TestConsole from "effect/testing/TestConsole" ;
811import { Command } from "effect/cli" ;
12+ import * as ChildProcess from "effect/process/ChildProcess" ;
13+ import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner" ;
914
1015import { browserCommand } from "./browser.ts" ;
1116
17+ const BROWSER = "/t3/tools/chrome-headless-shell/linux64/154/chrome-headless-shell" ;
18+ const FONT = "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf" ;
19+
20+ /**
21+ * Records every command. `fc-match` is missing until fonts are installed, unless
22+ * the host starts with a font; the browser fails to load libnss3 when asked to.
23+ */
24+ const fakeSpawner = ( host : { readonly font ?: string ; readonly missingLibrary ?: boolean } ) => {
25+ const commands : Array < ReadonlyArray < string > > = [ ] ;
26+ let font = host . font ;
27+ const spawner = ChildProcessSpawner . make ( ( command ) =>
28+ Effect . gen ( function * ( ) {
29+ if ( ! ChildProcess . isStandardCommand ( command ) ) return yield * Effect . die ( "Unexpected pipeline" ) ;
30+ commands . push ( [ command . command , ...command . args ] ) ;
31+ if ( command . command === "apt-get" && command . args . includes ( "fonts-liberation" ) ) font = FONT ;
32+ if ( command . command === "fc-match" && font === undefined ) {
33+ return yield * PlatformError . systemError ( {
34+ _tag : "NotFound" ,
35+ module : "ChildProcess" ,
36+ method : "spawn" ,
37+ } ) ;
38+ }
39+ const output =
40+ command . command === "fc-match"
41+ ? font !
42+ : command . command === BROWSER && host . missingLibrary
43+ ? "error while loading shared libraries: libnss3.so: cannot open shared object file"
44+ : command . command === "ldd"
45+ ? "\tlibnss3.so => not found\n"
46+ : "" ;
47+ const bytes = Stream . make ( new TextEncoder ( ) . encode ( output ) ) ;
48+ return ChildProcessSpawner . makeHandle ( {
49+ pid : ChildProcessSpawner . ProcessId ( 1 ) ,
50+ exitCode : Effect . succeed ( ChildProcessSpawner . ExitCode ( 0 ) ) ,
51+ isRunning : Effect . succeed ( false ) ,
52+ kill : ( ) => Effect . void ,
53+ unref : Effect . succeed ( Effect . void ) ,
54+ stdin : Sink . drain ,
55+ stdout : bytes ,
56+ stderr : Stream . empty ,
57+ all : bytes ,
58+ getInputFd : ( ) => Sink . drain ,
59+ getOutputFd : ( ) => Stream . empty ,
60+ } ) ;
61+ } ) ,
62+ ) ;
63+ return { spawner, commands } ;
64+ } ;
65+
66+ /** A Linux host with apt and T3's browser installed under `/t3`. */
67+ const runSetup = ( host : {
68+ readonly root : boolean ;
69+ readonly font ?: string ;
70+ readonly missingLibrary ?: boolean ;
71+ } ) => {
72+ const { spawner, commands } = fakeSpawner ( host ) ;
73+ const fs = FileSystem . makeNoop ( {
74+ exists : ( path ) => Effect . succeed ( path === "/usr/bin/apt-get" || path === BROWSER ) ,
75+ readDirectory : ( path ) =>
76+ Effect . succeed ( path . endsWith ( "chrome-headless-shell" ) ? [ "linux64" ] : [ "154" ] ) ,
77+ } ) ;
78+ return Effect . gen ( function * ( ) {
79+ yield * Command . runWith ( browserCommand , { version : "0.0.0" } ) ( [ "setup" , "--base-dir" , "/t3" ] ) ;
80+ return { commands, output : ( yield * TestConsole . logLines ) . join ( "\n" ) } ;
81+ } ) . pipe (
82+ Effect . provideService ( FileSystem . FileSystem , fs ) ,
83+ Effect . provideService ( ChildProcessSpawner . ChildProcessSpawner , spawner ) ,
84+ Effect . provideService ( HostProcess . Platform , "linux" ) ,
85+ Effect . provideService ( HostProcess . UserId , host . root ? 0 : 1000 ) ,
86+ // Root through sudo from a regular user, so setup reports ready instead of the root warning.
87+ Effect . provideService ( HostProcess . Environment , host . root ? { SUDO_USER : "alice" } : { } ) ,
88+ Effect . provide ( Layer . mergeAll ( NodeServices . layer , TestConsole . layer ) ) ,
89+ ) ;
90+ } ;
91+
92+ const aptGet = ( commands : ReadonlyArray < ReadonlyArray < string > > ) =>
93+ commands . filter ( ( [ name ] ) => name === "apt-get" ) ;
94+
1295it . effect ( "setup in a root shell warns that the server cannot run as root" , ( ) =>
1396 Effect . gen ( function * ( ) {
1497 // Every file read fails, so setup finds nothing to install and changes nothing.
@@ -18,6 +101,10 @@ it.effect("setup in a root shell warns that the server cannot run as root", () =
18101 "/nonexistent" ,
19102 ] ) . pipe (
20103 Effect . provide ( FileSystem . layerNoop ( { } ) ) ,
104+ Effect . provideService (
105+ ChildProcessSpawner . ChildProcessSpawner ,
106+ fakeSpawner ( { font : FONT } ) . spawner ,
107+ ) ,
21108 Effect . provideService ( HostProcess . Platform , "linux" ) ,
22109 Effect . provideService ( HostProcess . UserId , 0 ) ,
23110 // A root user who follows the old advice runs setup through sudo.
@@ -28,3 +115,32 @@ it.effect("setup in a root shell warns that the server cannot run as root", () =
28115 expect ( output ) . not . toContain ( "ready" ) ;
29116 } ) . pipe ( Effect . provide ( Layer . mergeAll ( NodeServices . layer , TestConsole . layer ) ) ) ,
30117) ;
118+
119+ it . effect ( "setup without root does not report a host without fonts as ready" , ( ) =>
120+ Effect . gen ( function * ( ) {
121+ const { commands, output } = yield * runSetup ( { root : false } ) ;
122+ expect ( output ) . toContain ( "T3's browser needs fontconfig and system fonts; setup installs them." ) ;
123+ expect ( output ) . toContain ( "This needs root. Run:" ) ;
124+ expect ( output ) . not . toContain ( "ready" ) ;
125+ expect ( aptGet ( commands ) ) . toEqual ( [ ] ) ;
126+ } ) ,
127+ ) ;
128+
129+ it . effect ( "setup as root installs missing libraries and fonts after one package refresh" , ( ) =>
130+ Effect . gen ( function * ( ) {
131+ const { commands, output } = yield * runSetup ( { root : true , missingLibrary : true } ) ;
132+ const apt = aptGet ( commands ) ;
133+ expect ( apt . filter ( ( [ , action ] ) => action === "update" ) ) . toHaveLength ( 1 ) ;
134+ expect ( apt . find ( ( args ) => args . includes ( "libnss3" ) ) ) . toBeDefined ( ) ;
135+ expect ( apt ) . toContainEqual ( [
136+ "apt-get" ,
137+ "install" ,
138+ "-y" ,
139+ "--no-install-recommends" ,
140+ "fontconfig" ,
141+ "fonts-liberation" ,
142+ ] ) ;
143+ expect ( output ) . toContain ( "Installed the browser's fonts." ) ;
144+ expect ( output ) . toContain ( "This host is ready for T3's browser." ) ;
145+ } ) ,
146+ ) ;
0 commit comments