跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

proxy-yaml-drop-smoke.mjs

171 行 · 9.6 KB历史下载
1// Frontend interaction checks with synthetic files and isolated API fixtures.
2import assert from "node:assert/strict";
3import fs from "node:fs/promises";
4import path from "node:path";
5import { createWorkbench } from "./workbench-fixture.mjs";
6
7const engine = process.env.CAGE_TEST_BROWSER || "chromium";
8const output = path.resolve(process.env.CAGE_YAML_QA_OUTPUT || "/tmp/cage-yaml-drop-qa", engine);
9await fs.mkdir(output, { recursive: true });
10const { browser, page, state, errors } = await createWorkbench({ viewport: { width: 1280, height: 1000 } });
11const yaml = "proxies:\n - {name: 本地测试, type: socks5, server: 127.0.0.1, port: 1080}\n";
12const proxy = { active: false, sources: [], port: 17896, core_available: true };
13const imports = [];
14try {
15 await page.route("**/api/proxy", (route) => route.fulfill({ json: proxy }));
16 await page.route("**/api/proxy/import", (route) => {
17 const body = route.request().postDataJSON();
18 imports.push(body);
19 const job = { id: "yaml-drop-import", running: false, message: "已导入" };
20 state.status.job = job;
21 proxy.sources.push({ id: "drop-source", name: body.name, nodes: [{ id: "drop-node", name: "本地测试", type: "socks5" }], remote: false, updated_at: new Date().toISOString() });
22 return route.fulfill({ json: job });
23 });
24 const open = async () => {
25 await page.getByRole("button", { name: /^设置/ }).click();
26 await page.getByRole("button", { name: "网络与代理", exact: true }).click();
27 await page.getByRole("tab", { name: "订阅", exact: true }).click();
28 await page.getByRole("button", { name: "添加来源", exact: true }).click();
29 await page.getByRole("button", { name: "本地 YAML", exact: true }).click();
30 };
31 await open();
32 const region = page.locator(".proxy-yaml-input");
33 const dropzone = page.locator(".proxy-yaml-dropzone");
34 const editor = page.getByRole("textbox", { name: "节点 YAML", exact: true });
35 const name = page.getByLabel("来源名称", { exact: true });
36 const submit = page.getByRole("button", { name: "导入并验证", exact: true });
37 const expectText = async (locator, value) => {
38 await locator.filter({ hasText: value }).waitFor();
39 };
40 const dataTransfer = async (files) => page.evaluateHandle((entries) => {
41 const transfer = new DataTransfer();
42 for (const entry of entries) transfer.items.add(new File([entry.content || ""], entry.name, { type: entry.type || "application/yaml" }));
43 return transfer;
44 }, files);
45 const drop = async (files, target = dropzone) => {
46 const transfer = await dataTransfer(files);
47 await target.dispatchEvent("dragenter", { dataTransfer: transfer });
48 await target.dispatchEvent("dragover", { dataTransfer: transfer });
49 await target.dispatchEvent("drop", { dataTransfer: transfer });
50 await transfer.dispose();
51 };
52 const payload = (filename = "日常代理.yaml", content = yaml) => ({ name: filename, mimeType: "application/yaml", buffer: Buffer.from(content) });
53 const fileInput = region.locator('input[type="file"]');
54 assert.equal(await submit.isDisabled(), true);
55 await dropzone.scrollIntoViewIfNeeded();
56 await page.screenshot({ path: path.join(output, "empty.png"), fullPage: true });
57
58 // Keyboard activation reaches the actual file input, including same-file reselection.
59 await dropzone.focus();
60 const firstChooser = page.waitForEvent("filechooser");
61 await dropzone.press("Enter");
62 await (await firstChooser).setFiles(payload());
63 await expectText(region.getByRole("status"), "文件已读取");
64 assert.equal(await editor.inputValue(), yaml);
65 assert.equal(await name.inputValue(), "日常代理");
66 assert.equal(await submit.isEnabled(), true);
67 await editor.fill(yaml + "# 手动编辑\n");
68 await expectText(region.getByRole("status"), "内容已编辑");
69 await dropzone.focus();
70 const secondChooser = page.waitForEvent("filechooser");
71 await dropzone.press("Space");
72 await (await secondChooser).setFiles(payload());
73 await page.waitForFunction((expected) => document.querySelector('[aria-label="节点 YAML"]').value === expected, yaml);
74 await name.fill("我的来源");
75
76 // Nested drag events must keep the highlight until the pointer leaves the region.
77 const transfer = await dataTransfer([{ name: "替换.YML", content: yaml + "# 拖入\n" }]);
78 await dropzone.dispatchEvent("dragenter", { dataTransfer: transfer });
79 await dropzone.locator("strong").dispatchEvent("dragenter", { dataTransfer: transfer });
80 await dropzone.dispatchEvent("dragleave", { dataTransfer: transfer });
81 assert.match(await region.getAttribute("class"), /is-dragging/);
82 await expectText(dropzone, "松开以读取配置文件");
83 await page.screenshot({ path: path.join(output, "dragging.png"), fullPage: true });
84 await editor.dispatchEvent("drop", { dataTransfer: transfer });
85 await transfer.dispose();
86 await expectText(dropzone, "替换.YML");
87 assert.equal(await editor.inputValue(), yaml + "# 拖入\n");
88 assert.equal(await name.inputValue(), "我的来源", "replacement preserves the explicit source name");
89 assert.doesNotMatch(await region.getAttribute("class"), /is-dragging/);
90
91 const preserved = await editor.inputValue();
92 for (const [files, message] of [
93 [[{ name: "image.png", content: "image" }], ".yaml、.yml 或 .json"],
94 [[{ name: "a.yaml", content: yaml }, { name: "b.yml", content: yaml }], "一次只能导入一个"],
95 [[{ name: "empty.yaml", content: " " }], "文件内容为空"],
96 ]) {
97 await drop(files);
98 await expectText(region.getByRole("alert"), message);
99 assert.equal(await editor.inputValue(), preserved);
100 }
101 await fileInput.setInputFiles(payload("large.yaml", "x".repeat(8 * 1024 * 1024 + 1)));
102 await expectText(region.getByRole("alert"), "文件超过 8 MiB");
103 assert.equal(await editor.inputValue(), preserved);
104 await page.screenshot({ path: path.join(output, "error.png"), fullPage: true });
105 await page.evaluate(() => {
106 window.originalFileText = File.prototype.text;
107 File.prototype.text = function () { return Promise.reject(new Error("Synthetic read failure")); };
108 });
109 await drop([{ name: "unreadable.yaml", content: yaml }]);
110 await expectText(region.getByRole("alert"), "文件读取失败");
111 assert.equal(await editor.inputValue(), preserved);
112 await page.evaluate(() => { File.prototype.text = window.originalFileText; });
113
114 // A newer file must win even if an earlier asynchronous read completes later.
115 await page.evaluate(() => {
116 File.prototype.text = function () {
117 if (this.name === "slow.yaml") return new Promise((resolve) => { window.finishSlowRead = resolve; });
118 return window.originalFileText.call(this);
119 };
120 });
121 await drop([{ name: "slow.yaml", content: yaml }]);
122 await expectText(region.getByRole("status"), "正在读取");
123 assert.equal(await submit.isDisabled(), true);
124 await drop([{ name: "最新.json", content: '{"proxies": []}' }]);
125 await expectText(dropzone, "最新.json");
126 await page.evaluate(() => window.finishSlowRead("stale content"));
127 assert.equal(await editor.inputValue(), '{"proxies": []}');
128 await drop([{ name: "slow.yaml", content: yaml }]);
129 await expectText(region.getByRole("status"), "正在读取");
130 await page.getByRole("button", { name: "订阅链接", exact: true }).click();
131 await page.evaluate(() => window.finishSlowRead("stale content"));
132 await page.getByRole("button", { name: "本地 YAML", exact: true }).click();
133 assert.equal(await editor.inputValue(), "");
134 await page.evaluate(() => { File.prototype.text = window.originalFileText; });
135
136 const origin = page.url();
137 await drop([{ name: "outside.yaml", content: yaml }], page.locator("h4").filter({ hasText: "添加节点来源" }));
138 assert.equal(page.url(), origin);
139 assert.equal(await editor.inputValue(), "");
140 await drop([{ name: "最终配置.yaml", content: yaml }]);
141 await expectText(dropzone, "最终配置.yaml");
142 await editor.fill(yaml + "# 已编辑\n");
143 await page.locator(".proxy-import").evaluate((element) => element.scrollIntoView({ block: "start" }));
144 await page.screenshot({ path: path.join(output, "selected.png"), fullPage: true });
145 await page.locator(".proxy-import").screenshot({ path: path.join(output, "preview.png") });
146
147 // Test the component's narrow layout in the existing settings shell.
148 await page.setViewportSize({ width: 375, height: 1000 });
149 await dropzone.scrollIntoViewIfNeeded();
150 const box = await dropzone.boundingBox();
151 assert.ok(box.x >= 0 && box.x + box.width <= 376, "dropzone stays within the narrow viewport");
152 await page.screenshot({ path: path.join(output, "narrow.png"), fullPage: true });
153 await page.setViewportSize({ width: 1280, height: 1000 });
154 await page.emulateMedia({ colorScheme: "dark" });
155 await page.waitForFunction(() => document.documentElement.dataset.theme === "dark");
156 await page.locator(".proxy-import").evaluate((element) => element.scrollIntoView({ block: "start" }));
157 await page.screenshot({ path: path.join(output, "dark.png"), fullPage: true });
158 await submit.click();
159 await expectText(page.locator(".proxy-notice"), "已导入 1 个节点");
160 assert.equal(imports.length, 1);
161 assert.equal(imports[0].content, yaml + "# 已编辑\n");
162 assert.equal(imports[0].name, "我的来源");
163 assert.equal(imports[0].interval_hours, 0);
164 await expectText(page.locator(".proxy-source"), "我的来源");
165 assert.deepEqual(errors, []);
166 const result = { engine, passed: true, imports: imports.length, browserErrors: errors, scope: "Synthetic browser file drops and file chooser; mocked API. Native OS drag and backend validation are outside this check." };
167 await fs.writeFile(path.join(output, "result.json"), JSON.stringify(result, null, 2));
168 console.log(JSON.stringify(result));
169} finally {
170 await browser.close();
171}