@@ -4,6 +4,7 @@ import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; |
4 | 4 | import { expect, it } from "@effect/vitest"; |
5 | 5 | import * as C from "@t3tools/contracts"; |
6 | 6 | import * as Context from "effect/Context"; |
| 7 | +import * as Cause from "effect/Cause"; |
7 | 8 | import * as Clock from "effect/Clock"; |
8 | 9 | import * as Deferred from "effect/Deferred"; |
9 | 10 | import * as Effect from "effect/Effect"; |
@@ -28,6 +29,8 @@ import * as Broker from "../mcp/PreviewAutomationBroker.ts"; |
28 | 29 | import * as HtmlRender from "../htmlRender/HtmlRender.ts"; |
29 | 30 | import * as RemotePreview from "./RemoteWorkspacePreview.ts"; |
30 | 31 | import * as RuntimeClient from "./RuntimeClient.ts"; |
| 32 | +import * as Bindings from "./Bindings.ts"; |
| 33 | +import * as RuntimeSchema from "./Schema.ts"; |
31 | 34 | import { makePreviewExecutor } from "./worker/PreviewExecutor.ts"; |
32 | 35 | import { openWorkerJournal } from "../execution/WorkerJournal.ts"; |
33 | 36 | import { createExecutionWorker } from "../execution/ExecutionWorker.ts"; |
@@ -44,6 +47,103 @@ const layer = Layer.mergeAll( |
44 | 47 | ServerConfig.layerTest(process.cwd(), { prefix: "remote-preview-" }), |
45 | 48 | ).pipe(Layer.provideMerge(NodeServices.layer)); |
46 | 49 | it.layer(layer, { excludeTestServices: true })("remote browser transport", (it) => { |
| 50 | + it.effect("lists an unbound draft without observing or acquiring compute", () => |
| 51 | + Effect.gen(function* () { |
| 52 | + yield* RuntimeSchema.migrate; |
| 53 | + const sql = yield* SqlClient.SqlClient; |
| 54 | + const threadId = C.ThreadId.make("unbound-draft"); |
| 55 | + const local = yield* PreviewManager.PreviewManager; |
| 56 | + const expected = yield* local.list({ threadId }); |
| 57 | + let observations = 0; |
| 58 | + const remote = yield* PreviewManager.PreviewManager.pipe( |
| 59 | + Effect.provide( |
| 60 | + RemotePreview.managerLayer.pipe( |
| 61 | + Layer.provide( |
| 62 | + Layer.mock(RuntimeClient.RuntimeClient, { |
| 63 | + enabled: true, |
| 64 | + observeThread: (id) => { |
| 65 | + observations++; |
| 66 | + return Bindings.read(id).pipe( |
| 67 | + Effect.provideService(SqlClient.SqlClient, sql), |
| 68 | + Effect.mapError( |
| 69 | + (cause) => |
| 70 | + new RuntimeClient.RuntimeClientError({ |
| 71 | + operation: "observe-thread", |
| 72 | + cause, |
| 73 | + }), |
| 74 | + ), |
| 75 | + Effect.andThen(Effect.die("Unexpected draft binding")), |
| 76 | + ); |
| 77 | + }, |
| 78 | + }), |
| 79 | + ), |
| 80 | + ), |
| 81 | + ), |
| 82 | + ); |
| 83 | + const result = yield* remote.list({ threadId }); |
| 84 | + expect(result.sessions).toEqual(expected.sessions); |
| 85 | + expect(result.revision).toBe(0); |
| 86 | + expect(result.serverEpoch).toBeTruthy(); |
| 87 | + expect(observations).toBe(0); |
| 88 | + }), |
| 89 | + ); |
| 90 | + |
| 91 | + it.effect.each(["observation", "transport"] as const)( |
| 92 | + "preserves %s failures for a bound thread", |
| 93 | + (failure) => |
| 94 | + Effect.gen(function* () { |
| 95 | + yield* RuntimeSchema.migrate; |
| 96 | + const sql = yield* SqlClient.SqlClient; |
| 97 | + const threadId = C.ThreadId.make(`preview-${failure}-failure`); |
| 98 | + const workspaceId = `preview-${failure}-workspace`; |
| 99 | + yield* sql`INSERT INTO agent_runtime_workspaces(workspace_id, owner_thread_id) |
| 100 | + VALUES (${workspaceId}, ${threadId})`; |
| 101 | + yield* sql`INSERT INTO agent_runtime_thread_bindings(thread_id, workspace_id) |
| 102 | + VALUES (${threadId}, ${workspaceId})`; |
| 103 | + const client = createExecutionClient({ |
| 104 | + identity: { |
| 105 | + workspaceId, |
| 106 | + allocationId: "one", |
| 107 | + generation: 1, |
| 108 | + incarnation: "one", |
| 109 | + podUid: "one", |
| 110 | + protocolVersion: 1, |
| 111 | + }, |
| 112 | + token: "preview-test-token", |
| 113 | + endpoint: "http://worker.test", |
| 114 | + fetch: async () => new Response("Unauthorized", { status: 401 }), |
| 115 | + }); |
| 116 | + const remote = yield* PreviewManager.PreviewManager.pipe( |
| 117 | + Effect.provide( |
| 118 | + RemotePreview.managerLayer.pipe( |
| 119 | + Layer.provide( |
| 120 | + Layer.mock(RuntimeClient.RuntimeClient, { |
| 121 | + enabled: true, |
| 122 | + observeThread: () => |
| 123 | + failure === "transport" |
| 124 | + ? Effect.succeed({ kind: "running", client }) |
| 125 | + : Effect.fail( |
| 126 | + new RuntimeClient.RuntimeClientError({ |
| 127 | + operation: "observe-thread", |
| 128 | + cause: "Controller authorization failed", |
| 129 | + }), |
| 130 | + ), |
| 131 | + }), |
| 132 | + ), |
| 133 | + ), |
| 134 | + ), |
| 135 | + ); |
| 136 | + const result = yield* Effect.exit(remote.list({ threadId })); |
| 137 | + expect(Exit.isFailure(result)).toBe(true); |
| 138 | + if (Exit.isFailure(result)) |
| 139 | + expect(Cause.pretty(result.cause)).toContain( |
| 140 | + failure === "transport" |
| 141 | + ? "Worker transport authorization" |
| 142 | + : "Controller authorization failed", |
| 143 | + ); |
| 144 | + }), |
| 145 | + ); |
| 146 | + |
47 | 147 | it.effect( |
48 | 148 | "preserves viewer input order across delayed HTTP without blocking dialog or another viewer", |
49 | 149 | () => |
|