|
|
@@ -1,12 +1,16 @@ |
|
|
import { SERVER_EXIT_CODE_STATE_DIR_OWNED } from "@t3tools/contracts"; |
|
|
import { assert, describe, it } from "@effect/vitest"; |
|
|
import * as Deferred from "effect/Deferred"; |
|
|
import * as Effect from "effect/Effect"; |
|
|
import * as FileSystem from "effect/FileSystem"; |
|
|
import * as Layer from "effect/Layer"; |
|
|
import * as Option from "effect/Option"; |
|
|
import * as Ref from "effect/Ref"; |
|
|
import * as Sink from "effect/Sink"; |
|
|
import * as Stream from "effect/Stream"; |
|
|
import { HttpClient } from "effect/http"; |
|
|
import { ChildProcessSpawner } from "effect/process"; |
|
|
import type * as Electron from "electron"; |
|
|
|
|
|
import * as DesktopObservability from "../app/DesktopObservability.ts"; |
|
|
import * as DesktopState from "../app/DesktopState.ts"; |
|
Expand All
|
@@ -15,24 +19,80 @@ import * as DesktopTelemetryPublisher from "../telemetry/DesktopTelemetryPublish |
|
|
import * as DesktopBrowserHost from "../preview/DesktopBrowserHost.ts"; |
|
|
import * as ElectronDialog from "../electron/ElectronDialog.ts"; |
|
|
import * as ElectronApp from "../electron/ElectronApp.ts"; |
|
|
import * as ElectronWindow from "../electron/ElectronWindow.ts"; |
|
|
import * as DesktopWindow from "../window/DesktopWindow.ts"; |
|
|
import * as DesktopWslEnvironment from "../wsl/DesktopWslEnvironment.ts"; |
|
|
import * as DesktopBackendConfiguration from "./DesktopBackendConfiguration.ts"; |
|
|
import * as DesktopBackendPool from "./DesktopBackendPool.ts"; |
|
|
import type { DesktopBackendStartConfig } from "./DesktopBackendManager.ts"; |
|
|
|
|
|
function layerPool(labelRef: Ref.Ref<string>): Layer.Layer<DesktopBackendPool.DesktopBackendPool> { |
|
|
const backendConfig: DesktopBackendStartConfig = { |
|
|
executablePath: "/electron", |
|
|
args: [], |
|
|
entryPath: "/server/bin.mjs", |
|
|
cwd: "/server", |
|
|
env: {}, |
|
|
bootstrap: { |
|
|
mode: "desktop", |
|
|
noBrowser: true, |
|
|
port: 3774, |
|
|
t3Home: "/tmp/t3", |
|
|
host: "127.0.0.1", |
|
|
desktopBootstrapToken: "token", |
|
|
tailscaleServeEnabled: false, |
|
|
tailscaleServePort: 443, |
|
|
}, |
|
|
bootstrapDelivery: "fd3", |
|
|
extendEnv: true, |
|
|
httpBaseUrl: new URL("http://127.0.0.1:3774"), |
|
|
captureOutput: false, |
|
|
preflightFailure: Option.none(), |
|
|
}; |
|
|
|
|
|
interface LayerPoolOverrides { |
|
|
readonly primaryExitCode?: number; |
|
|
readonly electronDialog?: ElectronDialog.ElectronDialog["Service"]; |
|
|
readonly electronApp?: Partial<ElectronApp.ElectronApp["Service"]>; |
|
|
readonly mainWindow?: Electron.BrowserWindow; |
|
|
} |
|
|
|
|
|
function layerPool( |
|
|
labelRef: Ref.Ref<string>, |
|
|
overrides: LayerPoolOverrides = {}, |
|
|
): Layer.Layer<DesktopBackendPool.DesktopBackendPool> { |
|
|
const { primaryExitCode, mainWindow } = overrides; |
|
|
const main = Option.fromUndefinedOr(mainWindow); |
|
|
return DesktopBackendPool.layer.pipe( |
|
|
Layer.provideMerge( |
|
|
Layer.mergeAll( |
|
|
FileSystem.layerNoop({}), |
|
|
FileSystem.layerNoop({ exists: () => Effect.succeed(true) }), |
|
|
Layer.succeed( |
|
|
ChildProcessSpawner.ChildProcessSpawner, |
|
|
ChildProcessSpawner.make(() => Effect.die("unexpected child process spawn")), |
|
|
ChildProcessSpawner.make(() => |
|
|
primaryExitCode === undefined |
|
|
? Effect.die("unexpected child process spawn") |
|
|
: Effect.succeed( |
|
|
ChildProcessSpawner.makeHandle({ |
|
|
pid: ChildProcessSpawner.ProcessId(123), |
|
|
stdout: Stream.empty, |
|
|
stderr: Stream.empty, |
|
|
all: Stream.empty, |
|
|
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(primaryExitCode)), |
|
|
isRunning: Effect.succeed(false), |
|
|
kill: () => Effect.void, |
|
|
stdin: Sink.drain, |
|
|
getInputFd: () => Sink.drain, |
|
|
getOutputFd: () => Stream.empty, |
|
|
unref: Effect.succeed(Effect.void), |
|
|
}), |
|
|
), |
|
|
), |
|
|
), |
|
|
Layer.succeed( |
|
|
HttpClient.HttpClient, |
|
|
HttpClient.make(() => Effect.die("unexpected HTTP request")), |
|
|
HttpClient.make(() => |
|
|
primaryExitCode === undefined ? Effect.die("unexpected HTTP request") : Effect.never, |
|
|
), |
|
|
), |
|
|
Layer.succeed(DesktopObservability.DesktopBackendOutputLogFactory, { |
|
|
forInstance: () => |
|
Expand All
|
@@ -57,16 +117,38 @@ function layerPool(labelRef: Ref.Ref<string>): Layer.Layer<DesktopBackendPool.De |
|
|
}), |
|
|
DesktopBrowserHost.layer, |
|
|
Layer.succeed(DesktopBackendConfiguration.DesktopBackendConfiguration, { |
|
|
resolvePrimary: Effect.die("unexpected primary config resolve"), |
|
|
resolvePrimary: |
|
|
primaryExitCode === undefined |
|
|
? Effect.die("unexpected primary config resolve") |
|
|
: Effect.succeed(backendConfig), |
|
|
resolvePrimaryLabel: Ref.get(labelRef), |
|
|
resolveWsl: () => Effect.die("unexpected WSL config resolve"), |
|
|
currentBootstrapToken: Effect.die("unexpected bootstrap token read"), |
|
|
} satisfies DesktopBackendConfiguration.DesktopBackendConfiguration["Service"]), |
|
|
DesktopAppSettings.layerTest(), |
|
|
DesktopState.layer, |
|
|
DesktopWslEnvironment.layerTest(), |
|
|
ElectronDialog.layer, |
|
|
ElectronApp.layer, |
|
|
overrides.electronDialog === undefined |
|
|
? ElectronDialog.layer |
|
|
: Layer.succeed(ElectronDialog.ElectronDialog, overrides.electronDialog), |
|
|
Layer.succeed(ElectronApp.ElectronApp, { |
|
|
...ElectronApp.make, |
|
|
...overrides.electronApp, |
|
|
}), |
|
|
Layer.succeed(ElectronWindow.ElectronWindow, { |
|
|
create: () => Effect.die("unexpected window create"), |
|
|
main: Effect.succeed(main), |
|
|
currentMainOrFirst: Effect.succeed(main), |
|
|
focusedMainOrFirst: Effect.succeed(main), |
|
|
setMain: () => Effect.die("unexpected main window set"), |
|
|
clearMain: () => Effect.die("unexpected main window clear"), |
|
|
prepareReveal: () => Effect.succeed(false), |
|
|
reveal: () => Effect.die("unexpected window reveal"), |
|
|
sendAll: () => Effect.void, |
|
|
destroyAll: Effect.void, |
|
|
syncAllAppearance: (sync) => |
|
|
Option.match(main, { onNone: () => Effect.void, onSome: sync }), |
|
|
} satisfies ElectronWindow.ElectronWindow["Service"]), |
|
|
Layer.succeed(DesktopWindow.DesktopWindow, { |
|
|
createMain: Effect.die("unexpected window create"), |
|
|
ensureMain: Effect.die("unexpected window ensure"), |
|
Expand Down
Expand Up
|
@@ -99,25 +181,7 @@ describe("DesktopBackendPool", () => { |
|
|
id, |
|
|
label: Effect.succeed("WSL (Ubuntu)"), |
|
|
configResolve: Effect.succeed({ |
|
|
executablePath: "/electron", |
|
|
args: [], |
|
|
entryPath: "/server/bin.mjs", |
|
|
cwd: "/server", |
|
|
env: {}, |
|
|
bootstrap: { |
|
|
mode: "desktop", |
|
|
noBrowser: true, |
|
|
port: 3774, |
|
|
t3Home: "/tmp/t3", |
|
|
host: "127.0.0.1", |
|
|
desktopBootstrapToken: "token", |
|
|
tailscaleServeEnabled: false, |
|
|
tailscaleServePort: 443, |
|
|
}, |
|
|
bootstrapDelivery: "fd3", |
|
|
extendEnv: true, |
|
|
httpBaseUrl: new URL("http://127.0.0.1:3774"), |
|
|
captureOutput: false, |
|
|
...backendConfig, |
|
|
preflightFailure: Option.some({ reason: "WSL starting", fatal: false }), |
|
|
} satisfies DesktopBackendStartConfig), |
|
|
}); |
|
Expand All
|
@@ -133,4 +197,63 @@ describe("DesktopBackendPool", () => { |
|
|
}).pipe(Effect.provide(layerPool(Ref.makeUnsafe("Windows")))), |
|
|
), |
|
|
); |
|
|
|
|
|
// Linux shows a parentless error box behind the focused main window, so the user never sees it. |
|
|
it.effect("shows the unavailable T3 home error attached to the main window, then quits", () => { |
|
|
const mainWindow = { id: 1, isDestroyed: () => false } as unknown as Electron.BrowserWindow; |
|
|
const events: Array< |
|
|
| { readonly _tag: "Dialog"; readonly text: string; readonly owner?: Electron.BrowserWindow } |
|
|
| { readonly _tag: "Quit" } |
|
|
> = []; |
|
|
const quit = Deferred.makeUnsafe<void>(); |
|
|
return Effect.gen(function* () { |
|
|
const primary = yield* (yield* DesktopBackendPool.DesktopBackendPool).primary; |
|
|
yield* primary.start; |
|
|
yield* Deferred.await(quit); |
|
|
|
|
|
const dialog = events.find( |
|
|
(event) => event._tag === "Dialog" && event.text.includes("This T3 home is unavailable"), |
|
|
); |
|
|
assert.isDefined(dialog, "the T3 home unavailable error was never shown"); |
|
|
assert.strictEqual( |
|
|
dialog._tag === "Dialog" ? dialog.owner : undefined, |
|
|
mainWindow, |
|
|
"the T3 home unavailable error must be attached to the main window", |
|
|
); |
|
|
assert.deepStrictEqual( |
|
|
events.map((event) => event._tag), |
|
|
["Dialog", "Quit"], |
|
|
); |
|
|
}).pipe( |
|
|
Effect.scoped, |
|
|
Effect.provide( |
|
|
layerPool(Ref.makeUnsafe("Windows"), { |
|
|
primaryExitCode: SERVER_EXIT_CODE_STATE_DIR_OWNED, |
|
|
electronDialog: { |
|
|
pickFolder: () => Effect.die("unexpected folder picker"), |
|
|
pickFiles: () => Effect.die("unexpected file picker"), |
|
|
showMessageBox: (options, owner) => |
|
|
Effect.sync(() => { |
|
|
events.push({ |
|
|
_tag: "Dialog", |
|
|
text: [options.title, options.message, options.detail].join("\n"), |
|
|
...(owner === undefined ? {} : { owner }), |
|
|
}); |
|
|
return { response: 0, checkboxChecked: false }; |
|
|
}), |
|
|
showErrorBox: (title, content) => |
|
|
Effect.sync(() => { |
|
|
events.push({ _tag: "Dialog", text: `${title}\n${content}` }); |
|
|
}), |
|
|
}, |
|
|
electronApp: { |
|
|
quit: Effect.sync(() => { |
|
|
events.push({ _tag: "Quit" }); |
|
|
}).pipe(Effect.andThen(Deferred.succeed(quit, undefined))), |
|
|
}, |
|
|
mainWindow, |
|
|
}), |
|
|
), |
|
|
); |
|
|
}); |
|
|
}); |