// @effect-diagnostics nodeBuiltinImport:off - plain local servers stand in for LAN services. import * as NodeServices from "@effect/platform-node/NodeServices"; import { describe, expect, it } from "@effect/vitest"; import { ThreadId } from "@t3tools/contracts"; import * as HostProcess from "@t3tools/shared/HostProcess"; import { HTML_RENDER_MEASURE_FONTS, HTML_RENDER_MEASURE_WIDTHS, htmlRenderTheme, } from "@t3tools/shared/htmlRender"; import { T3_CODE_DARK_THEME_COLORS } from "@t3tools/shared/themePalettes"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; import * as Base64 from "effect/encoding/Base64"; import * as Fiber from "effect/Fiber"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as Path from "effect/Path"; import * as NodeDgram from "node:dgram"; import * as NodeHttp from "node:http"; import * as NodeURL from "node:url"; import { resolveAttachmentPathById } from "../attachmentStore.ts"; import * as ServerConfig from "../config.ts"; import * as HtmlRender from "./HtmlRender.ts"; import * as PreviewBrowser from "../preview/PreviewBrowser.ts"; // Real-browser tests run only when this names a chrome-headless-shell, for // example one T3 installed under /tools/chrome-headless-shell. const TEST_BROWSER_ENV = "T3CODE_TEST_HEADLESS_SHELL"; const layerHtmlRender = ( executable?: string, installed: Effect.Effect> = Effect.succeed( Option.fromUndefinedOr(executable), ), ) => HtmlRender.layer.pipe( Layer.provide( Layer.succeed( PreviewBrowser.PreviewBrowser, PreviewBrowser.PreviewBrowser.of({ executable: executable === undefined ? Effect.die("This test has no preview browser.") : Effect.succeed(executable), installed, }), ), ), Layer.provideMerge(ServerConfig.layerTest(process.cwd(), { prefix: "t3-html-render-" })), Layer.provideMerge(NodeServices.layer), ); const layerTest = layerHtmlRender(); const PNG_BYTES = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]); describe("HtmlRender", () => { it.effect("inlines local images by absolute path and leaves URLs and relative paths alone", () => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; const htmlRender = yield* HtmlRender.HtmlRender; const directory = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-html-images-" }); const png = path.join(directory, "shot.png"); const svg = path.join(directory, "logo.svg"); yield* fileSystem.writeFile(png, PNG_BYTES); yield* fileSystem.writeFileString(svg, ""); const kept = [ "https://example.com/a.png", "//cdn.example.com/b.png", "./c.png", "data:image/png;base64,AAAA", ]; const prepared = yield* htmlRender.prepare( [ "Shots", `
`, ``, ...kept.map((src) => ``), "", ].join(""), ); const pngUri = `data:image/png;base64,${Base64.encode(PNG_BYTES)}`; const svgUri = `data:image/svg+xml;base64,${Base64.encode("")}`; expect(prepared).toContain(``); expect(prepared).toContain(`url(${svgUri})`); expect(prepared).toContain(`['${pngUri}', \`${svgUri}\`]`); expect(prepared).not.toContain(directory); for (const src of kept) expect(prepared).toContain(``); // The theme bootstrap opens the head, ahead of the page's own markup. expect(prepared.indexOf("")).toBeLessThan(prepared.indexOf('