From aae39e49caea42b03adfe4117ac41283cd79c3ce Mon Sep 17 00:00:00 2001 From: hairyf Date: Tue, 13 Oct 2026 23:13:01 +0800 Subject: [PATCH] fix(ci): isolate layout persistence, accept a detached HEAD, auth the aggregate lane editor-host cases share one localStorage: setSession hydrates the previous case's layout for the same session id, so a persisted treeOpen:false decided whether the next case mounted its tree at all (the two Windows-lane failures). The file now clears it and the tree-seam call counters per test. smoke.spec asserted a branch name in a PR checkout, where HEAD is detached: it now compares git's own symbolic ref and status.detached. The aggregate lane exchanges its launch token for a signature cookie before probing /sidebar/api, because the plugin's native connection admission authenticates; the token is never echoed (redacted in the failure tail). --- scripts/e2e-aggregate-mount.sh | 21 +++++++----- tests/e2e-aggregate-auth.spec.ts | 59 ++++++++++++++++++++++++++++++++ tests/editor-host.spec.tsx | 16 ++++++++- tests/smoke.spec.ts | 10 ++++-- 4 files changed, 94 insertions(+), 12 deletions(-) create mode 100644 tests/e2e-aggregate-auth.spec.ts diff --git a/scripts/e2e-aggregate-mount.sh b/scripts/e2e-aggregate-mount.sh index a4e2fb12..69c029ce 100755 --- a/scripts/e2e-aggregate-mount.sh +++ b/scripts/e2e-aggregate-mount.sh @@ -89,19 +89,22 @@ $DSH_CMD plugin --profile web add "file:$TARBALL" say "启动 dsh web(--port ${PORT},日志 ${LOG_DIR})…" e2e_start_dsh_web "$OUT_LOG" "$ERR_LOG" -# 等待启动 URL 或进程退出(最多 120s)。这里有意只取 origin:0.1.2-alpha.1+ -# 的就绪行是 `…/?token=<43字符>` 鉴权 URL,但下方的探活全部打插件的 -# /sidebar/api/* 路由——webserver carrier 不做鉴权(只有 /api、index 与 -# remote.mux 升级在 browser auth 之后),origin 拼路径即正确且两版通用。 -E2E_READY_RE='http://127\.0\.0\.1:[0-9]+' +# 等待完整启动 URL(保留 token);插件的 native Connection admission 也要 +# 鉴权。先用启动 URL 兑换 cookie,再以 origin 拼 API 路径,绝不打印 token。 +E2E_READY_RE='dsh web: http://127\.0\.0\.1:[0-9]+[^ ]*' E2E_READY_PICK=tail e2e_wait_dsh_web_ready "$OUT_LOG" || true if [ -z "$URL" ]; then - warn "out log 尾部:$(tail -5 "$OUT_LOG" 2>/dev/null || true)" - warn "err log 尾部:$(tail -5 "$ERR_LOG" 2>/dev/null || true)" + warn "out log 尾部:$(tail -5 "$OUT_LOG" 2>/dev/null | sed -E 's/([?&]token=)[^&[:space:]]+/\1[redacted]/g' || true)" + warn "err log 尾部:$(tail -5 "$ERR_LOG" 2>/dev/null | sed -E 's/([?&]token=)[^&[:space:]]+/\1[redacted]/g' || true)" die "dsh web 未在 120s 内启动" fi +LAUNCH_URL="$URL" +URL="${LAUNCH_URL%%/\?*}" say "已启动:$URL" +COOKIE_JAR="$SCRATCH/auth.cookies" +# curl 默认不跟随 303;cookie jar 在既有 scratch/trap 清理范围内。 +curl -fsS --cookie-jar "$COOKIE_JAR" -o /dev/null "$LAUNCH_URL" || die "启动 token 兑换失败" # ── 断言 ──────────────────────────────────────────────────────────────────── if grep -q "duplicate prefix route" "$ERR_LOG" "$OUT_LOG" 2>/dev/null; then @@ -113,13 +116,13 @@ fi # 的理由:/sidebar/api 是插件自己 fence 的 prefix 路由,未注册的方法统统 404, # 所以 {"ok":true} 只可能来自插件真实注册的 handler —— generic missing-route # 404 无法冒充(P2: Probe a real sidebar API method)。 -RESP="$(curl -s -X POST "$URL/sidebar/api/settings.get" 2>/dev/null || true)" +RESP="$(curl -s --cookie "$COOKIE_JAR" -X POST "$URL/sidebar/api/settings.get" 2>/dev/null || true)" if ! printf '%s' "$RESP" | grep -q '"ok":true'; then die "/sidebar/api/settings.get 未返回 ok:true(响应:$(printf '%s' "$RESP" | head -c 200))" fi say "/sidebar/api/settings.get → ok:true(真实 handler 存活)" -STATUS="$(curl -s -o /dev/null -w '%{http_code}' -X POST "$URL/sidebar/api/__e2e_unknown__" 2>/dev/null || true)" +STATUS="$(curl -s --cookie "$COOKIE_JAR" -o /dev/null -w '%{http_code}' -X POST "$URL/sidebar/api/__e2e_unknown__" 2>/dev/null || true)" say "/sidebar/api POST 未知方法 → HTTP ${STATUS}(期望 404)" [ "$STATUS" = "404" ] || die "/sidebar/api 未知方法未按预期返回 404(HTTP ${STATUS})" diff --git a/tests/e2e-aggregate-auth.spec.ts b/tests/e2e-aggregate-auth.spec.ts new file mode 100644 index 00000000..e7098d38 --- /dev/null +++ b/tests/e2e-aggregate-auth.spec.ts @@ -0,0 +1,59 @@ +import { spawnSync } from 'node:child_process' +import { mkdtempSync, readFileSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { fileURLToPath } from 'node:url' +import { expect, it } from 'vitest' + +const root = fileURLToPath(new URL('../', import.meta.url)).replaceAll('\\', '/') +const script = readFileSync(join(root, 'scripts/e2e-aggregate-mount.sh'), 'utf8') +const hasBash = spawnSync('bash', ['--version'], { stdio: 'ignore' }).status === 0 + +it.skipIf(!hasBash)('exchanges the full aggregate launch URL and authenticates both unchanged API probes', () => { + const scratch = mkdtempSync(join(tmpdir(), 'dsh-aggregate-auth-')).replaceAll('\\', '/') + const launch = 'http://127.0.0.1:4199/?token=aggregate-test-token' + try { + expect(spawnSync('bash', ['-n', join(root, 'scripts/e2e-aggregate-mount.sh')]).status).toBe(0) + const start = script.indexOf('E2E_READY_RE=') + expect(start).toBeGreaterThan(-1) + const result = spawnSync('bash', ['-c', ` +set -euo pipefail +E2E_TAG=e2e-aggregate-auth +source "$ROOT/scripts/e2e-common.sh" +OUT_LOG="$SCRATCH/out.log" +ERR_LOG="$SCRATCH/err.log" +printf 'dsh web: %s (LAN: http://example.invalid:4199)\\n' "$TEST_LAUNCH_URL" > "$OUT_LOG" +: > "$ERR_LOG" +SERVER_PID=$$ +curl() { + printf '%s\\n' "$*" >> "$SCRATCH/curl.log" + case "\${!#}" in + "$TEST_LAUNCH_URL") : > "$SCRATCH/auth.cookies" ;; + */settings.get) printf '%s' '{"ok":true}' ;; + */__e2e_unknown__) printf '404' ;; + *) return 1 ;; + esac +} +${script.slice(start)} +`], { + encoding: 'utf8', timeout: 5_000, + env: { ...process.env, ROOT: root, SCRATCH: scratch, TEST_LAUNCH_URL: launch }, + }) + expect(result.stderr).toBe('') + expect(result.status).toBe(0) + expect(result.stdout).not.toContain('aggregate-test-token') + const calls = readFileSync(join(scratch, 'curl.log'), 'utf8').trim().split('\n') + expect(calls).toHaveLength(3) + expect(calls[0]).toContain(`--cookie-jar ${scratch}/auth.cookies`) + expect(calls[0]).toContain(launch) + expect(calls[0]).not.toMatch(/--location|-[^-\s]*L/) + for (const call of calls.slice(1)) { + expect(call).toContain(`--cookie ${scratch}/auth.cookies`) + expect(call).toContain('-X POST http://127.0.0.1:4199/sidebar/api/') + expect(call).not.toContain('?token=') + } + expect(result.stdout).toContain('HTTP 404') + } finally { + rmSync(scratch, { recursive: true, force: true }) + } +}) diff --git a/tests/editor-host.spec.tsx b/tests/editor-host.spec.tsx index 180baf96..8516d477 100644 --- a/tests/editor-host.spec.tsx +++ b/tests/editor-host.spec.tsx @@ -9,7 +9,7 @@ * only, no editor chrome); file tabs keep the full chrome in both modes. */ // @vitest-environment jsdom -import { describe, expect, it, vi } from 'vitest' +import { beforeEach, describe, expect, it, vi } from 'vitest' import { createElement, useEffect, type ReactNode } from 'react' import { createRoot } from 'react-dom/client' import { act } from 'react-dom/test-utils' @@ -36,6 +36,20 @@ const treeAnswers = (): { calls: number; landedAt: number[] } => const treeScript = (): { slow: number[]; hang: number[] } => ((globalThis as unknown as { __treeScript?: { slow: number[]; hang: number[] } }).__treeScript ??= { slow: [], hang: [] }) +// Layout persistence is process-global: `setSession` hydrates whatever the +// previous case persisted for the same session id, so one case's `treeOpen: +// false` silently decides whether the next case mounts its tree at all. The +// tree seam's call numbers are per-case scripting for the same reason. +beforeEach(() => { + localStorage.clear() + const answers = treeAnswers() + answers.calls = 0 + answers.landedAt = [] + const script = treeScript() + script.slow = [] + script.hang = [] +}) + // The tree mounts inside EditorHost in these scenarios, so the api seam must // answer without a host (the sibling FileTree specs mock it the same way). vi.mock('../src/client/api.ts', () => ({ diff --git a/tests/smoke.spec.ts b/tests/smoke.spec.ts index 6d293460..a1014557 100644 --- a/tests/smoke.spec.ts +++ b/tests/smoke.spec.ts @@ -170,13 +170,19 @@ describe('host plugin smoke', () => { const cwd = process.cwd() const status = await git.status(cwd) expect(status.isRepo).toBe(true) - expect(typeof status.branch).toBe('string') + // PR checkouts have a detached HEAD; compare the native ref rather than invent a branch. + const head = gitRun(cwd, ['rev-parse', '--symbolic-full-name', 'HEAD']).trim() + const branch = head === 'HEAD' ? undefined : head.replace(/^refs\/heads\//, '') + expect(status.detached).toBe(head === 'HEAD') + expect(status.branch).toBe(branch) expect(Array.isArray(status.entries)).toBe(true) const log = await git.log(cwd) expect(log.length).toBeGreaterThan(0) expect(log[0]!.hash).toMatch(/^[0-9a-f]{7,}$/) const branches = await git.branches(cwd) - expect(branches.names).toContain(branches.current) + expect(branches.current).toBe(branch ?? '') + expect(Array.isArray(branches.names)).toBe(true) + if (branch !== undefined) expect(branches.names).toContain(branch) }) it('enriches the log (full hash + refs) and renders commit diffs', async () => {