// @vitest-environment happy-dom import * as NodeHttpClient from "@effect/platform-node/NodeHttpClient" import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer" import { DevTools, Inspector } from "@effect/ui-devtools" import * as DevToolsHeadless from "@effect/ui-devtools/DevToolsHeadless" import * as Control from "@effect/ui-devtools/internal/control" import * as Mcp from "@effect/ui-devtools/internal/mcp" import * as Remote from "@effect/ui-devtools/internal/remote" import * as Server from "@effect/ui-devtools/internal/server" import { DevToolsServer, DevToolsVite } from "@effect/ui-devtools/node" import { assert, describe, it } from "@effect/vitest" import { Context, Effect, Exit, Layer, Option, PubSub, Schedule, Schema, SchemaGetter, Scope } from "effect" import { McpClient, McpProtocol } from "effect/ai" import { HttpRouter } from "effect/http" import { AsyncResult } from "effect/reactivity" import * as Socket from "effect/socket/Socket" import { Command, MessageLog, Query, UiSchema } from "effect/ui" import { BrowserHost, Html } from "effect/ui/html" import { HtmlLocator } from "effect/ui/html/testing" import { createServer, request } from "node:http" import type { AddressInfo } from "node:net" import { type Capture, EditorMessage, play } from "./fixtures/TwoEditors.ts" import { playTwoWrites, secret } from "./fixtures/TwoWrites.ts" const wait = (ms: number) => Effect.promise(() => new Promise((resolve) => setTimeout(resolve, ms))) const token = "tok_live_9f2c" const text = (node: Element | null | undefined) => (node?.textContent ?? "").replace(/\s+/g, " ").trim() const overlay = () => document.querySelector("iframe[data-effect-ui-devtools]")! .contentDocument!.body // happy-dom's fetch applies CORS, so the origin probes use node:http. const probeStatus = ( url: string, options: { readonly method?: string readonly headers: Record }, body = "" ) => Effect.promise( () => new Promise((resolve, reject) => { const req = request(url, options, (res) => { res.resume() res.on("end", () => resolve(res.statusCode ?? 0)) }) req.on("upgrade", (res, socket) => { socket.destroy() resolve(res.statusCode ?? 0) }) req.on("error", reject) req.end(body) }) ) const makeAgent = Effect.fnUntraced(function*(base: string) { const transport = yield* McpClient.http({ url: base + "/mcp", protocol: McpProtocol.v2025_11_25 }).pipe(Effect.provide(NodeHttpClient.layerNodeHttp)) const client = yield* McpClient.make({ name: "test", version: "0" }).pipe( Effect.provideService(McpClient.Transport, transport) ) const tools = yield* client.listTools() const byName = new Map(tools.map((tool) => [tool.name, tool])) const results: Array = [] const invoke = (name: string, args?: Record) => client.callTool({ tool: byName.get(name)!, arguments: args }).pipe( Effect.orDie, Effect.tap((result) => Effect.sync(() => results.push(JSON.stringify(result)))) ) return { tools, results, call: ( name: string, args?: Record ): Effect.Effect => invoke(name, args).pipe( Effect.map((result) => { if (result.isError === true) { throw new Error( result.content?.[0]?.type === "text" ? result.content[0].text : JSON.stringify(result) ) } return (result.structuredContent as { readonly result: A }).result }) ), fails: ( name: string, args?: Record ): Effect.Effect => invoke(name, args).pipe( Effect.map((result) => { assert.isTrue(result.isError, name + " should fail") return JSON.stringify(result.content ?? result.structuredContent) }) ) } }) const serve = Effect.fnUntraced(function*(options?: DevToolsServer.Options) { const port = options?.port ?? (yield* freePort) yield* Layer.build(DevToolsServer.layer({ ...options, port })) const url = DevToolsServer.url({ port }) return { url, agent: yield* makeAgent(url) } }) const open = Effect.fnUntraced( function*(url: string, capture: Capture) { localStorage.clear() const services = yield* Layer.build(DevTools.layer({ server: url })) const app = yield* play(capture).pipe(Effect.provide(services)) yield* wait(150) return app } ) const until = (check: () => boolean) => Effect.suspend(function loop(): Effect.Effect { return check() ? Effect.void : Effect.andThen(wait(10), Effect.suspend(loop)) }).pipe(Effect.timeout("2 seconds")) // A fixed port can collide with one the OS hands out. const freePort = Effect.promise(() => new Promise((resolve, reject) => { const server = createServer() server.on("error", reject) server.listen(0, "127.0.0.1", () => { const { port } = server.address() as AddressInfo server.close(() => resolve(port)) }) }) ) class GetNote extends Query.Service()("notes/GetNote", { args: { id: Schema.String }, success: Schema.String, error: Schema.Never, staleTime: "30 seconds" }) {} class Touch extends Command.Service()("notes/Touch", { args: { id: Schema.String }, success: Schema.Void }) {} const Note = Html.component({ name: "Note", props: Schema.Struct({ id: Schema.String }), model: Schema.Struct({}), message: UiSchema.TaggedEnum({ Click: {} }), queries: { note: { query: GetNote, args: ({ props }) => ({ id: props.id }) } } }).make({ init: () => ({}), update: ($) => $.model, view: (h) => h.div() }) const serveFast = Effect.fnUntraced(function*(timeout: number, size = 100) { const http = createServer() const { routes, server } = Server.make({ size, dispatch: true, keep: 5, timeout }) yield* Layer.build( HttpRouter.serve(Layer.mergeAll(routes, Mcp.layer(server)), { disableLogger: true, disableListenLog: true }).pipe( Layer.provide( NodeHttpServer.layer(() => http, { port: 0, host: "127.0.0.1" }) ) ) ) const url = "http://127.0.0.1:" + (http.address() as AddressInfo).port return { url, agent: yield* makeAgent(url) } }) const connectSocket = (url: string) => Effect.callback((resume) => { const socket = new WebSocket(url) socket.addEventListener("open", () => resume(Effect.succeed(socket))) }) const at = { session: "s", at: 0, path: "/App", instance: 1, component: "App" } const propsAt = (seq: number) => JSON.stringify({ _tag: "PropsChanged", ...at, seq, props: { seq } }) const updateAt = (seq: number, excluded: boolean) => JSON.stringify({ _tag: "Update", ...at, seq, source: { _tag: "DomEvent", name: "click" }, message: { _tag: excluded ? "Ticked" : "Clicked" }, props: {}, model: { seq }, commands: [], events: [], lists: [], sends: [], ...(excluded ? { excluded: true } : {}) }) const fakePage = Effect.fnUntraced(function*( url: string, dispatch: (log: WebSocket, message: unknown) => Effect.Effect< BrowserHost.Dispatched > ) { const log = yield* connectSocket(url.replace("http", "ws") + "/log") yield* Effect.addFinalizer(() => Effect.sync(() => log.close())) log.send(JSON.stringify({ _tag: "Hello", version: 1, host: "test" })) log.send( JSON.stringify({ _tag: "SessionStarted", ...at, seq: 0, app: "App" }) ) log.send(propsAt(1)) const found = { instance: 1, path: "/App", component: "App", message: Schema.Struct({ _tag: Schema.Literal("Go"), n: Schema.Number }), dispatch: (message: unknown) => dispatch(log, message) } const root = { instance: (n: number) => n === 1 ? found : undefined } yield* Remote.connect(url.replace("http", "ws") + "/control", { roots: new Map([["s", root as unknown as BrowserHost.Controllable]]), size: 100, paused: () => undefined, paint: () => Effect.succeed(undefined), resume: Effect.void, notRecording: () => [], setRecording: () => {}, picks: () => [], changes: yield* PubSub.sliding(1) }).pipe(Effect.provide(Socket.layerWebSocketConstructorGlobal)) yield* wait(100) return log }) const go = (count: number) => Array.from({ length: count }, (_, n) => ({ _tag: "Go", n })) describe("DevToolsServer", { concurrent: false }, () => { it.live( "answers why a save never finished from the server's copy of the log", () => Effect.gen(function*() { const { agent, url } = yield* serve() yield* open(url, "interrupted") assert.deepStrictEqual(agent.tools.map((tool) => tool.name), [ "list_sessions", "list_instances", "list_entries", "count_entries", "get_entry", "get_chain", "get_state", "diff_state", "show_entry", "resume", "set_recording", "get_message_schema", "find_instances", "get_picked", "dispatch", "wait" ]) assert.isTrue( agent.tools.find((tool) => tool.name === "dispatch")?.annotations ?.destructiveHint ) assert.isTrue( agent.tools.find((tool) => tool.name === "get_state")?.annotations ?.readOnlyHint ) const [session] = yield* agent.call("list_sessions") assert.strictEqual(session.app, "Workspace") assert.strictEqual(session.host, "browser") assert.isTrue(session.connected) assert.isTrue(session.controllable) const instances: ReadonlyArray = yield* agent.call( "list_instances" ) const n2 = instances.find((instance) => instance.path === "/Workspace/Editor[n2]") assert.match(n2.status, /interrupted/) assert.isDefined(n2.unmounted) const { rows } = yield* agent.call("list_entries", { instance: n2.instance, filter: "commands" }) const interrupted = rows.find((row: any) => row.tag === "CommandInterrupted") const save = rows.find((row: any) => row.label === "ClickedSave") assert.isDefined(interrupted) const chain: ReadonlyArray = yield* agent.call("get_chain", { seq: interrupted.seq }) const steps = JSON.stringify(chain) assert.include(steps, "ClickedSave") assert.include(steps, "PressedEscape") assert.include(steps, "notes/SaveNote") const diff = yield* agent.call("diff_state", { seq: save.seq }) assert.deepInclude(diff.changes, { path: ["status"], kind: "changed", before: "Dirty", after: "Saving" }) const state = yield* agent.call("get_state", { instance: n2.instance, at: save.seq }) assert.strictEqual(state.model.status, "Saving") assert.deepStrictEqual(state.props.token, { "~redacted": true }) const now = yield* agent.call("get_state", { instance: n2.instance }) assert.match(now.unmounted, /^unmounted at #\d+$/) const counts = yield* agent.call("count_entries") assert.strictEqual(counts.messages.ClickedSave, 2) const entry = yield* agent.call("get_entry", { seq: save.seq, path: ["message"] }) assert.deepStrictEqual(entry, { _tag: "ClickedSave" }) assert.notInclude(agent.results.join("\n"), token) }).pipe(Effect.scoped), 30_000 ) it.live( "reads and sets the page's choice of what to record", () => Effect.gen(function*() { const { agent, url } = yield* serve() localStorage.clear() sessionStorage.clear() const services = yield* Layer.build( DevTools.layer({ server: url, exclude: ["UpdatedDraft"] }) ) const app = yield* play("open").pipe(Effect.provide(services)) yield* wait(150) const body = HtmlLocator.getByRole(app, "textbox", { name: "Body n1" }) const labels = Effect.map( agent.call("list_entries", { filter: "messages", limit: 100 }), ({ rows }) => rows.map((row: any) => row.label) as Array ) assert.notInclude(yield* labels, "UpdatedDraft") const [session] = yield* agent.call("list_sessions") assert.deepStrictEqual(session.notRecording, [{ tag: "UpdatedDraft" }]) assert.deepStrictEqual( yield* agent.call("set_recording", { tag: "UpdatedDraft", recording: true }), [] ) HtmlLocator.type(body, "draft") yield* wait(100) assert.include(yield* labels, "UpdatedDraft") }).pipe(Effect.scoped), 30_000 ) it.live( "keeps the state of an instance whose entries the ring dropped", () => Effect.gen(function*() { const { agent, url } = yield* serve({ size: 12 }) yield* open(url, "open") // Wait for eviction, not just the socket connection. const [session] = yield* agent.call>( "list_sessions" ).pipe( Effect.filterOrFail(([session]) => session?.first > 1), Effect.retry(Schedule.spaced("10 millis")), Effect.timeout("2 seconds") ) const instances: ReadonlyArray = yield* agent.call( "list_instances" ) const workspace = instances.find((instance) => instance.path === "/Workspace") assert.isBelow(workspace.mounted, session.first) const state = yield* agent.call("get_state", { instance: workspace.instance }) assert.deepStrictEqual(state.model, { open: ["n1", "n2"] }) const message = yield* agent.fails("get_state", { instance: workspace.instance, at: 0 }) assert.include(message, "older than the last 12 entries") }).pipe(Effect.scoped), 30_000 ) it.live( "truncates a headless app's Model over maxEntryBytes", () => Effect.gen(function*() { const { agent, url } = yield* serve() const services = yield* Layer.build( DevToolsHeadless.layer({ server: url, maxEntryBytes: 1 }) ) const log = Context.get(services, MessageLog.MessageLog) const base = { session: "s", at: 0 } log.record({ _tag: "SessionStarted", ...base, seq: 0, app: "Note" }) log.record({ _tag: "Mounted", ...base, seq: 1, path: "/Note", instance: 1, component: Note, props: { id: "a" }, model: {}, commands: [], events: [], adopted: false } as MessageLog.Entry) yield* agent.call>("list_sessions").pipe( Effect.filterOrFail(([session]) => session?.last === 1), Effect.retry(Schedule.spaced("10 millis")), Effect.timeout("5 seconds") ) const state = yield* agent.call("get_state", { instance: 1 }) assert.deepStrictEqual(state.model, { "~truncated": 2 }) }).pipe(Effect.scoped), 30_000 ) it.live("headless log connects with the provided WebSocket constructor", () => Effect.gen(function*() { const urls: Array = [] yield* Layer.build( DevToolsHeadless.layerWebSocket({ server: "http://127.0.0.1:4774" }) .pipe(Layer.provide( Layer.succeed(Socket.WebSocketConstructor, (url) => { urls.push(String(url)) throw new Error("no WebSocket on this host") }) )) ) yield* until(() => urls.length > 0) assert.deepStrictEqual(urls, ["ws://127.0.0.1:4774/log"]) }).pipe(Effect.scoped)) it.live( "shows live state that left the page's buffer before the server started", () => Effect.gen(function*() { const port = yield* freePort const page = yield* Layer.build( MessageLog.socket( DevToolsServer.url({ port }).replace("http", "ws") + "/log", { buffer: 1 } ).pipe(Layer.provide(Socket.layerWebSocketConstructorGlobal)) ) const log = Context.get(page, MessageLog.MessageLog) const def = Query.defOf(GetNote) const note = (id: string) => Query.makeSnapshot( def, "note", { args: { id }, argsKey: def.argsKey({ id }) }, AsyncResult.success("Note " + id, { timestamp: 0 }) ) const base = (seq: number) => ({ seq, at: 0, session: "s", path: "/Note", instance: 1, component: Note }) const run = (seq: number, index: number) => ({ ...base(seq), name: "notes/Touch", issued: { seq: 1, index }, spanId: "span", traceId: "trace" }) const update = ( seq: number, commands: ReadonlyArray> ) => ({ _tag: "Update", ...base(seq), source: { _tag: "Dispatch" }, message: { _tag: "Click" }, props: { id: "b" }, model: {}, queries: { note: note("b") }, commands, events: [], lists: [], sends: [] }) for ( const entry of [ { _tag: "SessionStarted", seq: 0, at: 0, session: "s", app: "Note" }, { _tag: "Mounted", ...base(1), props: { id: "a" }, model: {}, queries: { note: note("a") }, commands: [Touch.make({ id: "a" }), Touch.make({ id: "b" })], events: [], adopted: false }, { _tag: "CommandStarted", ...run(2, 0) }, { _tag: "CommandEnded", ...run(3, 0), silent: true }, { _tag: "CommandStarted", ...run(4, 1) }, // Simulate a queued command with no CommandStarted yet. update(5, [Touch.make({ id: "b" })]), update(6, []), { _tag: "PropsChanged", ...base(7), props: { id: "c" } }, { _tag: "QueryChanged", ...base(8), name: "note", result: note("c") }, { _tag: "Placed", ...base(9) } ] as ReadonlyArray ) log.record(entry) const { agent } = yield* serve({ port }) const [session] = yield* agent.call>( "list_sessions" ).pipe( Effect.filterOrFail(([session]) => session?.last === 9), Effect.retry(Schedule.spaced("10 millis")), Effect.timeout("5 seconds") ) assert.strictEqual(session.app, "Note") const [instance] = yield* agent.call("list_instances") assert.strictEqual(instance.status, "1 ended, 1 running, 1 waiting") const state = yield* agent.call("get_state", { instance: 1 }) assert.deepStrictEqual(state.props, { id: "c" }) assert.strictEqual(state.queries.note[2], "Note c") }).pipe(Effect.scoped), 30_000 ) it.live( "keeps the host a native app's socket reports", () => Effect.gen(function*() { const { agent, url } = yield* serve() const page = yield* Layer.build( MessageLog.socket(url.replace("http", "ws") + "/log", { host: "native" }).pipe(Layer.provide(Socket.layerWebSocketConstructorGlobal)) ) Context.get(page, MessageLog.MessageLog).record({ _tag: "SessionStarted", seq: 0, at: 0, session: "s", app: "App" }) const [session] = yield* agent.call>( "list_sessions" ).pipe( Effect.filterOrFail(([session]) => session !== undefined), Effect.retry(Schedule.spaced("10 millis")), Effect.timeout("5 seconds") ) assert.strictEqual(session.host, "native") }).pipe(Effect.scoped), 30_000 ) it.live( "keeps a stopped instance's query results and unmount, for its state and a late viewer", () => Effect.gen(function*() { const { agent, url } = yield* serve() const socket = yield* connectSocket(url.replace("http", "ws") + "/log") yield* Effect.addFinalizer(() => Effect.sync(() => socket.close())) const stopped = { ...at, excluded: true } const update = (seq: number, queries?: object) => ({ _tag: "Update", ...stopped, seq, source: { _tag: "Dispatch" }, message: { _tag: "Ticked" }, props: {}, model: seq, ...(queries === undefined ? undefined : { queries }), commands: [], events: [], lists: [], sends: [] }) for ( const frame of [ { _tag: "Hello", version: 1, host: "test" }, { _tag: "SessionStarted", ...at, seq: 0, app: "App" }, { _tag: "Mounted", ...at, seq: 1, props: {}, model: 1, queries: { a: 1, b: 2 }, commands: [], events: [], adopted: false }, update(2), { _tag: "QueryChanged", ...stopped, seq: 3, name: "a", result: 3 }, update(4), // A shown entry, so the next stopped update doesn't replace #4. { _tag: "PropsChanged", ...at, seq: 5, props: {} }, update(6, { a: 3, b: 4 }), update(7), { _tag: "Unmounted", ...stopped, seq: 8 }, // Shown FetchStarted closes the interval so Child's stopped unmount replays. { _tag: "Mounted", ...stopped, path: "/App/Child", instance: 2, seq: 9, props: {}, model: 0, commands: [], events: [], adopted: false }, { _tag: "FetchStarted", session: "s", at: 0, seq: 10, query: "q", logKey: "q", trigger: "get", restart: false, inWindow: true, stored: false, empty: true }, { _tag: "Unmounted", ...stopped, path: "/App/Child", instance: 2, seq: 11 }, // A run that started while shown ends for a late viewer too. { _tag: "CommandStarted", ...at, path: "/App/Other", instance: 3, seq: 12, name: "Load", issued: { seq: 12, index: 0 }, spanId: "a", traceId: "b" }, { _tag: "CommandEnded", ...stopped, path: "/App/Other", instance: 3, seq: 13, name: "Load", issued: { seq: 12, index: 0 }, spanId: "a", traceId: "b", silent: false } ] ) socket.send(JSON.stringify(frame)) const state = yield* agent.call("get_state", { instance: 1 }).pipe( Effect.filterOrFail((state) => state.at === 13), Effect.retry(Schedule.spaced("10 millis")), Effect.timeout("5 seconds") ) assert.deepStrictEqual(state.queries, { a: 3, b: 4 }) const before = yield* agent.call("get_state", { instance: 1, at: 5 }) assert.deepStrictEqual(before.queries, { a: 3, b: 2 }) const watch = yield* connectSocket(url.replace("http", "ws") + "/watch") yield* Effect.addFinalizer(() => Effect.sync(() => watch.close())) const frames: Array = [] watch.addEventListener("message", (event) => { const frame = JSON.parse(String(event.data)) if (frame._tag !== "Held") frames.push(frame) }) yield* until(() => frames.some((frame) => frame.seq === 13)) const viewer = Inspector.make() viewer.ingest(frames) assert.strictEqual(viewer.sessions()[0]!.summary(1)?.unmounted, 8) assert.strictEqual(viewer.sessions()[0]!.summary(2)?.unmounted, 11) assert.strictEqual(viewer.sessions()[0]!.summary(3)?.running, 0) }).pipe(Effect.scoped), 30_000 ) it.live( "refuses browser origins on /mcp and foreign origins on the WebSocket routes", () => Effect.gen(function*() { const { agent, url } = yield* serve() const mcpStatus = yield* probeStatus( url + "/mcp", { method: "POST", headers: { "content-type": "application/json", accept: "application/json, text/event-stream", origin: "http://evil.example" } }, JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/list" }) ) assert.strictEqual(mcpStatus, 403) for (const path of ["/log", "/control"]) { const probe = (origin: string) => probeStatus(url + path, { headers: { connection: "Upgrade", upgrade: "websocket", "sec-websocket-version": "13", "sec-websocket-key": "dGhlIHNhbXBsZSBub25jZQ==", origin } }) assert.strictEqual(yield* probe("http://evil.example"), 403, path) assert.strictEqual(yield* probe("http://localhost:5173"), 101, path) } const message = yield* agent.fails("list_instances") assert.include(message, "No session has connected yet") }).pipe(Effect.scoped), 30_000 ) it.live( "shares one pause between an agent and the overlay", () => Effect.gen(function*() { const { agent, url } = yield* serve() const app = yield* open(url, "open") const root = overlay() const { rows } = yield* agent.call("list_entries", { tag: "ClickedSave" }) const seq = rows[0].seq const shown = yield* agent.call("show_entry", { seq }) assert.deepStrictEqual(shown, { paused: seq, fellBack: [], fallbacks: [], skipped: ["/Workspace/Editor[n1]", "/Workspace/Editor[n2]"] }) assert.strictEqual(app.style.display, "none") yield* until(() => text(root.querySelector(".paused-label")) === "Paused at #" + seq) const [paused] = yield* agent.call("list_sessions") assert.strictEqual(paused.paused, seq) yield* agent.call("resume") assert.notStrictEqual(app.style.display, "none") yield* until(() => root.querySelector(".paused") === null) HtmlLocator.click( HtmlLocator.getByRole(root, "button", { name: "Open the Effect UI inspector" }) ) yield* wait(20) const saves = HtmlLocator.queryAllByRole(root, "button", { name: /Update ClickedSave$/ }) .filter((node) => node.classList.contains("row-button")) HtmlLocator.click(saves.at(-1)!) yield* wait(20) HtmlLocator.click( HtmlLocator.getByRole(root, "button", { name: "Time travel" }) ) yield* wait(50) const [human] = yield* agent.call("list_sessions") assert.strictEqual( "Paused at #" + human.paused, text(root.querySelector(".paused-label")) ) yield* agent.call("resume") yield* until(() => root.querySelector(".paused") === null) const both: ReadonlyArray = rows.map((row: any) => row.seq) yield* Effect.all( both.map((seq) => agent.call("show_entry", { seq })), { concurrency: "unbounded" } ) const [raced] = yield* agent.call("list_sessions") assert.include(both, raced.paused) yield* until(() => text(root.querySelector(".paused-label")) === "Paused at #" + raced.paused ) assert.strictEqual( document.querySelectorAll("[style*=not-allowed]").length, 1 ) yield* agent.call("resume") yield* until(() => root.querySelector(".paused") === null) assert.strictEqual( document.querySelectorAll("[style*=not-allowed]").length, 0 ) const outside = yield* agent.fails("show_entry", { seq: 10_000 }) assert.include(outside, "not in the session's buffer") const off = yield* agent.fails("dispatch", { instance: 1, messages: [{ _tag: "ClickedSave" }] }) assert.include(off, "Dispatch is off") }).pipe(Effect.scoped), 30_000 ) it.live( "dispatches decoded messages to an instance and waits for their Commands", () => Effect.gen(function*() { const { agent, url } = yield* serve({ dispatch: true }) const app = yield* open(url, "open") const instances: ReadonlyArray = yield* agent.call( "list_instances" ) const n1 = instances.find((instance) => instance.path === "/Workspace/Editor[n1]").instance const schema = yield* agent.call("get_message_schema", { instance: n1, tag: "UpdatedDraft" }) assert.include(JSON.stringify(schema), "text") const allSchemas = yield* agent.call("get_message_schema", { instance: n1 }) assert.deepStrictEqual( allSchemas.examples.map((entry: any) => entry.message._tag), Object.keys(EditorMessage.cases) ) const decodeEditor = Schema.decodeUnknownSync( Schema.toCodecJson(EditorMessage) ) for (const entry of allSchemas.examples) { assert.isUndefined(entry.redacted) assert.isUndefined(entry.invalid) assert.doesNotThrow(() => decodeEditor(entry.message)) } const unknownTag = yield* agent.fails("get_message_schema", { instance: n1, tag: "Nope" }) assert.include(unknownTag, "ClickedSave") const bad = yield* agent.fails("dispatch", { instance: n1, messages: [{ _tag: "UpdatedDraft", text: "ok" }, { _tag: "UpdatedDraft", text: 1 }] }) assert.include(bad, "Message 1 does not decode") const { last } = yield* agent.call("wait", { timeout: 0, instance: n1 }) const sent = yield* agent.call("dispatch", { instance: n1, messages: [{ _tag: "UpdatedDraft", text: "From the agent" }, { _tag: "ClickedSave" }] }) assert.strictEqual(sent.applied, 2) assert.deepStrictEqual( sent.outcomes.map((outcome: any) => outcome._tag), ["Updated", "Updated"] ) const update = yield* agent.call("get_entry", { seq: sent.outcomes[0].seq }) assert.deepStrictEqual(update.source, { _tag: "Dispatch" }) assert.strictEqual( (HtmlLocator.getByRole(app, "textbox", { name: "Body n1" }) as HTMLTextAreaElement).value, "From the agent" ) const settled = yield* agent.call("wait", { since: last, instance: n1, timeout: 2_000 }) assert.isTrue(settled.settled) const state = yield* agent.call("get_state", { instance: n1 }) assert.strictEqual(state.model.status, "Saved") const pending = yield* agent.call("wait", { timeout: 50 }) assert.isFalse(pending.settled) assert.strictEqual(pending.open.length, 1) }).pipe(Effect.scoped), 30_000 ) it.live( "returns validated, redacted, and invalid examples from get_message_schema", () => Effect.gen(function*() { // The JSON form: the JSON Schema drops a Redacted example. const sampled = Schema.Struct({ token: Schema.Redacted(Schema.String) }) .pipe( Schema.annotateEncoded({ examples: [{ token: "sample" }] as never }) ) const SampleMessage = UiSchema.TaggedEnum({ SelectedItem: { index: Schema.Number, maybeLabel: Schema.Option(Schema.String), tags: Schema.Array(Schema.String) }, SavedCredential: { token: Schema.Redacted(Schema.String), email: Schema.String.check(Schema.isPattern(/^\S+@\S+$/)) }, Impossible: { never: Schema.Never }, // Decodes as -0, but the client receives 0. NegativeZero: { point: Schema.Struct({ x: Schema.Finite.check(Schema.makeFilter((n) => Object.is(n, -0))) }).pipe(Schema.annotateEncoded({ examples: [{ x: -0 }] as never })) }, Truncated: { data: Schema.Struct({ "~truncated": Schema.Int }) }, Sampled: { payload: sampled }, // Decodes, but can't be encoded to mark the token. Unencodable: { payload: sampled, extra: Schema.String.pipe(Schema.decodeTo(Schema.String, { decode: SchemaGetter.passthrough(), encode: SchemaGetter.forbidden(() => "no") })) } }).annotate({ identifier: "app/SampleMessage" }) const Sample = Html.component({ name: "Sample", model: Schema.Struct({ count: Schema.Number }), message: SampleMessage }).make({ init: () => ({ count: 0 }), update: ($) => ({ count: $.model.count + 1 }), view: (h, model) => h.p(String(model.count)) }) const { agent, url } = yield* serve() localStorage.clear() const services = yield* Layer.build(DevTools.layer({ server: url })) const app = document.body.appendChild(document.createElement("div")) yield* Effect.addFinalizer(() => Effect.sync(() => app.remove())) yield* BrowserHost.mountEffect(Sample, {}, app).pipe( Effect.provide(services) ) yield* wait(150) const [{ instance }] = yield* agent.call>( "list_instances" ) const all = yield* agent.call("get_message_schema", { instance }) assert.strictEqual(all.examples.length, 7) const [ selected, savedCredential, impossible, negativeZero, truncated, sampledEntry, unencodable ] = all.examples assert.deepStrictEqual(selected, { message: { _tag: "SelectedItem", index: 0, maybeLabel: { _tag: "Some", value: "" }, tags: [""] } }) const filtered = yield* agent.call("get_message_schema", { instance, tag: "SelectedItem" }) assert.deepStrictEqual(filtered.examples, [selected]) assert.deepStrictEqual(savedCredential.message, { _tag: "SavedCredential", token: { "~redacted": true }, email: "" }) assert.isTrue(savedCredential.redacted) assert.include(savedCredential.invalid, "email") assert.strictEqual(impossible.tag, "Impossible") assert.isUndefined(impossible.message) assert.isString(impossible.invalid) assert.deepStrictEqual(sampledEntry, { message: { _tag: "Sampled", payload: { token: { "~redacted": true } } }, redacted: true }) assert.strictEqual(unencodable.tag, "Unencodable") assert.isUndefined(unencodable.message) assert.notInclude(unencodable.invalid, "sample") assert.deepStrictEqual(negativeZero.message, { _tag: "NegativeZero", point: { x: 0 } }) assert.include(negativeZero.invalid, "x") assert.deepStrictEqual(truncated, { message: { _tag: "Truncated", data: { "~truncated": 0 } } }) }).pipe(Effect.scoped), 30_000 ) it.live( "keeps a closed session readable but not controllable", () => Effect.gen(function*() { const { agent, url } = yield* serve({ keep: 1 }) yield* open(url, "open").pipe(Effect.scoped) yield* wait(100) const [closed] = yield* agent.call("list_sessions") assert.isFalse(closed.connected) assert.isFalse(closed.controllable) const instances: ReadonlyArray = yield* agent.call( "list_instances" ) assert.isAbove(instances.length, 0) const message = yield* agent.fails("show_entry", { seq: closed.last }) assert.include(message, "can't be controlled") yield* open(url, "open").pipe(Effect.scoped) yield* open(url, "open").pipe(Effect.scoped) yield* wait(100) const sessions: ReadonlyArray = yield* agent.call("list_sessions") assert.strictEqual(sessions.length, 1) assert.notStrictEqual(sessions[0].session, closed.session) }).pipe(Effect.scoped), 30_000 ) it.live( "keeps a truncated query map opaque when a query changes", () => Effect.gen(function*() { const { agent, url } = yield* serve({ size: 3 }) const socket = new WebSocket(url.replace("http", "ws") + "/log") yield* Effect.callback((resume) => socket.addEventListener("open", () => resume(Effect.void))) const base = { session: "s", at: 0 } const at = { ...base, path: "/App", instance: 1, component: "App" } const frames = [ { _tag: "Hello", version: 1, host: "test" }, { _tag: "SessionStarted", ...base, seq: 0, app: "App" }, { _tag: "Mounted", ...at, seq: 1, props: {}, model: 0, queries: { "~truncated": 9000 }, commands: [], events: [], adopted: false }, { _tag: "QueryChanged", ...at, seq: 2, name: "todos", result: [1] }, { _tag: "PropsChanged", ...at, seq: 3, props: { a: 1 } }, { _tag: "PropsChanged", ...at, seq: 4, props: { a: 2 } }, { _tag: "PropsChanged", ...at, seq: 5, props: { a: 3 } } ] for (const frame of frames) socket.send(JSON.stringify(frame)) yield* wait(100) const state = yield* agent.call("get_state", { session: "s", instance: 1 }) assert.deepStrictEqual(state.queries, { "~truncated": 9000 }) assert.deepStrictEqual(state.props, { a: 3 }) socket.close() }).pipe(Effect.scoped), 30_000 ) it.live( "scrubs the root an agent paused when the page has two", () => Effect.gen(function*() { const { agent, url } = yield* serve() localStorage.clear() const services = yield* Layer.build(DevTools.layer({ server: url })) const older = yield* play("open").pipe(Effect.provide(services)) const newer = yield* play("open").pipe(Effect.provide(services)) yield* wait(150) const [first] = yield* agent.call("list_sessions") const { rows } = yield* agent.call("list_entries", { session: first.session, tag: "ClickedSave" }) yield* agent.call("show_entry", { session: first.session, seq: rows[0].seq }) assert.strictEqual(older.style.display, "none") const root = overlay() HtmlLocator.click( HtmlLocator.getByRole(root, "button", { name: "Open the Effect UI inspector" }) ) yield* wait(20) const slider = root.querySelector( "input[type=range]" )! assert.strictEqual( slider.getAttribute("aria-valuetext"), "#" + rows[0].seq ) slider.value = "0" slider.dispatchEvent(new Event("input", { bubbles: true })) yield* wait(50) const sessions: ReadonlyArray = yield* agent.call("list_sessions") const paused = sessions.find((session) => session.session === first.session).paused assert.isBelow(paused, rows[0].seq) assert.strictEqual(older.style.display, "none") assert.notStrictEqual(newer.style.display, "none") assert.strictEqual( document.querySelectorAll("[style*=not-allowed]").length, 1 ) }).pipe(Effect.scoped), 30_000 ) it.live( "keeps a pick the page reports before the session's first log frame", () => Effect.gen(function*() { const { agent, url } = yield* serveFast(300) const control = yield* connectSocket( url.replace("http", "ws") + "/control" ) const pick = { session: "s", tag: "button", instance: 1, path: "/App", component: "App" } control.send( JSON.stringify({ _tag: "State", sessions: ["s"], paused: null, notRecording: [], picks: [pick] }) ) yield* wait(100) const log = yield* connectSocket(url.replace("http", "ws") + "/log") log.send(JSON.stringify({ _tag: "Hello", version: 1, host: "test" })) log.send( JSON.stringify({ _tag: "SessionStarted", ...at, seq: 0, app: "App" }) ) yield* wait(100) assert.deepStrictEqual( yield* agent.call("get_picked", { session: "s" }), pick ) log.close() control.close() }).pipe(Effect.scoped), 30_000 ) it.live( "adds no meta tag when the plugin's port is taken, and injects overlay and hot modules unless disabled", () => Effect.gen(function*() { const port = yield* freePort yield* serve({ port }) const start = Effect.fnUntraced( function*(options: DevToolsVite.Options) { let close = () => {} const warnings: Array = [] const plugin = DevToolsVite.plugin(options) plugin.configureServer({ config: { logger: { info: () => {}, warn: (message) => warnings.push(message) } }, httpServer: { once: (_, listener) => close = listener }, middlewares: { use: () => {} }, transformIndexHtml: async (_, html) => html }) yield* Effect.addFinalizer(() => Effect.sync(close)) return { plugin, warnings } } ) const taken = yield* start({ port, overlay: { position: "TopLeft" } }) const takenTags = yield* Effect.promise(() => taken.plugin.transformIndexHtml()) assert.deepStrictEqual(takenTags, [ { tag: "script", attrs: { type: "module", src: "/@effect-ui/devtools" }, injectTo: "head-prepend" }, { tag: "script", attrs: { type: "module", src: "/@effect-ui/hot" }, injectTo: "head" } ]) const resolvedHot = taken.plugin.resolveId("/@effect-ui/hot") assert.isDefined(resolvedHot) assert.include( taken.plugin.load(resolvedHot!)!, "vite:beforeFullReload" ) const resolvedDevTools = taken.plugin.resolveId("/@effect-ui/devtools") assert.isDefined(resolvedDevTools) assert.include( taken.plugin.load(resolvedDevTools!)!, "DevTools.attach({\"position\":\"TopLeft\"})" ) assert.include( taken.warnings[0], "could not start on http://127.0.0.1:" + port ) const off = yield* start({ port, hot: false, overlay: false }) assert.deepStrictEqual( yield* Effect.promise(() => off.plugin.transformIndexHtml()), [] ) assert.isUndefined(off.plugin.resolveId("/@effect-ui/hot")) assert.isUndefined(off.plugin.resolveId("/@effect-ui/devtools")) const freed = yield* freePort const free = yield* start({ port: freed }) const tags = yield* Effect.promise(() => free.plugin.transformIndexHtml()) assert.deepStrictEqual(free.warnings, []) assert.strictEqual( JSON.parse(tags[0]!.attrs.content!).url, "http://127.0.0.1:" + freed ) assert.strictEqual(tags[1]!.attrs.src, "/@effect-ui/devtools") assert.strictEqual(tags[2]!.attrs.src, "/@effect-ui/hot") }).pipe(Effect.scoped), 30_000 ) it.live( "auto-attached DevTools captures initial mount before iframe load and yields to explicit DevTools.layer", () => Effect.gen(function*() { localStorage.clear() const dispose = DevTools.attach() yield* Effect.addFinalizer(() => Effect.sync(dispose)) yield* play("open") yield* until(() => document.querySelectorAll("iframe[data-effect-ui-devtools]") .length === 1 && overlay().querySelector("button") !== null ) const root = overlay() HtmlLocator.click( HtmlLocator.getByRole(root, "button", { name: "Open the Effect UI inspector" }) ) yield* until(() => text(root).includes("Workspace")) assert.strictEqual( document.querySelectorAll("iframe[data-effect-ui-devtools]").length, 1 ) yield* Layer.build(DevTools.layer()) yield* wait(20) assert.strictEqual( document.querySelectorAll("iframe[data-effect-ui-devtools]").length, 1 ) }).pipe(Effect.scoped), 30_000 ) it.live( "returns the outcomes of a batch that ran out of time, and what it didn't send", () => Effect.gen(function*() { const { agent, url } = yield* serveFast(1_000) const applied: Array = [] yield* fakePage(url, (log, message) => Effect.gen(function*() { const n = (message as { n: number }).n yield* Effect.sleep("300 millis") applied.push(n) log.send(propsAt(n + 2)) return { _tag: "Updated", seq: n + 2 } as const })) const sent = yield* agent.call("dispatch", { session: "s", instance: 1, messages: go(5) }) assert.deepStrictEqual(sent.outcomes, [ { _tag: "Updated", seq: 2 }, { _tag: "Updated", seq: 3 }, { _tag: "Unknown" } ]) assert.strictEqual(sent.applied, 2) assert.deepStrictEqual(sent.notSent, [3, 4]) assert.include(sent.note, "Message 2 was sent but did not finish") assert.include( sent.note, "Messages 3, 4 were not sent, because message 2's outcome is unknown." ) assert.isUndefined(sent.unseen) yield* wait(500) assert.deepStrictEqual(applied, [0, 1]) }).pipe(Effect.scoped), 30_000 ) it.live( "tells the agent any message may have applied when the page's reply is lost", () => Effect.gen(function*() { const { agent, url } = yield* serveFast(300) const log = yield* connectSocket(url.replace("http", "ws") + "/log") log.send(JSON.stringify({ _tag: "Hello", version: 1, host: "test" })) log.send( JSON.stringify({ _tag: "SessionStarted", ...at, seq: 0, app: "App" }) ) log.send(propsAt(1)) const control = yield* connectSocket( url.replace("http", "ws") + "/control" ) control.send( JSON.stringify({ _tag: "State", sessions: ["s"], paused: null, notRecording: [], picks: [] }) ) yield* wait(100) const message = yield* agent.fails("dispatch", { session: "s", instance: 1, messages: go(2) }) assert.include(message, "did not answer within 300ms") assert.include( message, "Any of the messages may have been applied, or none" ) assert.include(message, "after #1") log.close() control.close() }).pipe(Effect.scoped), 30_000 ) it.live( "accepts a pause from a page that reports no skipped mounts", () => Effect.gen(function*() { const { agent, url } = yield* serveFast(300) const log = yield* connectSocket(url.replace("http", "ws") + "/log") log.send(JSON.stringify({ _tag: "Hello", version: 1, host: "test" })) log.send( JSON.stringify({ _tag: "SessionStarted", ...at, seq: 0, app: "App" }) ) const control = yield* connectSocket( url.replace("http", "ws") + "/control" ) control.send( JSON.stringify({ _tag: "State", sessions: ["s"], paused: { session: "s", seq: 0, fellBack: [], fallbacks: [] }, notRecording: [], picks: [] }) ) yield* wait(100) const [session] = yield* agent.call("list_sessions") assert.strictEqual(session.paused, 0) log.close() control.close() }).pipe(Effect.scoped), 30_000 ) it.live( "tells a coalesced excluded update from a skipped one, and replays the kept one to a late viewer as the next diff's baseline", () => Effect.gen(function*() { const { agent, url } = yield* serveFast(1_000) const log = yield* fakePage(url, (log, message) => Effect.sync(() => { const seq = 2 + (message as { n: number }).n log.send(updateAt(seq, true)) return { _tag: "Updated", seq } as const })) const sent = yield* agent.call("dispatch", { session: "s", instance: 1, messages: go(2) }) assert.isUndefined(sent.unseen) assert.deepStrictEqual(sent.unavailable, [{ seq: 2, reason: "excluded" }]) log.send(updateAt(4, false)) yield* wait(100) const watch = yield* connectSocket( url.replace("http", "ws") + "/watch" ) yield* Effect.addFinalizer(() => Effect.sync(() => watch.close())) const frames: Array = [] watch.addEventListener("message", (event) => { const frame = JSON.parse(String(event.data)) if (frame._tag !== "Held") frames.push(frame) }) yield* until(() => frames.some((frame) => frame.seq === 4)) const viewer = Inspector.make() viewer.ingest(frames) assert.strictEqual(viewer.sessions()[0]!.previous(4)?.seq, 3) }).pipe(Effect.scoped), 30_000 ) it.live( "reports a dispatched entry the server will never hold when a later frame arrives, inside or before the kept window", () => Effect.gen(function*() { const { agent, url } = yield* serveFast(1_000, 1) let first = true yield* fakePage(url, (log) => { // Seq 2 is kept as the hidden baseline of 4, so 3 is inside the window. if (first) { first = false log.send(updateAt(2, true)) log.send(propsAt(4)) return Effect.succeed({ _tag: "Updated", seq: 3 } as const) } log.send(propsAt(5)) log.send(propsAt(6)) return Effect.as(wait(100), { _tag: "Updated", seq: 5 } as const) }) const sent = yield* agent.call("dispatch", { session: "s", instance: 1, messages: go(1) }) assert.deepStrictEqual(sent.outcomes, [{ _tag: "Updated", seq: 3 }]) assert.deepStrictEqual(sent.unavailable, [{ seq: 3, reason: "skipped" }]) assert.isUndefined(sent.unseen) assert.include( sent.note, "#3 were logged by the page, but the server can't show them" ) yield* agent.fails("get_entry", { session: "s", seq: 3 }) const shown = yield* agent.call("get_entry", { session: "s", seq: 4 }) assert.strictEqual(shown._tag, "PropsChanged") const old = yield* agent.call("dispatch", { session: "s", instance: 1, messages: go(1) }) assert.deepStrictEqual(old.unavailable, [{ seq: 5, reason: "evicted" }]) }).pipe(Effect.scoped), 30_000 ) it.live( "filters list_entries by declared and ad-hoc payload fields across instances", () => Effect.gen(function*() { localStorage.clear() const { agent, url } = yield* serve({ fields: ["commandId", "secret"] }) const services = yield* Layer.build(DevTools.layer({ server: url })) yield* playTwoWrites().pipe(Effect.provide(services)) yield* wait(150) const list = (name: string, value: unknown) => agent.call("list_entries", { field: { name, value } }) const cmd1 = yield* list("commandId", "cmd-1") assert.deepStrictEqual( cmd1.rows.map((row: any) => [row.tag, row.path, row.fields]).sort(), [ ["CommandEnded", "/Board", { commandId: "cmd-1" }], ["CommandStarted", "/Board", { commandId: "cmd-1" }], ["Update", "/Board/Feed[a]", { commandId: "cmd-1" }], ["Update", "/Board/Feed[b]", { commandId: "cmd-1" }] ] ) const byAttempt = yield* list("attempt", 1) assert.strictEqual(byAttempt.total, 4) assert.strictEqual(byAttempt.rows[0].fields.attempt, 1) assert.strictEqual((yield* list("attempt", "1")).total, 0) assert.strictEqual((yield* list("secret", secret)).total, 0) assert.notInclude(agent.results.join("\n"), secret) const fields = ["owner's id", "a&b", "$&"] const page = new DOMParser().parseFromString( Control.metaTag({ fields }), "text/html" ) assert.deepStrictEqual( JSON.parse(page.querySelector("meta")!.content).fields, fields ) }).pipe(Effect.scoped), 30_000 ) it.live( "finds owning instances by CSS selector without exposing element text, and returns the last picked element after the tab closes", () => Effect.gen(function*() { const { agent, url } = yield* serve() for (const name of ["find_instances", "get_picked"]) { assert.isTrue( agent.tools.find((tool) => tool.name === name)?.annotations ?.readOnlyHint ) } const decoy = document.body.insertBefore( document.createElement("button"), document.body.firstChild ) yield* Effect.addFinalizer(() => Effect.sync(() => decoy.remove())) const scope = yield* Scope.make() const app = yield* open(url, "open").pipe(Scope.provide(scope)) const [session] = yield* agent.call>("list_sessions") const instances: ReadonlyArray = yield* agent.call( "list_instances" ) const n1 = instances.find((instance) => instance.path === "/Workspace/Editor[n1]") // find_instances keeps only matches in the root, reports their // document.querySelectorAll index, and omits DOM text. const buttons = yield* agent.call("find_instances", { selector: "button" }) assert.strictEqual(buttons.total, 4) assert.deepStrictEqual(buttons.matches[0], { tag: "button", instance: n1.instance, path: "/Workspace/Editor[n1]", component: "Editor", index: 1 }) assert.notInclude(JSON.stringify(buttons), "Save n1") assert.notInclude(JSON.stringify(buttons), token) yield* agent.fails("find_instances", { selector: "[invalid" }) const extra = document.createElement("div") for (let i = 0; i < 25; i++) { extra.appendChild(document.createElement("span")).className = "many" } app.querySelector("section")!.appendChild(extra) const capped = yield* agent.call("find_instances", { selector: ".many" }) assert.strictEqual(capped.total, 25) assert.lengthOf(capped.matches, 20) // get_picked returns null before picking, then the picked metadata, // and still answers after the tab closes. assert.isNull(yield* agent.call("get_picked")) const root = overlay() HtmlLocator.click( HtmlLocator.getByRole(root, "button", { name: /pick/i }) ) yield* wait(20) HtmlLocator.click( HtmlLocator.getByRole(app, "button", { name: "Save n1" }) ) yield* wait(100) const picked = yield* agent.call("get_picked") assert.deepInclude(picked, { tag: "button", instance: n1.instance, path: "/Workspace/Editor[n1]", component: "Editor" }) assert.notInclude(JSON.stringify(picked), "Save n1") yield* Scope.close(scope, Exit.void) yield* wait(100) const afterClose = yield* agent.call("get_picked", { session: session.session }) assert.deepStrictEqual(afterClose, picked) }).pipe(Effect.scoped), 30_000 ) it.live("reports a root registered during the initial control snapshot", () => Effect.gen(function*() { const roots = new Map() const changes = yield* PubSub.sliding(1) const snapshots: Array> = [] const socket: Socket.WebSocketLike = { readyState: 1, addEventListener() {}, removeEventListener() {}, close() {}, send(data) { const message = Option.getOrThrow(Control.decodeToServer(data)) if (message._tag !== "State") return snapshots.push(message.sessions) if (snapshots.length === 1) { roots.set("s", {} as BrowserHost.Controllable) PubSub.publishUnsafe(changes, undefined) } } } yield* Remote.connect("ws://127.0.0.1:4774/control", { roots, size: 100, paused: () => undefined, paint: () => Effect.succeed(undefined), resume: Effect.void, notRecording: () => [], setRecording: () => {}, picks: () => [], changes }).pipe( Effect.andThen(until(() => snapshots.length === 2)), Effect.provideService(Socket.WebSocketConstructor, () => socket), Effect.scoped ) assert.deepStrictEqual(snapshots, [[], ["s"]]) PubSub.publishUnsafe(changes, undefined) assert.strictEqual(PubSub.sizeUnsafe(changes), 0) })) })