Open History to see the last commit
1import { expect, it } from "@effect/vitest";
2import {
3 DEFAULT_SERVER_SETTINGS,
4 EnvironmentId,
5 ProjectId,
6 ProviderInstanceId,
7 ThreadId,
8} from "@t3tools/contracts";
9import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
10import * as Effect from "effect/Effect";
11import * as Layer from "effect/Layer";
12import * as Stream from "effect/Stream";
13
14import * as Preview from "../../../preview/Manager.ts";
15import * as ServerSettings from "../../../serverSettings.ts";
16import * as McpInvocationContext from "../../McpInvocationContext.ts";
17import { PreviewControlsHandlersLive } from "./handlers.ts";
18import { PreviewControlsToolkit } from "./tools.ts";
19
20it.effect.each([
21 { name: "project opt-in", globalAccess: false, projectAccess: true },
22 { name: "project opt-out", globalAccess: true, projectAccess: false },
23])("list and close respect the credential for a $name", ({ globalAccess, projectAccess }) =>
24 Effect.scoped(
25 Effect.gen(function* () {
26 const projectId = ProjectId.make("preview-controls-project");
27 const threadId = ThreadId.make("preview-controls-thread");
28 const settings = {
29 ...DEFAULT_SERVER_SETTINGS,
30 enableAgentBrowserAccess: globalAccess,
31 projectSettingsOverrides: {
32 [projectId]: { enableAgentBrowserAccess: projectAccess },
33 },
34 };
35 const effective = resolveProjectSettings(settings, projectId).settings;
36 const scope: McpInvocationContext.McpInvocationScope = {
37 environmentId: EnvironmentId.make("preview-controls-environment"),
38 threadId,
39 providerSessionId: "preview-controls-provider-session",
40 providerInstanceId: ProviderInstanceId.make("codex"),
41 capabilities: new Set(effective.enableAgentBrowserAccess ? ["preview"] : []),
42 issuedAt: 0,
43 };
44 const manager = yield* Preview.make;
45 const tab = yield* manager.open({ threadId, url: "http://localhost:3000" });
46 const dependencies = Layer.mergeAll(
47 Layer.succeed(Preview.PreviewManager, manager),
48 Layer.succeed(McpInvocationContext.McpInvocationContext, scope),
49 Layer.mock(ServerSettings.ServerSettingsService)({
50 getSettings: Effect.succeed(settings),
51 }),
52 );
53 const toolkit = yield* PreviewControlsToolkit.pipe(
54 Effect.provide(PreviewControlsHandlersLive.pipe(Layer.provide(dependencies))),
55 );
56 const listed = yield* toolkit
57 .handle("t3_preview_list", {})
58 .pipe(Stream.unwrap, Stream.runCollect, Effect.provide(dependencies));
59 const closed = yield* toolkit
60 .handle("t3_preview_close", { tabId: tab.tabId })
61 .pipe(Stream.unwrap, Stream.runCollect, Effect.provide(dependencies));
62 if (projectAccess) {
63 expect(listed.at(-1)?.result).toMatchObject({ sessions: [tab], nextCursor: null });
64 expect(closed.at(-1)?.result).toEqual({});
65 expect((yield* manager.list({ threadId })).sessions).toEqual([]);
66 } else {
67 for (const result of [listed, closed]) {
68 expect(result.at(-1)?.result).toMatchObject({
69 _tag: "PreviewAutomationUnavailableError",
70 capability: "preview",
71 threadId,
72 });
73 }
74 expect((yield* manager.list({ threadId })).sessions).toEqual([tab]);
75 }
76 }),
77 ),
78);