@firstpick/pi-package-webui 0.6.3 → 0.6.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/pi-webui.mjs +1522 -53
- package/lib/git-worktrees.mjs +42 -0
- package/lib/trust-boundaries.mjs +4 -0
- package/package.json +3 -3
- package/public/app.js +3048 -164
- package/public/index.html +91 -3
- package/public/styles.css +991 -25
- package/tests/fixtures/fake-pi.mjs +59 -3
- package/tests/http-endpoints-harness.test.mjs +602 -2
- package/tests/mobile-static.test.mjs +3 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
2
|
import { spawn, spawnSync } from "node:child_process";
|
|
3
3
|
import { createServer } from "node:http";
|
|
4
|
-
import { chmod, mkdir, mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises";
|
|
4
|
+
import { chmod, mkdir, mkdtemp, readFile, rm, stat, symlink, writeFile } from "node:fs/promises";
|
|
5
5
|
import { networkInterfaces, tmpdir } from "node:os";
|
|
6
6
|
import path from "node:path";
|
|
7
7
|
import { setTimeout as delay } from "node:timers/promises";
|
|
@@ -53,6 +53,10 @@ async function rmWithRetry(target) {
|
|
|
53
53
|
throw lastError;
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
+
async function pathExists(target) {
|
|
57
|
+
return !!(await stat(target).catch(() => null));
|
|
58
|
+
}
|
|
59
|
+
|
|
56
60
|
function runGitFixture(args, cwd, message) {
|
|
57
61
|
const result = spawnSync("git", args, {
|
|
58
62
|
cwd,
|
|
@@ -70,8 +74,19 @@ function runGitFixture(args, cwd, message) {
|
|
|
70
74
|
}
|
|
71
75
|
|
|
72
76
|
const cwd = await mkdtemp(path.join(tmpdir(), "pi-webui-http-harness-"));
|
|
73
|
-
const
|
|
77
|
+
const harnessSideEffectsRoot = await mkdtemp(path.join(tmpdir(), "pi-webui-http-harness-side-effects-"));
|
|
78
|
+
const settingsFile = path.join(harnessSideEffectsRoot, "webui-settings.json");
|
|
79
|
+
const openCommandLog = path.join(harnessSideEffectsRoot, "open-default.log");
|
|
80
|
+
const openCommandScript = path.join(harnessSideEffectsRoot, "fake-open-default.mjs");
|
|
81
|
+
const fakeOpenBinDir = path.join(harnessSideEffectsRoot, "bin");
|
|
74
82
|
await chmod(fakePi, 0o755);
|
|
83
|
+
await mkdir(fakeOpenBinDir, { recursive: true });
|
|
84
|
+
await writeFile(openCommandScript, `#!/usr/bin/env node\nimport { appendFile } from "node:fs/promises";\nawait appendFile(process.env.PI_WEBUI_OPEN_LOG, "custom-open\\t" + process.argv.slice(2).join("\\t") + "\\n", "utf8");\n`, "utf8");
|
|
85
|
+
await chmod(openCommandScript, 0o755);
|
|
86
|
+
await writeFile(path.join(fakeOpenBinDir, "xdg-open"), `#!/usr/bin/env node\nimport { appendFile } from "node:fs/promises";\nawait appendFile(process.env.PI_WEBUI_OPEN_LOG, "xdg-open\\t" + process.argv.slice(2).join("\\t") + "\\n", "utf8");\n`, "utf8");
|
|
87
|
+
await writeFile(path.join(fakeOpenBinDir, "gio"), `#!/usr/bin/env node\nimport { appendFile } from "node:fs/promises";\nawait appendFile(process.env.PI_WEBUI_OPEN_LOG, "gio\\t" + process.argv.slice(2).join("\\t") + "\\n", "utf8");\n`, "utf8");
|
|
88
|
+
await writeFile(path.join(fakeOpenBinDir, "xdg-mime"), `#!/usr/bin/env node\nconst [,, verb, mode, value = ""] = process.argv;\nif (verb === "query" && mode === "filetype") {\n if (value.endsWith(".piunknown")) console.log("application/x-pi-unknown");\n else if (value.endsWith(".md")) console.log("text/markdown");\n else console.log("text/plain");\n process.exit(0);\n}\nif (verb === "query" && mode === "default") {\n if (value === "text/plain") console.log("fake-text-editor.desktop");\n process.exit(0);\n}\nprocess.exit(1);\n`, "utf8");
|
|
89
|
+
await Promise.all(["xdg-open", "gio", "xdg-mime"].map((name) => chmod(path.join(fakeOpenBinDir, name), 0o755)));
|
|
75
90
|
|
|
76
91
|
const voiceProviderRequests = [];
|
|
77
92
|
const voiceProvider = createServer(async (req, res) => {
|
|
@@ -103,7 +118,12 @@ const child = spawn(process.execPath, [serverScript, "--cwd", cwd, "--host", "0.
|
|
|
103
118
|
GIT_AUTHOR_EMAIL: "pi-webui-test@example.invalid",
|
|
104
119
|
GIT_COMMITTER_NAME: "Pi WebUI Test",
|
|
105
120
|
GIT_COMMITTER_EMAIL: "pi-webui-test@example.invalid",
|
|
121
|
+
PATH: `${fakeOpenBinDir}${path.delimiter}${process.env.PATH || ""}`,
|
|
122
|
+
PI_CODING_AGENT_DIR: path.join(harnessSideEffectsRoot, "agent"),
|
|
106
123
|
PI_WEBUI_SETTINGS_FILE: settingsFile,
|
|
124
|
+
...(process.platform === "linux" ? {} : { PI_WEBUI_OPEN_COMMAND: openCommandScript }),
|
|
125
|
+
PI_WEBUI_OPEN_LOG: openCommandLog,
|
|
126
|
+
FAKE_PI_VOICE_SCRIPTS: "1",
|
|
107
127
|
PI_VOICE_STT_URL: `http://127.0.0.1:${voiceProviderPort}/stt`,
|
|
108
128
|
PI_VOICE_TTS_URL: `http://127.0.0.1:${voiceProviderPort}/tts`,
|
|
109
129
|
},
|
|
@@ -201,6 +221,45 @@ try {
|
|
|
201
221
|
assert.equal(state.status, 200);
|
|
202
222
|
assert.equal(state.body?.data?.model?.provider, "fake", "state should come from the fake pi RPC");
|
|
203
223
|
|
|
224
|
+
const forkSourceTab = await request("127.0.0.1", "/api/tabs", { method: "POST", body: { cwd, title: "fork-running-source" } });
|
|
225
|
+
assert.equal(forkSourceTab.status, 201, `fork source tab should open: ${forkSourceTab.body?.error || ""}`);
|
|
226
|
+
const forkSourceTabId = forkSourceTab.body?.data?.tab?.id;
|
|
227
|
+
assert.ok(forkSourceTabId, "fork source tab should have an id");
|
|
228
|
+
const runningPrompt = "voice test slow fork fixture";
|
|
229
|
+
const runningPromptResponse = await request("127.0.0.1", "/api/prompt", { method: "POST", body: { message: runningPrompt, tab: forkSourceTabId }, timeoutMs: 10_000 });
|
|
230
|
+
assert.equal(runningPromptResponse.status, 200, `slow scripted prompt should start: ${runningPromptResponse.body?.error || ""}`);
|
|
231
|
+
let streamingState;
|
|
232
|
+
for (let attempt = 0; attempt < 30; attempt++) {
|
|
233
|
+
streamingState = await request("127.0.0.1", `/api/state?tab=${encodeURIComponent(forkSourceTabId)}`);
|
|
234
|
+
if (streamingState.body?.data?.isStreaming === true) break;
|
|
235
|
+
await delay(100);
|
|
236
|
+
}
|
|
237
|
+
assert.equal(streamingState?.body?.data?.isStreaming, true, "source tab should still be running before fork");
|
|
238
|
+
const forkMessagesWhileRunning = await request("127.0.0.1", `/api/fork-messages?tab=${encodeURIComponent(forkSourceTabId)}`);
|
|
239
|
+
assert.equal(forkMessagesWhileRunning.status, 200, "fork selector data should load while the source tab is running");
|
|
240
|
+
const runningForkPoint = (forkMessagesWhileRunning.body?.data?.messages || []).find((item) => item.text === runningPrompt);
|
|
241
|
+
assert.ok(runningForkPoint?.entryId, "running prompt should be available as a fork point");
|
|
242
|
+
const forkWhileRunning = await request("127.0.0.1", "/api/fork", { method: "POST", body: { tab: forkSourceTabId, entryId: runningForkPoint.entryId }, timeoutMs: 10_000 });
|
|
243
|
+
assert.equal(forkWhileRunning.status, 200, `fork while running should succeed: ${forkWhileRunning.body?.error || ""}`);
|
|
244
|
+
const forkedTabId = forkWhileRunning.body?.data?.tab?.id;
|
|
245
|
+
assert.ok(forkedTabId, "fork response should include the opened fork tab");
|
|
246
|
+
assert.notEqual(forkedTabId, forkSourceTabId, "forking should create a new tab instead of replacing the running source tab");
|
|
247
|
+
assert.equal(forkWhileRunning.body?.data?.text, runningPrompt, "fork response should restore the selected prompt text for editing");
|
|
248
|
+
assert.ok((forkWhileRunning.body?.data?.tabs || []).some((tab) => tab.id === forkSourceTabId), "fork response should keep the original running tab in the tab list");
|
|
249
|
+
assert.ok((forkWhileRunning.body?.data?.tabs || []).some((tab) => tab.id === forkedTabId), "fork response should include the new fork tab in the tab list");
|
|
250
|
+
assert.match(String(forkWhileRunning.body?.data?.sessionFile || ""), /\.jsonl$/, "fork response should include the new session file");
|
|
251
|
+
const forkSessionContent = await readFile(forkWhileRunning.body.data.sessionFile, "utf8");
|
|
252
|
+
assert.match(forkSessionContent, /"type":"session"/, "forked session file should be written before opening its tab");
|
|
253
|
+
const sourceAfterFork = await request("127.0.0.1", `/api/state?tab=${encodeURIComponent(forkSourceTabId)}`);
|
|
254
|
+
assert.equal(sourceAfterFork.status, 200, "source tab should still respond after forking");
|
|
255
|
+
for (let attempt = 0; attempt < 40 && sourceAfterFork.body?.data?.isStreaming; attempt++) {
|
|
256
|
+
const next = await request("127.0.0.1", `/api/state?tab=${encodeURIComponent(forkSourceTabId)}`);
|
|
257
|
+
if (!next.body?.data?.isStreaming) break;
|
|
258
|
+
await delay(100);
|
|
259
|
+
}
|
|
260
|
+
const closeForkTestTabs = await request("127.0.0.1", "/api/tabs/close", { method: "POST", body: { ids: [forkSourceTabId, forkedTabId] }, timeoutMs: 10_000 });
|
|
261
|
+
assert.equal(closeForkTestTabs.status, 200, "fork test tabs should close before continuing baseline endpoint checks");
|
|
262
|
+
|
|
204
263
|
const gitAvailable = spawnSync("git", ["--version"], { encoding: "utf8" }).status === 0;
|
|
205
264
|
if (gitAvailable) {
|
|
206
265
|
const gitInit = await request("127.0.0.1", "/api/git-workflow/init", { method: "POST", body: { tab: tabId } });
|
|
@@ -225,6 +284,21 @@ try {
|
|
|
225
284
|
assert.equal(gitReadmeAgain.body?.data?.readme?.created, false);
|
|
226
285
|
assert.equal(gitReadmeAgain.body?.data?.gitignore?.created, false);
|
|
227
286
|
|
|
287
|
+
const bypassCommit = await request("127.0.0.1", `/api/git-workflow/initial-commit?tab=${encodeURIComponent(tabId)}`);
|
|
288
|
+
assert.equal(bypassCommit.status, 405, "GET on a mutating git workflow path should be refused with 405");
|
|
289
|
+
assert.equal(bypassCommit.body?.ok, false, "GET bypass refusal should carry ok: false");
|
|
290
|
+
const headAfterBypass = spawnSync("git", ["rev-parse", "HEAD"], { cwd, encoding: "utf8" });
|
|
291
|
+
assert.notEqual(headAfterBypass.status, 0, "GET bypass attempt must not create a commit");
|
|
292
|
+
|
|
293
|
+
const bypassPush = await request("127.0.0.1", `/api/git-workflow/push?tab=${encodeURIComponent(tabId)}`);
|
|
294
|
+
assert.equal(bypassPush.status, 405, "GET /api/git-workflow/push should be refused with 405");
|
|
295
|
+
|
|
296
|
+
const bypassAdd = await request("127.0.0.1", `/api/git-workflow/add?tab=${encodeURIComponent(tabId)}`);
|
|
297
|
+
assert.equal(bypassAdd.status, 405, "GET /api/git-workflow/add should be refused with 405");
|
|
298
|
+
|
|
299
|
+
const postReadonly = await request("127.0.0.1", "/api/git-workflow/default-commit-message", { method: "POST", body: { tab: tabId } });
|
|
300
|
+
assert.equal(postReadonly.status, 405, "POST on a read-only git workflow path should be refused with 405");
|
|
301
|
+
|
|
228
302
|
const gitCommit = await request("127.0.0.1", "/api/git-workflow/initial-commit", { method: "POST", body: { tab: tabId } });
|
|
229
303
|
assert.equal(gitCommit.status, 200);
|
|
230
304
|
assert.equal(gitCommit.body?.ok, true, "initial commit endpoint should commit the staged README.md");
|
|
@@ -445,6 +519,305 @@ try {
|
|
|
445
519
|
assert.equal(multipleDefault.status, 200);
|
|
446
520
|
assert.equal(multipleDefault.body?.ok, true, "default commit message endpoint should still return ok when no default is available");
|
|
447
521
|
assert.equal(multipleDefault.body?.data?.message, "", "multiple staged files should not get a default commit message");
|
|
522
|
+
|
|
523
|
+
// ---- Git action endpoints: staging, operations, stash, fetch/divergence, undo, tags, prune ----
|
|
524
|
+
|
|
525
|
+
const gitFixturesRoot = await mkdtemp(path.join(tmpdir(), "pi-webui-git-actions-"));
|
|
526
|
+
const makeFixtureRepo = async (name) => {
|
|
527
|
+
const dir = path.join(gitFixturesRoot, name);
|
|
528
|
+
await mkdir(dir, { recursive: true });
|
|
529
|
+
runGitFixture(["init", "-b", "main", dir], gitFixturesRoot, `${name} fixture should initialize`);
|
|
530
|
+
runGitFixture(["config", "user.name", "Pi WebUI Test"], dir, `${name} fixture should set user name`);
|
|
531
|
+
runGitFixture(["config", "user.email", "pi-webui-test@example.invalid"], dir, `${name} fixture should set user email`);
|
|
532
|
+
await writeFile(path.join(dir, "file.txt"), "base\n");
|
|
533
|
+
runGitFixture(["add", "file.txt"], dir, `${name} fixture should stage base`);
|
|
534
|
+
runGitFixture(["commit", "-m", "base"], dir, `${name} fixture should commit base`);
|
|
535
|
+
return dir;
|
|
536
|
+
};
|
|
537
|
+
const openFixtureTab = async (dir, title) => {
|
|
538
|
+
const created = await request("127.0.0.1", "/api/tabs", { method: "POST", body: { cwd: dir, title } });
|
|
539
|
+
assert.equal(created.status, 201, `${title} tab should open`);
|
|
540
|
+
const id = created.body?.data?.tab?.id;
|
|
541
|
+
assert.ok(id, `${title} tab should have an id`);
|
|
542
|
+
return id;
|
|
543
|
+
};
|
|
544
|
+
|
|
545
|
+
// Staging endpoints
|
|
546
|
+
const stagingRepo = await makeFixtureRepo("staging");
|
|
547
|
+
const stagingTab = await openFixtureTab(stagingRepo, "staging-fixture");
|
|
548
|
+
await writeFile(path.join(stagingRepo, "file.txt"), "modified\n");
|
|
549
|
+
await writeFile(path.join(stagingRepo, "loose.txt"), "loose\n");
|
|
550
|
+
|
|
551
|
+
const stageFile = await request("127.0.0.1", "/api/git-changes/stage-file", { method: "POST", body: { tab: stagingTab, path: "file.txt" } });
|
|
552
|
+
assert.equal(stageFile.status, 200);
|
|
553
|
+
assert.equal(stageFile.body?.ok, true, "stage-file endpoint should stage a modified file");
|
|
554
|
+
assert.equal(stageFile.body?.data?.changes?.summary?.staged, 1, "stage-file should report one staged file afterwards");
|
|
555
|
+
|
|
556
|
+
const unstageFile = await request("127.0.0.1", "/api/git-changes/unstage-file", { method: "POST", body: { tab: stagingTab, path: "file.txt" } });
|
|
557
|
+
assert.equal(unstageFile.status, 200);
|
|
558
|
+
assert.equal(unstageFile.body?.ok, true, "unstage-file endpoint should unstage the file");
|
|
559
|
+
assert.equal(unstageFile.body?.data?.changes?.summary?.staged, 0, "unstage-file should report zero staged files afterwards");
|
|
560
|
+
|
|
561
|
+
const discardUnconfirmed = await request("127.0.0.1", "/api/git-changes/discard-file", { method: "POST", body: { tab: stagingTab, path: "file.txt" } });
|
|
562
|
+
assert.equal(discardUnconfirmed.status, 409, "discard-file without confirmed: true should be refused");
|
|
563
|
+
const discardConfirmed = await request("127.0.0.1", "/api/git-changes/discard-file", { method: "POST", body: { tab: stagingTab, path: "file.txt", confirmed: true } });
|
|
564
|
+
assert.equal(discardConfirmed.status, 200);
|
|
565
|
+
assert.equal(discardConfirmed.body?.ok, true, "confirmed discard-file should restore the file");
|
|
566
|
+
assert.equal(await readFile(path.join(stagingRepo, "file.txt"), "utf8"), "base\n", "discard-file should restore committed content");
|
|
567
|
+
|
|
568
|
+
const escapeStage = await request("127.0.0.1", "/api/git-changes/stage-file", { method: "POST", body: { tab: stagingTab, path: "../outside.txt" } });
|
|
569
|
+
assert.equal(escapeStage.body?.ok, false, "stage-file must reject paths escaping the repository root");
|
|
570
|
+
|
|
571
|
+
const deleteTracked = await request("127.0.0.1", "/api/git-changes/delete-untracked", { method: "POST", body: { tab: stagingTab, path: "file.txt", confirmed: true } });
|
|
572
|
+
assert.equal(deleteTracked.status, 409, "delete-untracked must refuse tracked files");
|
|
573
|
+
const deleteUnconfirmed = await request("127.0.0.1", "/api/git-changes/delete-untracked", { method: "POST", body: { tab: stagingTab, path: "loose.txt" } });
|
|
574
|
+
assert.equal(deleteUnconfirmed.status, 409, "delete-untracked without confirmed: true should be refused");
|
|
575
|
+
const deleteConfirmed = await request("127.0.0.1", "/api/git-changes/delete-untracked", { method: "POST", body: { tab: stagingTab, path: "loose.txt", confirmed: true } });
|
|
576
|
+
assert.equal(deleteConfirmed.status, 200);
|
|
577
|
+
assert.equal(deleteConfirmed.body?.ok, true, "confirmed delete-untracked should delete the file");
|
|
578
|
+
assert.equal(await readFile(path.join(stagingRepo, "loose.txt"), "utf8").then(() => true, () => false), false, "delete-untracked should remove the file from disk");
|
|
579
|
+
|
|
580
|
+
// Diff truncation transparency: a diff larger than the 500KB cap must be flagged.
|
|
581
|
+
const truncationRepo = await makeFixtureRepo("diff-truncation");
|
|
582
|
+
const truncationTab = await openFixtureTab(truncationRepo, "diff-truncation-fixture");
|
|
583
|
+
const bigLines = Array.from({ length: 24_000 }, (_, index) => `line ${index} ${"x".repeat(24)}`).join("\n");
|
|
584
|
+
await writeFile(path.join(truncationRepo, "big.txt"), `${bigLines}\n`);
|
|
585
|
+
runGitFixture(["add", "big.txt"], truncationRepo, "truncation fixture should stage the big file");
|
|
586
|
+
runGitFixture(["commit", "-m", "big base"], truncationRepo, "truncation fixture should commit the big file");
|
|
587
|
+
const flipped = Array.from({ length: 24_000 }, (_, index) => `LINE ${index} ${"y".repeat(24)}`).join("\n");
|
|
588
|
+
await writeFile(path.join(truncationRepo, "big.txt"), `${flipped}\n`);
|
|
589
|
+
const truncatedChanges = await request("127.0.0.1", `/api/git-changes?tab=${encodeURIComponent(truncationTab)}`, { timeoutMs: 20_000 });
|
|
590
|
+
assert.equal(truncatedChanges.body?.ok, true, "git changes should load the oversized diff repo");
|
|
591
|
+
const unstagedSection = (truncatedChanges.body?.data?.sections || []).find((section) => section.key === "unstaged");
|
|
592
|
+
assert.ok(unstagedSection, "unstaged section should exist for the oversized diff");
|
|
593
|
+
assert.equal(unstagedSection.truncated, true, "oversized diffs must carry a structured truncated flag");
|
|
594
|
+
assert.ok(Number(unstagedSection.capBytes) > 0, "truncated sections must report the cap size");
|
|
595
|
+
const stagedSection = (truncatedChanges.body?.data?.sections || []).find((section) => section.key === "staged");
|
|
596
|
+
assert.equal(stagedSection?.truncated, false, "small diffs must not be flagged as truncated");
|
|
597
|
+
|
|
598
|
+
// Merge conflict lifecycle
|
|
599
|
+
const makeConflictRepo = async (name) => {
|
|
600
|
+
const dir = await makeFixtureRepo(name);
|
|
601
|
+
runGitFixture(["checkout", "-b", "side"], dir, `${name} fixture should branch`);
|
|
602
|
+
await writeFile(path.join(dir, "file.txt"), "side\n");
|
|
603
|
+
runGitFixture(["commit", "-am", "side"], dir, `${name} fixture should commit side`);
|
|
604
|
+
runGitFixture(["checkout", "main"], dir, `${name} fixture should return to main`);
|
|
605
|
+
await writeFile(path.join(dir, "file.txt"), "main\n");
|
|
606
|
+
runGitFixture(["commit", "-am", "main"], dir, `${name} fixture should commit main`);
|
|
607
|
+
const merge = spawnSync("git", ["merge", "side"], { cwd: dir, encoding: "utf8" });
|
|
608
|
+
assert.notEqual(merge.status, 0, `${name} fixture merge should conflict`);
|
|
609
|
+
return dir;
|
|
610
|
+
};
|
|
611
|
+
|
|
612
|
+
const mergeRepo = await makeConflictRepo("merge-conflict");
|
|
613
|
+
const mergeTab = await openFixtureTab(mergeRepo, "merge-conflict-fixture");
|
|
614
|
+
const operationSnapshot = await request("127.0.0.1", `/api/git-operation?tab=${encodeURIComponent(mergeTab)}`);
|
|
615
|
+
assert.equal(operationSnapshot.status, 200);
|
|
616
|
+
assert.equal(operationSnapshot.body?.ok, true, "operation endpoint should read a merging repo");
|
|
617
|
+
assert.equal(operationSnapshot.body?.data?.operation, "merge");
|
|
618
|
+
assert.equal(operationSnapshot.body?.data?.canContinue, false, "merge with conflicts must not be continuable");
|
|
619
|
+
assert.equal(operationSnapshot.body?.data?.conflicts?.[0]?.path, "file.txt");
|
|
620
|
+
assert.equal(operationSnapshot.body?.data?.conflicts?.[0]?.status, "UU");
|
|
621
|
+
assert.equal(operationSnapshot.body?.data?.conflicts?.[0]?.preview?.hasMarkers, true, "conflict preview should detect conflict markers");
|
|
622
|
+
|
|
623
|
+
const continueBlocked = await request("127.0.0.1", "/api/git-operation/continue", { method: "POST", body: { tab: mergeTab } });
|
|
624
|
+
assert.equal(continueBlocked.status, 200);
|
|
625
|
+
assert.equal(continueBlocked.body?.ok, false, "continue with unmerged paths must fail");
|
|
626
|
+
assert.equal(continueBlocked.body?.code, "UNMERGED_PATHS");
|
|
627
|
+
|
|
628
|
+
await writeFile(path.join(mergeRepo, "file.txt"), "resolved\n");
|
|
629
|
+
const markResolved = await request("127.0.0.1", "/api/git-operation/stage-file", { method: "POST", body: { tab: mergeTab, path: "file.txt" } });
|
|
630
|
+
assert.equal(markResolved.status, 200);
|
|
631
|
+
assert.equal(markResolved.body?.ok, true, "operation stage-file should mark the conflict as resolved");
|
|
632
|
+
assert.equal(markResolved.body?.data?.operation?.canContinue, true, "after staging the only conflict, continue should be possible");
|
|
633
|
+
|
|
634
|
+
const continueMerge = await request("127.0.0.1", "/api/git-operation/continue", { method: "POST", body: { tab: mergeTab }, timeoutMs: 20_000 });
|
|
635
|
+
assert.equal(continueMerge.status, 200);
|
|
636
|
+
assert.equal(continueMerge.body?.ok, true, `continue should commit the resolved merge: ${continueMerge.body?.error || ""}`);
|
|
637
|
+
assert.equal(continueMerge.body?.data?.operation?.operation, null, "after continuing, no operation should remain");
|
|
638
|
+
|
|
639
|
+
const abortRepo = await makeConflictRepo("merge-abort");
|
|
640
|
+
const abortTab = await openFixtureTab(abortRepo, "merge-abort-fixture");
|
|
641
|
+
const abortUnconfirmed = await request("127.0.0.1", "/api/git-operation/abort", { method: "POST", body: { tab: abortTab } });
|
|
642
|
+
assert.equal(abortUnconfirmed.status, 409, "abort without confirmed: true should be refused");
|
|
643
|
+
const abortConfirmed = await request("127.0.0.1", "/api/git-operation/abort", { method: "POST", body: { tab: abortTab, confirmed: true }, timeoutMs: 20_000 });
|
|
644
|
+
assert.equal(abortConfirmed.status, 200);
|
|
645
|
+
assert.equal(abortConfirmed.body?.ok, true, "confirmed abort should stop the merge");
|
|
646
|
+
assert.equal(abortConfirmed.body?.data?.operation?.operation, null, "after aborting, no operation should remain");
|
|
647
|
+
assert.equal(await readFile(path.join(abortRepo, "file.txt"), "utf8"), "main\n", "abort should restore the pre-merge content");
|
|
648
|
+
|
|
649
|
+
// Rebase lifecycle: skip support + abort
|
|
650
|
+
const rebaseRepo = await makeFixtureRepo("rebase-conflict");
|
|
651
|
+
runGitFixture(["checkout", "-b", "side"], rebaseRepo, "rebase fixture should branch");
|
|
652
|
+
await writeFile(path.join(rebaseRepo, "file.txt"), "side\n");
|
|
653
|
+
runGitFixture(["commit", "-am", "side"], rebaseRepo, "rebase fixture should commit side");
|
|
654
|
+
runGitFixture(["checkout", "main"], rebaseRepo, "rebase fixture should return to main");
|
|
655
|
+
await writeFile(path.join(rebaseRepo, "file.txt"), "main\n");
|
|
656
|
+
runGitFixture(["commit", "-am", "main"], rebaseRepo, "rebase fixture should commit main");
|
|
657
|
+
runGitFixture(["checkout", "side"], rebaseRepo, "rebase fixture should return to side");
|
|
658
|
+
const rebaseStart = spawnSync("git", ["rebase", "main"], { cwd: rebaseRepo, encoding: "utf8" });
|
|
659
|
+
assert.notEqual(rebaseStart.status, 0, "rebase fixture should conflict");
|
|
660
|
+
const rebaseTab = await openFixtureTab(rebaseRepo, "rebase-conflict-fixture");
|
|
661
|
+
const rebaseSnapshot = await request("127.0.0.1", `/api/git-operation?tab=${encodeURIComponent(rebaseTab)}`);
|
|
662
|
+
assert.equal(rebaseSnapshot.body?.data?.operation, "rebase");
|
|
663
|
+
assert.equal(rebaseSnapshot.body?.data?.canSkip, true, "rebase should support skip");
|
|
664
|
+
const rebaseAbort = await request("127.0.0.1", "/api/git-operation/abort", { method: "POST", body: { tab: rebaseTab, confirmed: true }, timeoutMs: 20_000 });
|
|
665
|
+
assert.equal(rebaseAbort.body?.ok, true, "confirmed rebase abort should succeed");
|
|
666
|
+
|
|
667
|
+
// Bisect lifecycle
|
|
668
|
+
const bisectRepo = await makeFixtureRepo("bisect");
|
|
669
|
+
runGitFixture(["bisect", "start"], bisectRepo, "bisect fixture should start");
|
|
670
|
+
const bisectTab = await openFixtureTab(bisectRepo, "bisect-fixture");
|
|
671
|
+
const bisectSnapshot = await request("127.0.0.1", `/api/git-operation?tab=${encodeURIComponent(bisectTab)}`);
|
|
672
|
+
assert.equal(bisectSnapshot.body?.data?.operation, "bisect");
|
|
673
|
+
const bisectInvalid = await request("127.0.0.1", "/api/git-operation/bisect", { method: "POST", body: { tab: bisectTab, verdict: "evil" } });
|
|
674
|
+
assert.equal(bisectInvalid.status, 400, "invalid bisect verdicts must be rejected");
|
|
675
|
+
const bisectResetUnconfirmed = await request("127.0.0.1", "/api/git-operation/bisect", { method: "POST", body: { tab: bisectTab, verdict: "reset" } });
|
|
676
|
+
assert.equal(bisectResetUnconfirmed.status, 409, "bisect reset without confirmed: true should be refused");
|
|
677
|
+
const bisectReset = await request("127.0.0.1", "/api/git-operation/bisect", { method: "POST", body: { tab: bisectTab, verdict: "reset", confirmed: true }, timeoutMs: 20_000 });
|
|
678
|
+
assert.equal(bisectReset.body?.ok, true, "confirmed bisect reset should succeed");
|
|
679
|
+
assert.equal(bisectReset.body?.data?.operation?.operation, null, "after reset, no bisect should remain");
|
|
680
|
+
|
|
681
|
+
// Stash lifecycle
|
|
682
|
+
const stashRepo = await makeFixtureRepo("stash");
|
|
683
|
+
const stashTab = await openFixtureTab(stashRepo, "stash-fixture");
|
|
684
|
+
await writeFile(path.join(stashRepo, "file.txt"), "stash me\n");
|
|
685
|
+
await writeFile(path.join(stashRepo, "new-file.txt"), "untracked\n");
|
|
686
|
+
const stashSave = await request("127.0.0.1", "/api/git-stash/save", { method: "POST", body: { tab: stashTab, includeUntracked: true, message: "harness stash" } });
|
|
687
|
+
assert.equal(stashSave.status, 200);
|
|
688
|
+
assert.equal(stashSave.body?.ok, true, `stash save should succeed: ${stashSave.body?.error || ""}`);
|
|
689
|
+
assert.equal(stashSave.body?.data?.stashes?.length, 1, "stash save should leave one stash entry");
|
|
690
|
+
|
|
691
|
+
const stashList = await request("127.0.0.1", `/api/git-stash?tab=${encodeURIComponent(stashTab)}`);
|
|
692
|
+
assert.equal(stashList.body?.ok, true);
|
|
693
|
+
assert.equal(stashList.body?.data?.stashes?.[0]?.ref, "stash@{0}");
|
|
694
|
+
assert.match(stashList.body?.data?.stashes?.[0]?.subject || "", /harness stash/);
|
|
695
|
+
|
|
696
|
+
const stashShow = await request("127.0.0.1", `/api/git-stash/show?ref=${encodeURIComponent("stash@{0}")}&tab=${encodeURIComponent(stashTab)}`);
|
|
697
|
+
assert.equal(stashShow.body?.ok, true, "stash show should return a preview");
|
|
698
|
+
assert.match(stashShow.body?.data?.stat || "", /file\.txt/, "stash preview should mention the stashed file");
|
|
699
|
+
|
|
700
|
+
const stashBadRef = await request("127.0.0.1", `/api/git-stash/show?ref=${encodeURIComponent("stash@{0}; rm -rf /")}&tab=${encodeURIComponent(stashTab)}`);
|
|
701
|
+
assert.equal(stashBadRef.status, 400, "malformed stash refs must be rejected");
|
|
702
|
+
|
|
703
|
+
const stashApply = await request("127.0.0.1", "/api/git-stash/apply", { method: "POST", body: { tab: stashTab, ref: "stash@{0}" } });
|
|
704
|
+
assert.equal(stashApply.body?.ok, true, `stash apply should succeed: ${stashApply.body?.error || ""}`);
|
|
705
|
+
assert.equal(await readFile(path.join(stashRepo, "file.txt"), "utf8"), "stash me\n", "stash apply should restore the stashed content");
|
|
706
|
+
|
|
707
|
+
const stashDropUnconfirmed = await request("127.0.0.1", "/api/git-stash/drop", { method: "POST", body: { tab: stashTab, ref: "stash@{0}" } });
|
|
708
|
+
assert.equal(stashDropUnconfirmed.status, 409, "stash drop without confirmed: true should be refused");
|
|
709
|
+
const stashDrop = await request("127.0.0.1", "/api/git-stash/drop", { method: "POST", body: { tab: stashTab, ref: "stash@{0}", confirmed: true } });
|
|
710
|
+
assert.equal(stashDrop.body?.ok, true, "confirmed stash drop should succeed");
|
|
711
|
+
assert.equal(stashDrop.body?.data?.stashes?.length, 0, "dropping the only stash should empty the list");
|
|
712
|
+
|
|
713
|
+
// Fetch, divergence classification, integrate, push classification
|
|
714
|
+
await writeFile(path.join(remoteWork, "incoming.txt"), "base\nremote one\nremote two\nremote three\n");
|
|
715
|
+
runGitFixture(["commit", "-am", "remote three"], remoteWork, "remote worktree should commit third incoming change");
|
|
716
|
+
runGitFixture(["push", "origin", "main"], remoteWork, "remote worktree should push the third commit");
|
|
717
|
+
await writeFile(path.join(localRepo, "local-only.txt"), "local\n");
|
|
718
|
+
runGitFixture(["add", "local-only.txt"], localRepo, "local repo should stage local divergence");
|
|
719
|
+
runGitFixture(["commit", "-m", "local divergence"], localRepo, "local repo should commit local divergence");
|
|
720
|
+
|
|
721
|
+
const fetchResult = await request("127.0.0.1", "/api/git-fetch", { method: "POST", body: { tab: remoteTabId }, timeoutMs: 30_000 });
|
|
722
|
+
assert.equal(fetchResult.status, 200);
|
|
723
|
+
assert.equal(fetchResult.body?.ok, true, `fetch endpoint should fetch from the bare origin: ${fetchResult.body?.error || ""}`);
|
|
724
|
+
assert.equal(fetchResult.body?.data?.changes?.summary?.behind, 1, "fetch should reveal the remote commit");
|
|
725
|
+
assert.equal(fetchResult.body?.data?.changes?.remote?.diverged, true, "fetch should reveal divergence");
|
|
726
|
+
assert.equal(fetchResult.body?.data?.changes?.remote?.canPull, false, "diverged branches must not offer one-click pull");
|
|
727
|
+
|
|
728
|
+
const divergedPull = await request("127.0.0.1", "/api/git-changes/pull", { method: "POST", body: { tab: remoteTabId }, timeoutMs: 30_000 });
|
|
729
|
+
assert.equal(divergedPull.body?.ok, false, "ff-only pull must fail on diverged branches");
|
|
730
|
+
assert.equal(divergedPull.body?.code, "DIVERGED", "diverged pull failures should be classified");
|
|
731
|
+
assert.ok(divergedPull.body?.hint, "diverged pull failures should carry a hint");
|
|
732
|
+
|
|
733
|
+
const integrateUnconfirmed = await request("127.0.0.1", "/api/git-changes/integrate", { method: "POST", body: { tab: remoteTabId, mode: "merge" } });
|
|
734
|
+
assert.equal(integrateUnconfirmed.status, 409, "integrate without confirmed: true should be refused");
|
|
735
|
+
const integrateMerge = await request("127.0.0.1", "/api/git-changes/integrate", { method: "POST", body: { tab: remoteTabId, mode: "merge", confirmed: true }, timeoutMs: 30_000 });
|
|
736
|
+
assert.equal(integrateMerge.body?.ok, true, `confirmed merge integrate should succeed: ${integrateMerge.body?.error || ""}`);
|
|
737
|
+
assert.equal(integrateMerge.body?.data?.changes?.summary?.behind, 0, "after integrating, nothing should remain behind");
|
|
738
|
+
|
|
739
|
+
const pushDiverged = await request("127.0.0.1", "/api/git-workflow/push", { method: "POST", body: { tab: remoteTabId }, timeoutMs: 30_000 });
|
|
740
|
+
assert.equal(pushDiverged.body?.ok, true, `push should succeed after integration: ${pushDiverged.body?.error || ""}`);
|
|
741
|
+
assert.equal(pushDiverged.body?.data?.branch, "main");
|
|
742
|
+
assert.equal(pushDiverged.body?.data?.protectedBranch, true, "pushing main should be flagged as a protected branch");
|
|
743
|
+
|
|
744
|
+
// Undo guards: pushed commits must not be undoable
|
|
745
|
+
const undoPushedState = await request("127.0.0.1", `/api/git-undo?tab=${encodeURIComponent(remoteTabId)}`);
|
|
746
|
+
assert.equal(undoPushedState.body?.ok, true);
|
|
747
|
+
assert.equal(undoPushedState.body?.data?.canUndoLastCommit, false, "a pushed HEAD must not be undoable");
|
|
748
|
+
const undoPushed = await request("127.0.0.1", "/api/git-undo/last-commit", { method: "POST", body: { tab: remoteTabId, confirmed: true } });
|
|
749
|
+
assert.equal(undoPushed.status, 409, "undoing a pushed commit must be refused");
|
|
750
|
+
|
|
751
|
+
// Undo/amend on an unpushed repo
|
|
752
|
+
const undoRepo = await makeFixtureRepo("undo");
|
|
753
|
+
const undoTab = await openFixtureTab(undoRepo, "undo-fixture");
|
|
754
|
+
await writeFile(path.join(undoRepo, "file.txt"), "second\n");
|
|
755
|
+
runGitFixture(["commit", "-am", "second"], undoRepo, "undo fixture should commit a second change");
|
|
756
|
+
|
|
757
|
+
const undoState = await request("127.0.0.1", `/api/git-undo?tab=${encodeURIComponent(undoTab)}`);
|
|
758
|
+
assert.equal(undoState.body?.ok, true);
|
|
759
|
+
assert.equal(undoState.body?.data?.canUndoLastCommit, true, "an unpushed commit with a parent should be undoable");
|
|
760
|
+
assert.equal(undoState.body?.data?.lastCommit?.subject, "second");
|
|
761
|
+
|
|
762
|
+
const undoUnconfirmed = await request("127.0.0.1", "/api/git-undo/last-commit", { method: "POST", body: { tab: undoTab } });
|
|
763
|
+
assert.equal(undoUnconfirmed.status, 409, "undo without confirmed: true should be refused");
|
|
764
|
+
const undoConfirmed = await request("127.0.0.1", "/api/git-undo/last-commit", { method: "POST", body: { tab: undoTab, confirmed: true } });
|
|
765
|
+
assert.equal(undoConfirmed.body?.ok, true, `confirmed undo should soft-reset: ${undoConfirmed.body?.error || ""}`);
|
|
766
|
+
assert.equal(undoConfirmed.body?.data?.restoreCommand, "git reset --soft ORIG_HEAD");
|
|
767
|
+
assert.equal(undoConfirmed.body?.data?.changes?.summary?.staged, 1, "soft reset should keep the change staged");
|
|
768
|
+
assert.equal(runGitFixture(["log", "-1", "--format=%s"], undoRepo, "undo fixture should read HEAD subject"), "base", "undo should move HEAD back to the base commit");
|
|
769
|
+
|
|
770
|
+
const undoNoParent = await request("127.0.0.1", "/api/git-undo/last-commit", { method: "POST", body: { tab: undoTab, confirmed: true } });
|
|
771
|
+
assert.equal(undoNoParent.status, 409, "undoing the root commit must be refused");
|
|
772
|
+
|
|
773
|
+
const amendWithStaged = await request("127.0.0.1", "/api/git-undo/amend-message", { method: "POST", body: { tab: undoTab, confirmed: true, message: "nope" } });
|
|
774
|
+
assert.equal(amendWithStaged.status, 409, "amending with staged changes must be refused");
|
|
775
|
+
runGitFixture(["commit", "-m", "second again"], undoRepo, "undo fixture should re-commit the staged change");
|
|
776
|
+
const amendUnconfirmed = await request("127.0.0.1", "/api/git-undo/amend-message", { method: "POST", body: { tab: undoTab, message: "amended subject" } });
|
|
777
|
+
assert.equal(amendUnconfirmed.status, 409, "amend without confirmed: true should be refused");
|
|
778
|
+
const amendConfirmed = await request("127.0.0.1", "/api/git-undo/amend-message", { method: "POST", body: { tab: undoTab, confirmed: true, message: "amended subject" } });
|
|
779
|
+
assert.equal(amendConfirmed.body?.ok, true, `confirmed amend should rewrite the message: ${amendConfirmed.body?.error || ""}`);
|
|
780
|
+
assert.equal(runGitFixture(["log", "-1", "--format=%s"], undoRepo, "undo fixture should read amended subject"), "amended subject");
|
|
781
|
+
|
|
782
|
+
const reflog = await request("127.0.0.1", `/api/git-reflog?tab=${encodeURIComponent(undoTab)}`);
|
|
783
|
+
assert.equal(reflog.body?.ok, true, "reflog endpoint should return entries");
|
|
784
|
+
assert.ok((reflog.body?.data?.entries || []).length >= 3, "reflog should include the undo/amend history");
|
|
785
|
+
assert.match(reflog.body?.data?.entries?.[0]?.selector || "", /^HEAD@\{0\}$/);
|
|
786
|
+
|
|
787
|
+
// Tags
|
|
788
|
+
const tagInvalid = await request("127.0.0.1", "/api/git-tags/create", { method: "POST", body: { tab: undoTab, confirmed: true, name: "bad tag" } });
|
|
789
|
+
assert.equal(tagInvalid.status, 400, "invalid tag names must be rejected");
|
|
790
|
+
const tagUnconfirmed = await request("127.0.0.1", "/api/git-tags/create", { method: "POST", body: { tab: undoTab, name: "v0.0.1-harness", message: "harness tag" } });
|
|
791
|
+
assert.equal(tagUnconfirmed.status, 409, "tag creation without confirmed: true should be refused");
|
|
792
|
+
const tagCreate = await request("127.0.0.1", "/api/git-tags/create", { method: "POST", body: { tab: undoTab, confirmed: true, name: "v0.0.1-harness", message: "harness tag" } });
|
|
793
|
+
assert.equal(tagCreate.body?.ok, true, `confirmed tag creation should succeed: ${tagCreate.body?.error || ""}`);
|
|
794
|
+
const tagList = await request("127.0.0.1", `/api/git-tags?tab=${encodeURIComponent(undoTab)}`);
|
|
795
|
+
assert.equal(tagList.body?.ok, true);
|
|
796
|
+
const createdTag = (tagList.body?.data?.tags || []).find((tag) => tag.name === "v0.0.1-harness");
|
|
797
|
+
assert.ok(createdTag, "created tag should appear in the tag list");
|
|
798
|
+
assert.equal(createdTag.annotated, true, "created tag should be annotated");
|
|
799
|
+
assert.equal(createdTag.atHead, true, "created tag should point at HEAD");
|
|
800
|
+
|
|
801
|
+
// Signing diagnostics + submodule status (read-only)
|
|
802
|
+
const signing = await request("127.0.0.1", `/api/git-signing?tab=${encodeURIComponent(undoTab)}`);
|
|
803
|
+
assert.equal(signing.body?.ok, true, "signing diagnostics should load");
|
|
804
|
+
assert.equal(signing.body?.data?.mismatch, false, "fixture repo should not report a signing mismatch");
|
|
805
|
+
const submodules = await request("127.0.0.1", `/api/git-submodules?tab=${encodeURIComponent(undoTab)}`);
|
|
806
|
+
assert.equal(submodules.body?.ok, true, "submodule status should load");
|
|
807
|
+
assert.equal(submodules.body?.data?.hasSubmodules, false, "fixture repo has no submodules");
|
|
808
|
+
|
|
809
|
+
// Worktree prune (dry run + confirmed)
|
|
810
|
+
const pruneDryRun = await request("127.0.0.1", `/api/git-worktrees/prune?tab=${encodeURIComponent(undoTab)}`);
|
|
811
|
+
assert.equal(pruneDryRun.body?.ok, true, "prune dry run should load");
|
|
812
|
+
assert.equal(pruneDryRun.body?.data?.dryRun, true);
|
|
813
|
+
const pruneUnconfirmed = await request("127.0.0.1", "/api/git-worktrees/prune", { method: "POST", body: { tab: undoTab } });
|
|
814
|
+
assert.equal(pruneUnconfirmed.status, 409, "prune without confirmed: true should be refused");
|
|
815
|
+
const pruneConfirmed = await request("127.0.0.1", "/api/git-worktrees/prune", { method: "POST", body: { tab: undoTab, confirmed: true }, timeoutMs: 20_000 });
|
|
816
|
+
assert.equal(pruneConfirmed.body?.ok, true, `confirmed prune should succeed: ${pruneConfirmed.body?.error || ""}`);
|
|
817
|
+
|
|
818
|
+
const closeGitActionTabs = await request("127.0.0.1", "/api/tabs/close", { method: "POST", body: { ids: [stagingTab, truncationTab, mergeTab, abortTab, rebaseTab, bisectTab, stashTab, undoTab] }, timeoutMs: 10_000 });
|
|
819
|
+
assert.equal(closeGitActionTabs.status, 200, "git action fixture tabs should close");
|
|
820
|
+
await rmWithRetry(gitFixturesRoot);
|
|
448
821
|
} else {
|
|
449
822
|
console.log("http-endpoints-harness: git not available; skipping git init workflow endpoint checks");
|
|
450
823
|
}
|
|
@@ -694,6 +1067,196 @@ try {
|
|
|
694
1067
|
assert.equal(copy.body?.data?.status, "succeeded", "native /copy should succeed through the adapter");
|
|
695
1068
|
assert.equal(copy.body?.data?.copyText, "fake last text");
|
|
696
1069
|
|
|
1070
|
+
// File tree/viewer APIs stay scoped to the requested tab cwd and reject unsafe content.
|
|
1071
|
+
const filesRoot = path.join(cwd, "files-fixture");
|
|
1072
|
+
const viewerRelative = "files-fixture/viewer.txt";
|
|
1073
|
+
const markdownRelative = "files-fixture/docs/readme.md";
|
|
1074
|
+
const binaryRelative = "files-fixture/binary.bin";
|
|
1075
|
+
const noDefaultRelative = "files-fixture/no-default.piunknown";
|
|
1076
|
+
const largeRelative = "files-fixture/large.txt";
|
|
1077
|
+
const movableFileRelative = "files-fixture/move-me.txt";
|
|
1078
|
+
const movedFileRelative = "files-fixture/docs/moved-file.txt";
|
|
1079
|
+
const movableDirectoryRelative = "files-fixture/move-dir";
|
|
1080
|
+
const movedDirectoryRelative = "files-fixture/docs/move-dir";
|
|
1081
|
+
const deleteFileRelative = "files-fixture/delete-me.txt";
|
|
1082
|
+
const deleteDirectoryRelative = "files-fixture/delete-dir";
|
|
1083
|
+
const depthEightFileRelative = "deep-search/f1/f2/f3/f4/f5/f6/depth-eight-file.txt";
|
|
1084
|
+
const depthEightDirectoryRelative = "deep-search/d1/d2/d3/d4/d5/d6/depth-eight-dir";
|
|
1085
|
+
const depthNineFileRelative = "deep-search/t1/t2/t3/t4/t5/t6/t7/too-deep-file.txt";
|
|
1086
|
+
await mkdir(path.join(filesRoot, "docs"), { recursive: true });
|
|
1087
|
+
await mkdir(path.join(cwd, path.dirname(depthEightFileRelative)), { recursive: true });
|
|
1088
|
+
await mkdir(path.join(cwd, depthEightDirectoryRelative), { recursive: true });
|
|
1089
|
+
await mkdir(path.join(cwd, path.dirname(depthNineFileRelative)), { recursive: true });
|
|
1090
|
+
await writeFile(path.join(cwd, viewerRelative), "hello file viewer\nsecond line\n", "utf8");
|
|
1091
|
+
await writeFile(path.join(cwd, markdownRelative), "# File Viewer\n\nMarkdown preview support.\n", "utf8");
|
|
1092
|
+
await writeFile(path.join(cwd, noDefaultRelative), "unknown extension should use text/plain editor fallback\n", "utf8");
|
|
1093
|
+
await writeFile(path.join(cwd, depthEightFileRelative), "depth 8 search fixture\n", "utf8");
|
|
1094
|
+
await writeFile(path.join(cwd, depthNineFileRelative), "depth 9 search fixture\n", "utf8");
|
|
1095
|
+
await writeFile(path.join(cwd, binaryRelative), Buffer.from([0, 1, 2, 3]));
|
|
1096
|
+
await writeFile(path.join(cwd, largeRelative), Buffer.alloc(2 * 1024 * 1024 + 1, 0x61));
|
|
1097
|
+
await writeFile(path.join(cwd, movableFileRelative), "move me\n", "utf8");
|
|
1098
|
+
await mkdir(path.join(cwd, movableDirectoryRelative), { recursive: true });
|
|
1099
|
+
await writeFile(path.join(cwd, movableDirectoryRelative, "nested.txt"), "nested move\n", "utf8");
|
|
1100
|
+
await writeFile(path.join(cwd, deleteFileRelative), "delete me\n", "utf8");
|
|
1101
|
+
await mkdir(path.join(cwd, deleteDirectoryRelative), { recursive: true });
|
|
1102
|
+
await writeFile(path.join(cwd, deleteDirectoryRelative, "nested.txt"), "nested delete\n", "utf8");
|
|
1103
|
+
|
|
1104
|
+
const fileTree = await request("127.0.0.1", `/api/files?tab=${encodeURIComponent(tabId)}&path=${encodeURIComponent("files-fixture")}`);
|
|
1105
|
+
assert.equal(fileTree.status, 200, `file tree endpoint should list workspace directories: ${fileTree.body?.error || ""}`);
|
|
1106
|
+
assert.equal(fileTree.body?.ok, true);
|
|
1107
|
+
const fileTreeEntries = fileTree.body?.data?.entries || [];
|
|
1108
|
+
assert.equal(fileTreeEntries.find((entry) => entry.name === "docs")?.type, "directory", "file tree should identify subdirectories");
|
|
1109
|
+
assert.equal(fileTreeEntries.find((entry) => entry.name === "viewer.txt")?.type, "file", "file tree should identify regular files");
|
|
1110
|
+
|
|
1111
|
+
const fileSearch = await request("127.0.0.1", `/api/files/search?tab=${encodeURIComponent(tabId)}&q=${encodeURIComponent("readme")}`);
|
|
1112
|
+
assert.equal(fileSearch.status, 200, `file search endpoint should search workspace files: ${fileSearch.body?.error || ""}`);
|
|
1113
|
+
assert.equal(fileSearch.body?.ok, true);
|
|
1114
|
+
const fileSearchEntries = fileSearch.body?.data?.entries || [];
|
|
1115
|
+
assert.equal(fileSearchEntries.find((entry) => entry.path === markdownRelative)?.type, "file", "file search should find matching files recursively");
|
|
1116
|
+
|
|
1117
|
+
const directorySearch = await request("127.0.0.1", `/api/files/search?tab=${encodeURIComponent(tabId)}&q=${encodeURIComponent("docs")}`);
|
|
1118
|
+
assert.equal(directorySearch.status, 200, `file search endpoint should search directories: ${directorySearch.body?.error || ""}`);
|
|
1119
|
+
assert.equal(directorySearch.body?.data?.entries?.find((entry) => entry.path === "files-fixture/docs")?.type, "directory", "file search should find matching directories");
|
|
1120
|
+
|
|
1121
|
+
const depthLimitedSearch = await request("127.0.0.1", `/api/files/search?tab=${encodeURIComponent(tabId)}&q=${encodeURIComponent("depth")}`);
|
|
1122
|
+
assert.equal(depthLimitedSearch.status, 200, `file search endpoint should honor depth-limited recursive search: ${depthLimitedSearch.body?.error || ""}`);
|
|
1123
|
+
assert.equal(depthLimitedSearch.body?.data?.maxDepth, 8, "file search should advertise the recursive depth cap");
|
|
1124
|
+
assert.equal(depthLimitedSearch.body?.data?.entries?.find((entry) => entry.path === depthEightFileRelative)?.type, "file", "file search should find matching files at depth 8");
|
|
1125
|
+
assert.equal(depthLimitedSearch.body?.data?.entries?.find((entry) => entry.path === depthEightDirectoryRelative)?.type, "directory", "file search should find matching directories at depth 8");
|
|
1126
|
+
assert.equal(depthLimitedSearch.body?.data?.entries?.some((entry) => entry.path === depthNineFileRelative), false, "file search should not descend past depth 8");
|
|
1127
|
+
|
|
1128
|
+
const emptyFileSearch = await request("127.0.0.1", `/api/files/search?tab=${encodeURIComponent(tabId)}&q=`);
|
|
1129
|
+
assert.equal(emptyFileSearch.status, 200, "empty file search should be accepted");
|
|
1130
|
+
assert.deepEqual(emptyFileSearch.body?.data?.entries, [], "empty file search should not scan the workspace");
|
|
1131
|
+
|
|
1132
|
+
const textContent = await request("127.0.0.1", `/api/files/content?tab=${encodeURIComponent(tabId)}&path=${encodeURIComponent(viewerRelative)}`);
|
|
1133
|
+
assert.equal(textContent.status, 200, `text file should open in WebUI: ${textContent.body?.error || ""}`);
|
|
1134
|
+
assert.equal(textContent.body?.data?.content, "hello file viewer\nsecond line\n");
|
|
1135
|
+
assert.equal(textContent.body?.data?.language, "text");
|
|
1136
|
+
|
|
1137
|
+
const markdownContent = await request("127.0.0.1", `/api/files/content?tab=${encodeURIComponent(tabId)}&path=${encodeURIComponent(markdownRelative)}`);
|
|
1138
|
+
assert.equal(markdownContent.status, 200, `markdown file should open in WebUI: ${markdownContent.body?.error || ""}`);
|
|
1139
|
+
assert.equal(markdownContent.body?.data?.language, "markdown", "markdown files should get markdown viewer support metadata");
|
|
1140
|
+
|
|
1141
|
+
const emptyDefaultOpen = await request("127.0.0.1", "/api/files/open-default", { method: "POST", body: { tab: tabId, path: "" } });
|
|
1142
|
+
assert.equal(emptyDefaultOpen.status, 400, "default editor opens should reject empty paths instead of opening the workspace root");
|
|
1143
|
+
assert.match(String(emptyDefaultOpen.body?.error || ""), /Path to open is required/i);
|
|
1144
|
+
|
|
1145
|
+
const defaultOpen = await request("127.0.0.1", "/api/files/open-default", { method: "POST", body: { tab: tabId, path: viewerRelative } });
|
|
1146
|
+
assert.equal(defaultOpen.status, 200, `default editor should open the requested file: ${defaultOpen.body?.error || ""}`);
|
|
1147
|
+
assert.equal(defaultOpen.body?.data?.path, viewerRelative, "default editor endpoint should report the requested file path");
|
|
1148
|
+
const openedAbsolutePath = path.join(cwd, viewerRelative);
|
|
1149
|
+
let openLog = "";
|
|
1150
|
+
for (let attempt = 0; attempt < 20; attempt++) {
|
|
1151
|
+
openLog = await readFile(openCommandLog, "utf8").catch(() => "");
|
|
1152
|
+
if (openLog.includes(openedAbsolutePath)) break;
|
|
1153
|
+
await delay(50);
|
|
1154
|
+
}
|
|
1155
|
+
assert.ok(openLog.includes(openedAbsolutePath), "default editor command should receive the currently opened file path");
|
|
1156
|
+
|
|
1157
|
+
if (process.platform === "linux") {
|
|
1158
|
+
const fallbackOpen = await request("127.0.0.1", "/api/files/open-default", { method: "POST", body: { tab: tabId, path: noDefaultRelative } });
|
|
1159
|
+
assert.equal(fallbackOpen.status, 200, `default editor should fall back to the .txt editor for unassociated files: ${fallbackOpen.body?.error || ""}`);
|
|
1160
|
+
assert.equal(fallbackOpen.body?.data?.path, noDefaultRelative, "default editor fallback should report the requested file path");
|
|
1161
|
+
assert.equal(fallbackOpen.body?.data?.fallbackToTextEditor, true, "unassociated files should be opened through the text/plain fallback");
|
|
1162
|
+
assert.equal(fallbackOpen.body?.data?.desktopFile, "fake-text-editor.desktop", "fallback should use the text/plain desktop app association");
|
|
1163
|
+
const fallbackAbsolutePath = path.join(cwd, noDefaultRelative);
|
|
1164
|
+
for (let attempt = 0; attempt < 20; attempt++) {
|
|
1165
|
+
openLog = await readFile(openCommandLog, "utf8").catch(() => "");
|
|
1166
|
+
if (openLog.includes(`gio\tlaunch\tfake-text-editor.desktop\t${fallbackAbsolutePath}`)) break;
|
|
1167
|
+
await delay(50);
|
|
1168
|
+
}
|
|
1169
|
+
assert.ok(openLog.includes(`gio\tlaunch\tfake-text-editor.desktop\t${fallbackAbsolutePath}`), "default editor fallback should invoke the text/plain editor for the requested file");
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1172
|
+
const savedFile = await request("127.0.0.1", "/api/files/content", {
|
|
1173
|
+
method: "POST",
|
|
1174
|
+
body: { tab: tabId, path: viewerRelative, content: "updated from WebUI\n", mtimeMs: textContent.body?.data?.mtimeMs },
|
|
1175
|
+
});
|
|
1176
|
+
assert.equal(savedFile.status, 200, `file save should succeed from localhost: ${savedFile.body?.error || ""}`);
|
|
1177
|
+
assert.equal(await readFile(path.join(cwd, viewerRelative), "utf8"), "updated from WebUI\n", "file save endpoint should write UTF-8 text content");
|
|
1178
|
+
|
|
1179
|
+
const moveUnconfirmed = await request("127.0.0.1", "/api/files/move", { method: "POST", body: { tab: tabId, path: movableFileRelative, toPath: movedFileRelative } });
|
|
1180
|
+
assert.equal(moveUnconfirmed.status, 409, "file moves require explicit confirmation");
|
|
1181
|
+
|
|
1182
|
+
const moveFile = await request("127.0.0.1", "/api/files/move", { method: "POST", body: { tab: tabId, path: movableFileRelative, toPath: movedFileRelative, confirmed: true } });
|
|
1183
|
+
assert.equal(moveFile.status, 200, `file move should succeed from localhost: ${moveFile.body?.error || ""}`);
|
|
1184
|
+
assert.equal(moveFile.body?.data?.destination, movedFileRelative, "move endpoint should report the new relative file path");
|
|
1185
|
+
assert.equal(await readFile(path.join(cwd, movedFileRelative), "utf8"), "move me\n", "move endpoint should relocate file contents");
|
|
1186
|
+
assert.equal(await pathExists(path.join(cwd, movableFileRelative)), false, "move endpoint should remove the original file path");
|
|
1187
|
+
|
|
1188
|
+
const moveDirectory = await request("127.0.0.1", "/api/files/move", { method: "POST", body: { tab: tabId, path: movableDirectoryRelative, toPath: "files-fixture/docs", confirmed: true } });
|
|
1189
|
+
assert.equal(moveDirectory.status, 200, `directory move should succeed from localhost: ${moveDirectory.body?.error || ""}`);
|
|
1190
|
+
assert.equal(moveDirectory.body?.data?.destination, movedDirectoryRelative, "moving to an existing directory should place the source under that directory");
|
|
1191
|
+
assert.equal(await readFile(path.join(cwd, movedDirectoryRelative, "nested.txt"), "utf8"), "nested move\n", "move endpoint should relocate directory contents");
|
|
1192
|
+
assert.equal(await pathExists(path.join(cwd, movableDirectoryRelative)), false, "directory move endpoint should remove the original directory path");
|
|
1193
|
+
|
|
1194
|
+
const moveExistingDestination = await request("127.0.0.1", "/api/files/move", { method: "POST", body: { tab: tabId, path: movedFileRelative, toPath: markdownRelative, confirmed: true } });
|
|
1195
|
+
assert.equal(moveExistingDestination.status, 409, "move endpoint should refuse to overwrite an existing destination");
|
|
1196
|
+
|
|
1197
|
+
const deleteUnconfirmed = await request("127.0.0.1", "/api/files", { method: "DELETE", body: { tab: tabId, path: deleteFileRelative } });
|
|
1198
|
+
assert.equal(deleteUnconfirmed.status, 409, "file deletes require explicit confirmation");
|
|
1199
|
+
|
|
1200
|
+
const deleteFile = await request("127.0.0.1", "/api/files", { method: "DELETE", body: { tab: tabId, path: deleteFileRelative, confirmed: true } });
|
|
1201
|
+
assert.equal(deleteFile.status, 200, `file delete should succeed from localhost: ${deleteFile.body?.error || ""}`);
|
|
1202
|
+
assert.equal(deleteFile.body?.data?.deleted, true, "delete endpoint should report successful file deletion");
|
|
1203
|
+
assert.equal(await pathExists(path.join(cwd, deleteFileRelative)), false, "delete endpoint should remove regular files");
|
|
1204
|
+
|
|
1205
|
+
const deleteDirectory = await request("127.0.0.1", "/api/files", { method: "DELETE", body: { tab: tabId, path: deleteDirectoryRelative, confirmed: true } });
|
|
1206
|
+
assert.equal(deleteDirectory.status, 200, `directory delete should succeed from localhost: ${deleteDirectory.body?.error || ""}`);
|
|
1207
|
+
assert.equal(deleteDirectory.body?.data?.type, "directory", "delete endpoint should report directory deletions");
|
|
1208
|
+
assert.equal(await pathExists(path.join(cwd, deleteDirectoryRelative)), false, "delete endpoint should remove directories recursively");
|
|
1209
|
+
|
|
1210
|
+
const deleteWorkspaceRoot = await request("127.0.0.1", "/api/files", { method: "DELETE", body: { tab: tabId, path: "", confirmed: true } });
|
|
1211
|
+
assert.equal(deleteWorkspaceRoot.status, 400, "delete endpoint must refuse deleting the active workspace root");
|
|
1212
|
+
|
|
1213
|
+
const binaryContent = await request("127.0.0.1", `/api/files/content?tab=${encodeURIComponent(tabId)}&path=${encodeURIComponent(binaryRelative)}`);
|
|
1214
|
+
assert.equal(binaryContent.status, 415, "binary files should be rejected by the WebUI file viewer");
|
|
1215
|
+
assert.match(String(binaryContent.body?.error || ""), /binary/i);
|
|
1216
|
+
|
|
1217
|
+
const oversizedContent = await request("127.0.0.1", `/api/files/content?tab=${encodeURIComponent(tabId)}&path=${encodeURIComponent(largeRelative)}`);
|
|
1218
|
+
assert.equal(oversizedContent.status, 413, "oversized files should be rejected by the WebUI file viewer");
|
|
1219
|
+
assert.match(String(oversizedContent.body?.error || ""), /too large/i);
|
|
1220
|
+
|
|
1221
|
+
const outsideRoot = await mkdtemp(path.join(tmpdir(), "pi-webui-files-outside-"));
|
|
1222
|
+
try {
|
|
1223
|
+
const outsideFile = path.join(outsideRoot, "outside.txt");
|
|
1224
|
+
await writeFile(outsideFile, "outside\n", "utf8");
|
|
1225
|
+
const outsideAbsolute = await request("127.0.0.1", `/api/files/content?tab=${encodeURIComponent(tabId)}&path=${encodeURIComponent(outsideFile)}`);
|
|
1226
|
+
assert.equal(outsideAbsolute.status, 403, "absolute paths outside the active tab cwd should be rejected");
|
|
1227
|
+
assert.match(String(outsideAbsolute.body?.error || ""), /active tab working directory/i);
|
|
1228
|
+
|
|
1229
|
+
const outsideDelete = await request("127.0.0.1", "/api/files", { method: "DELETE", body: { tab: tabId, path: outsideFile, confirmed: true } });
|
|
1230
|
+
assert.equal(outsideDelete.status, 403, "delete endpoint should reject absolute paths outside the active tab cwd");
|
|
1231
|
+
|
|
1232
|
+
const outsideMoveDestination = await request("127.0.0.1", "/api/files/move", { method: "POST", body: { tab: tabId, path: movedFileRelative, toPath: outsideFile, confirmed: true } });
|
|
1233
|
+
assert.equal(outsideMoveDestination.status, 403, "move endpoint should reject destinations outside the active tab cwd");
|
|
1234
|
+
|
|
1235
|
+
const symlinkPath = path.join(filesRoot, "outside-link.txt");
|
|
1236
|
+
try {
|
|
1237
|
+
await symlink(outsideFile, symlinkPath);
|
|
1238
|
+
const symlinkEscape = await request("127.0.0.1", `/api/files/content?tab=${encodeURIComponent(tabId)}&path=${encodeURIComponent("files-fixture/outside-link.txt")}`);
|
|
1239
|
+
assert.equal(symlinkEscape.status, 403, "symlinks resolving outside the active tab cwd should be rejected");
|
|
1240
|
+
assert.match(String(symlinkEscape.body?.error || ""), /escapes the active tab working directory/i);
|
|
1241
|
+
} catch (error) {
|
|
1242
|
+
if (!["EPERM", "EACCES", "EINVAL", "ENOTSUP"].includes(error?.code)) throw error;
|
|
1243
|
+
console.log(`http-endpoints-harness: symlink unavailable (${error.code}); skipping file symlink confinement check`);
|
|
1244
|
+
}
|
|
1245
|
+
} finally {
|
|
1246
|
+
await rmWithRetry(outsideRoot);
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
const filesTab = await request("127.0.0.1", "/api/tabs", { method: "POST", body: { cwd: filesRoot, title: "files-fixture" } });
|
|
1250
|
+
assert.equal(filesTab.status, 201, `file fixture tab should open: ${filesTab.body?.error || ""}`);
|
|
1251
|
+
const filesTabId = filesTab.body?.data?.tab?.id;
|
|
1252
|
+
assert.ok(filesTabId, "file fixture tab should have an id");
|
|
1253
|
+
const scopedFileContent = await request("127.0.0.1", `/api/files/content?tab=${encodeURIComponent(filesTabId)}&path=${encodeURIComponent("viewer.txt")}`);
|
|
1254
|
+
assert.equal(scopedFileContent.status, 200, "file paths should resolve against the requested tab cwd");
|
|
1255
|
+
const wrongTabContent = await request("127.0.0.1", `/api/files/content?tab=${encodeURIComponent(tabId)}&path=${encodeURIComponent("viewer.txt")}`);
|
|
1256
|
+
assert.equal(wrongTabContent.status, 404, "file paths should not bleed across tab cwd scopes");
|
|
1257
|
+
const closeFilesTab = await request("127.0.0.1", "/api/tabs/close", { method: "POST", body: { ids: [filesTabId] }, timeoutMs: 10_000 });
|
|
1258
|
+
assert.equal(closeFilesTab.status, 200, "file fixture tab should close after scoped file checks");
|
|
1259
|
+
|
|
697
1260
|
// Natural Conversation shell: /talk availability drives per-tab status and safety guards.
|
|
698
1261
|
const conversationFeature = await request("127.0.0.1", `/api/features/natural-conversation?tab=${encodeURIComponent(tabId)}`);
|
|
699
1262
|
assert.equal(conversationFeature.status, 200);
|
|
@@ -738,6 +1301,18 @@ try {
|
|
|
738
1301
|
assert.equal(blockedBash.status, 409, "user bash should be blocked while Natural Conversation is active");
|
|
739
1302
|
assert.match(String(blockedBash.body?.error || ""), /bash is blocked|Natural Conversation Mode is active/);
|
|
740
1303
|
|
|
1304
|
+
const blockedFileSave = await request("127.0.0.1", "/api/files/content", { method: "POST", body: { tab: tabId, path: viewerRelative, content: "blocked by conversation mode\n" } });
|
|
1305
|
+
assert.equal(blockedFileSave.status, 409, "file edits should be blocked while Natural Conversation is active");
|
|
1306
|
+
assert.equal(blockedFileSave.body?.error, "file edits are blocked");
|
|
1307
|
+
|
|
1308
|
+
const blockedFileDelete = await request("127.0.0.1", "/api/files", { method: "DELETE", body: { tab: tabId, path: viewerRelative, confirmed: true } });
|
|
1309
|
+
assert.equal(blockedFileDelete.status, 409, "file deletes should be blocked while Natural Conversation is active");
|
|
1310
|
+
assert.match(String(blockedFileDelete.body?.error || ""), /file deletion is blocked/i);
|
|
1311
|
+
|
|
1312
|
+
const blockedFileMove = await request("127.0.0.1", "/api/files/move", { method: "POST", body: { tab: tabId, path: viewerRelative, toPath: "files-fixture/blocked-move.txt", confirmed: true } });
|
|
1313
|
+
assert.equal(blockedFileMove.status, 409, "file moves should be blocked while Natural Conversation is active");
|
|
1314
|
+
assert.match(String(blockedFileMove.body?.error || ""), /file moves are blocked/i);
|
|
1315
|
+
|
|
741
1316
|
const allowedConversationPrompt = await request("127.0.0.1", "/api/prompt", { method: "POST", body: { message: "Explain the current repo briefly", tab: tabId } });
|
|
742
1317
|
assert.equal(allowedConversationPrompt.status, 200, "ordinary prompts remain allowed while Natural Conversation is active");
|
|
743
1318
|
|
|
@@ -816,6 +1391,30 @@ try {
|
|
|
816
1391
|
});
|
|
817
1392
|
assert.equal(remoteDelete.status, 403, "session delete must be localhost-only");
|
|
818
1393
|
|
|
1394
|
+
const remoteFileSave = await request(lan, "/api/files/content", {
|
|
1395
|
+
method: "POST",
|
|
1396
|
+
body: { tab: tabId, path: viewerRelative, content: "remote save should be blocked\n" },
|
|
1397
|
+
});
|
|
1398
|
+
assert.equal(remoteFileSave.status, 403, "file saves must be localhost-only");
|
|
1399
|
+
|
|
1400
|
+
const remoteFileOpenDefault = await request(lan, "/api/files/open-default", {
|
|
1401
|
+
method: "POST",
|
|
1402
|
+
body: { tab: tabId, path: viewerRelative },
|
|
1403
|
+
});
|
|
1404
|
+
assert.equal(remoteFileOpenDefault.status, 403, "opening files in the default editor must be localhost-only");
|
|
1405
|
+
|
|
1406
|
+
const remoteFileDelete = await request(lan, "/api/files", {
|
|
1407
|
+
method: "DELETE",
|
|
1408
|
+
body: { tab: tabId, path: viewerRelative, confirmed: true },
|
|
1409
|
+
});
|
|
1410
|
+
assert.equal(remoteFileDelete.status, 403, "file deletes must be localhost-only");
|
|
1411
|
+
|
|
1412
|
+
const remoteFileMove = await request(lan, "/api/files/move", {
|
|
1413
|
+
method: "POST",
|
|
1414
|
+
body: { tab: tabId, path: viewerRelative, toPath: "files-fixture/remote-move.txt", confirmed: true },
|
|
1415
|
+
});
|
|
1416
|
+
assert.equal(remoteFileMove.status, 403, "file moves must be localhost-only");
|
|
1417
|
+
|
|
819
1418
|
const remoteExport = await request(lan, "/api/prompt", {
|
|
820
1419
|
method: "POST",
|
|
821
1420
|
body: { message: "/export", tab: tabId },
|
|
@@ -903,6 +1502,7 @@ try {
|
|
|
903
1502
|
}
|
|
904
1503
|
await new Promise((resolve) => voiceProvider.close(() => resolve()));
|
|
905
1504
|
await rmWithRetry(cwd);
|
|
1505
|
+
await rmWithRetry(harnessSideEffectsRoot);
|
|
906
1506
|
}
|
|
907
1507
|
|
|
908
1508
|
console.log("http-endpoints-harness.test.mjs passed");
|