Open History to see the last commit
1import { Schema } from "effect";
2import { describe, expect, it } from "vite-plus/test";
3
4import { EnvironmentId, ThreadId } from "./baseSchemas.ts";
5import { truncatePreviewTitle } from "./preview.ts";
6import {
7 PreviewAutomationHost,
8 PreviewAutomationInvalidSelectorError,
9 PreviewAutomationPressInput,
10 PreviewAutomationStatus,
11 PreviewAutomationUploadInput,
12 PreviewAutomationWaitForInput,
13} from "./previewAutomation.ts";
14import { ProviderInstanceId } from "./providerInstance.ts";
15
16const decodeAutomationHost = Schema.decodeUnknownSync(PreviewAutomationHost);
17const decodeAutomationStatus = Schema.decodeUnknownSync(PreviewAutomationStatus);
18const decodePressInput = Schema.decodeUnknownSync(PreviewAutomationPressInput);
19const decodeUploadInput = Schema.decodeUnknownSync(PreviewAutomationUploadInput);
20const decodeWaitForInput = Schema.decodeUnknownSync(PreviewAutomationWaitForInput);
21const encodeWaitForInput = Schema.encodeSync(PreviewAutomationWaitForInput);
22
23describe("PreviewAutomationHost", () => {
24 it("accepts legacy hosts and current operation advertisements", () => {
25 expect(decodeAutomationHost({ clientId: "legacy", environmentId: "environment-1" })).toEqual({
26 clientId: "legacy",
27 environmentId: "environment-1",
28 });
29 expect(
30 decodeAutomationHost({
31 clientId: "current",
32 environmentId: "environment-1",
33 supportedOperations: ["status", "resize"],
34 }).supportedOperations,
35 ).toEqual(["status", "resize"]);
36 });
37});
38
39describe("PreviewAutomationStatus", () => {
40 it("accepts old hosts without viewport data and exposes it from current hosts", () => {
41 const base = {
42 available: true,
43 visible: false,
44 tabId: "preview-t",
45 url: "https://example.com",
46 title: "Example",
47 loading: false,
48 };
49 expect(decodeAutomationStatus(base)).toEqual(base);
50 expect(
51 decodeAutomationStatus({
52 ...base,
53 viewportSetting: { _tag: "preset", presetId: "pixel-8", width: 412, height: 915 },
54 viewport: { width: 412, height: 915 },
55 }).viewport,
56 ).toEqual({ width: 412, height: 915 });
57 });
58});
59
60describe("PreviewAutomationUploadInput", () => {
61 it("keeps trailing whitespace in upload paths", () => {
62 expect(decodeUploadInput({ paths: ["/tmp/t3-upload/report.txt "] }).paths).toEqual([
63 "/tmp/t3-upload/report.txt ",
64 ]);
65 expect(() => decodeUploadInput({ paths: [""] })).toThrow();
66 });
67});
68
69describe("PreviewAutomationPressInput", () => {
70 it("accepts the single-space key while rejecting empty and padded keys", () => {
71 expect(decodePressInput({ key: " " })).toEqual({ key: " " });
72 expect(decodePressInput({ key: " ", modifiers: ["Shift"] })).toEqual({
73 key: " ",
74 modifiers: ["Shift"],
75 });
76 expect(decodePressInput({ key: "Enter" })).toEqual({ key: "Enter" });
77 for (const key of ["", " ", "\t", " Enter ", " a", "a "]) {
78 expect(() => decodePressInput({ key })).toThrow();
79 }
80 });
81});
82
83describe("PreviewAutomationWaitForInput", () => {
84 it("keeps edge whitespace in wait text while rejecting blank text", () => {
85 const input = { text: "READY ", timeoutMs: 60 };
86 expect(decodeWaitForInput(input)).toEqual(input);
87 expect(encodeWaitForInput(input)).toEqual(input);
88 for (const text of ["", " "]) {
89 expect(() => decodeWaitForInput({ text })).toThrow();
90 }
91 });
92});
93
94describe("PreviewAutomationInvalidSelectorError", () => {
95 const invalidSelector = (selectorKind: "locator" | "selector") =>
96 new PreviewAutomationInvalidSelectorError({
97 operation: "click",
98 environmentId: EnvironmentId.make("environment-1"),
99 threadId: ThreadId.make("thread-1"),
100 providerSessionId: "session-1",
101 providerInstanceId: ProviderInstanceId.make("claude"),
102 clientId: "desktop",
103 connectionId: "connection-1",
104 requestId: "request-1",
105 timeoutMs: 15_000,
106 remoteTag: "Error",
107 remoteMessageLength: 0,
108 cause: null,
109 selectorKind,
110 selectorLength: 42,
111 });
112
113 it("tells agents which locator forms are accepted and where CSS goes", () => {
114 const { message } = invalidSelector("locator");
115 expect(message).toContain("aria-ref=");
116 expect(message).toMatch(/role/);
117 expect(message).toMatch(/text/);
118 expect(message).toMatch(/CSS[^.]*\bselector\b/);
119 });
120
121 it("points an invalid CSS selector at the locator alternative", () => {
122 const { message } = invalidSelector("selector");
123 expect(message).toMatch(/CSS/);
124 expect(message).toMatch(/\blocator\b/);
125 expect(message).toContain("aria-ref=");
126 });
127});
128
129describe("truncatePreviewTitle", () => {
130 const flag = "\uD83C\uDDEF\uD83C\uDDF5";
131
132 it("does not leave the first half of a flag", () => {
133 expect(truncatePreviewTitle(`${"a".repeat(510)}${flag}END`)).toBe("a".repeat(510));
134 expect(truncatePreviewTitle(`${"a".repeat(508)}${flag}END`)).toBe(`${"a".repeat(508)}${flag}`);
135 });
136});