|
1 | 1 | // @effect-diagnostics nodeBuiltinImport:off - plain local servers stand in for LAN services. |
2 | 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
3 | | -import { describe, expect, it } from "@effect/vitest"; |
| 3 | +import { assert, describe, expect, it } from "@effect/vitest"; |
4 | 4 | import { EnvironmentId, ProviderInstanceId, ThreadId } from "@t3tools/contracts"; |
5 | 5 | import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions"; |
6 | 6 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
7 | 7 | import * as Deferred from "effect/Deferred"; |
8 | 8 | import * as Effect from "effect/Effect"; |
| 9 | +import * as Exit from "effect/Exit"; |
9 | 10 | import * as Base64 from "effect/encoding/Base64"; |
10 | 11 | import * as Fiber from "effect/Fiber"; |
11 | 12 | import * as FileSystem from "effect/FileSystem"; |
@@ -19,6 +20,7 @@ import * as NodeURL from "node:url"; |
19 | 20 | import { resolveAttachmentPathById } from "../attachmentStore.ts"; |
20 | 21 | import * as ServerConfig from "../config.ts"; |
21 | 22 | import * as HtmlRender from "./HtmlRender.ts"; |
| 23 | +import { HtmlRenderBrowserError } from "./headlessChrome.ts"; |
22 | 24 | import * as PreviewBrowser from "../preview/PreviewBrowser.ts"; |
23 | 25 |
|
24 | 26 | const TEST_BROWSER_ENV = "T3CODE_TEST_HEADLESS_SHELL"; |
@@ -287,6 +289,66 @@ describe("HtmlRender", () => { |
287 | 289 | }), |
288 | 290 | ); |
289 | 291 |
|
| 292 | + // Windows reports ENOENT asynchronously for this path instead of throwing ENOTDIR. |
| 293 | + describe.skipIf(HostProcess.Platform.defaultValue() === "win32")( |
| 294 | + "when the browser spawn throws synchronously", |
| 295 | + () => { |
| 296 | + it.live("reports a typed preview error", () => |
| 297 | + Effect.gen(function* () { |
| 298 | + const fileSystem = yield* FileSystem.FileSystem; |
| 299 | + const path = yield* Path.Path; |
| 300 | + const file = yield* fileSystem.makeTempFileScoped({ prefix: "t3-browser-spawn-" }); |
| 301 | + const outcome = yield* HtmlRender.HtmlRender.use((htmlRender) => |
| 302 | + htmlRender.preview({ html: "<p>x</p>" }).pipe(Effect.flip), |
| 303 | + ).pipe( |
| 304 | + Effect.provide(layerHtmlRender(path.join(file, "chrome-headless-shell"))), |
| 305 | + Effect.exit, |
| 306 | + ); |
| 307 | + assert(Exit.isSuccess(outcome), "Expected a typed preview error."); |
| 308 | + const error = outcome.value; |
| 309 | + expect(error).toBeInstanceOf(HtmlRenderBrowserError); |
| 310 | + expect(error.message).toBe( |
| 311 | + "Headless Chrome could not render the page: the browser could not be started.", |
| 312 | + ); |
| 313 | + expect(error).toMatchObject({ cause: { code: "ENOTDIR" } }); |
| 314 | + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 315 | + ); |
| 316 | + it.live("publishes the page without measured heights", () => |
| 317 | + Effect.gen(function* () { |
| 318 | + const fileSystem = yield* FileSystem.FileSystem; |
| 319 | + const path = yield* Path.Path; |
| 320 | + const file = yield* fileSystem.makeTempFileScoped({ prefix: "t3-browser-spawn-" }); |
| 321 | + yield* Effect.gen(function* () { |
| 322 | + const config = yield* ServerConfig.ServerConfig; |
| 323 | + const htmlRender = yield* HtmlRender.HtmlRender; |
| 324 | + const outcome = yield* htmlRender |
| 325 | + .publish({ |
| 326 | + threadId: ThreadId.make("thread-html-spawn"), |
| 327 | + html: "<p>Browser unavailable</p>", |
| 328 | + title: "Saved page", |
| 329 | + height: 300, |
| 330 | + }) |
| 331 | + .pipe(Effect.exit); |
| 332 | + assert(Exit.isSuccess(outcome), "Expected a published attachment."); |
| 333 | + const reference = outcome.value; |
| 334 | + expect(reference).toEqual({ |
| 335 | + attachmentId: expect.any(String), |
| 336 | + title: "Saved page", |
| 337 | + height: 300, |
| 338 | + }); |
| 339 | + const stored = resolveAttachmentPathById({ |
| 340 | + attachmentsDir: config.attachmentsDir, |
| 341 | + attachmentId: reference.attachmentId, |
| 342 | + }); |
| 343 | + expect(yield* fileSystem.readFileString(stored ?? "")).toContain( |
| 344 | + "<p>Browser unavailable</p>", |
| 345 | + ); |
| 346 | + }).pipe(Effect.provide(layerHtmlRender(path.join(file, "chrome-headless-shell")))); |
| 347 | + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 348 | + ); |
| 349 | + }, |
| 350 | + ); |
| 351 | + |
290 | 352 | it.live("keeps every local file but the page itself out of the browser", (ctx) => |
291 | 353 | Effect.gen(function* () { |
292 | 354 | const executable = (yield* HostProcess.Environment)[TEST_BROWSER_ENV]; |
|