diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000..cbec1b4 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,10 @@ +#!/bin/sh +set -eu + +# Keep the configured global checks when this checkout opts into repository hooks. +global_hooks=$(git config --global --path --get core.hooksPath || :) +if [ -n "$global_hooks" ] && [ -x "$global_hooks/pre-commit" ] && + [ ! "$global_hooks/pre-commit" -ef "$0" ]; then + "$global_hooks/pre-commit" +fi +exec make test-changed diff --git a/.gitignore b/.gitignore index db0cb9f..05e475e 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,8 @@ /web/node_modules/ /web/dist/ /web/tsconfig.tsbuildinfo +/web/test-results/ +/web/playwright-report/ # Local runtime .env diff --git a/Makefile b/Makefile index c7a1219..a9b4d5b 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,22 @@ BUILD_TIME ?= $(shell date -u +%Y-%m-%dT%H:%M:%SZ) verify: $(PY) scripts/ci_checks.py -.PHONY: qa qa-doctor qa-self-test +.PHONY: qa qa-doctor qa-self-test tests-e2e tests-e2e-ci test-changed test-tooling +tests-e2e: + npm --prefix web run test:e2e -- $(E2E) + +# The hosted CI job container is disposable and starts without Chromium's system libraries. +tests-e2e-ci: export PLAYWRIGHT_BROWSERS_PATH ?= /opt/citools-cache/playwright +tests-e2e-ci: + @test -f /.dockerenv || { printf '%s\n' 'tests-e2e-ci requires the disposable CI container. Use make tests-e2e here.' >&2; exit 1; } + DEBIAN_FRONTEND=noninteractive PLAYWRIGHT_DOWNLOAD_CONNECTION_TIMEOUT=90000 \ + timeout 360s npm --prefix web exec -- playwright install --with-deps --only-shell chromium + $(MAKE) tests-e2e +test-changed: + $(PY) scripts/test_changed.py +test-tooling: qa-self-test + $(PY) -m unittest discover -s scripts -p test_test_changed.py -v + qa: $(PY) scripts/qa_env.py run qa-doctor: diff --git a/docs/DEVELOPMENT_AND_TESTING.md b/docs/DEVELOPMENT_AND_TESTING.md index b7fc206..78fab3c 100644 --- a/docs/DEVELOPMENT_AND_TESTING.md +++ b/docs/DEVELOPMENT_AND_TESTING.md @@ -11,7 +11,7 @@ make qa-doctor KG_QA_SHA='<完整提交 SHA>' make qa ``` -`make qa-doctor` 检查工具链、HiFox 机器在线状态、OMP 模型调用、测试库身份和已安装的迁移。`make qa` 安装锁定依赖,在专用测试库上通过 API 初始化和迁移,随后核对完整迁移序列、API 就绪状态、Web 首页及 Vite 健康代理。通过后执行 Go 和 Vitest 测试,结束时关闭本次启动的服务。每次运行的服务日志保存在 `~/.local/state/knowledge-governance/qa/`。需要提交前完整门禁时执行 `make verify`。 +`make qa-doctor` 检查工具链、HiFox 机器在线状态、OMP 模型调用、测试库身份和已安装的迁移。`make qa` 安装锁定依赖,在专用测试库上通过 API 初始化和迁移,随后核对完整迁移序列、API 就绪状态、Web 首页及 Vite 健康代理。通过后执行 Go、Vitest 和 Chromium 浏览器测试,结束时关闭本次启动的服务。每次运行的服务日志保存在 `~/.local/state/knowledge-governance/qa/`。需要提交前完整门禁时执行 `make verify`。 初次配置由负责人在 QA 虚拟机准备 `~/.config/knowledge-governance/`。以下示例使用虚构的 RDS 主机名。 @@ -128,4 +128,19 @@ TEST_DATABASE_URL="postgresql://${PGUSER}@${PGHOST}:${PGPORT}/${PGDATABASE}?sslm go test -count=1 -v ./internal/platform/postgres -run '^TestUISettingsActivationRetiresPreviousRevisionIntegration$' ``` -检查输出出现目标用例的 `PASS`。用例被跳过或没有匹配时,应补齐配置或纠正用例名后重跑。前端 Vitest 负责组件与业务逻辑检查,浏览器 E2E 应单独执行并按 Feature 保存证据。提测记录至少包括完整提交 SHA、执行命令、退出码,以及失败用例的日志或浏览器截图。 +检查输出出现目标用例的 `PASS`。如果用例被跳过或没有匹配,补齐配置或修正用例名后重跑。提测记录包含完整提交 SHA、执行命令、退出码及失败证据。 + +## 5. 浏览器回归与提交前检查 + +浏览器用例、固定数据和独立 Vite 配置位于 `web/e2e/`,Playwright 版本由前端锁文件固定。第一次运行先执行 `make deps-install`,再在 QA 机的 `web/` 目录用 `npm exec -- playwright install chromium` 下载对应浏览器。已有浏览器缓存时测试本身可以离线运行。 + +```bash +make tests-e2e +make tests-e2e E2E=scope-rules.spec.ts +python3 scripts/test_changed.py --list +make test-changed +``` + +`make tests-e2e` 在 `127.0.0.1:4175` 启动夹具页并用 Chromium 检查目录规则。夹具页加载真实 React 编辑器并拦截 API 请求到固定的内存数据。覆盖保存和刷新读回、显示 HTTP 403、展示子目录与兄弟目录各自的继承来源。后端角色权限由 Go 测试覆盖,规则持久化和继承语义由 PostgreSQL 集成测试覆盖。失败截图与 trace 保存在 `web/test-results/`。 + +在独立克隆中可运行 `git config --local core.hooksPath .githooks` 启用仓内提交钩子。钩子先运行可用的全局提交检查,再运行 `make test-changed`。该入口读暂存差异,能定位时只跑修改的测试文件或 Go 包,遇到生产源码或公共依赖改动时扩大到相关套件。对已有仓库自定义钩子的克隆,应在现有钩子中调用 `make test-changed`。`make verify` 继续作为完整提交门禁,Gitea CI 将 Chromium 用例纳入必需检查。本地使用 `make tests-e2e` 直接运行。 diff --git a/internal/httpapi/auth_test.go b/internal/httpapi/auth_test.go index 56b42dc..7028cac 100644 --- a/internal/httpapi/auth_test.go +++ b/internal/httpapi/auth_test.go @@ -241,6 +241,16 @@ func TestAuthRBACMatrix(t *testing.T) { t.Fatalf("viewer write = %d", rec.Code) } + for _, method := range []string{http.MethodPut, http.MethodDelete} { + result := httptest.NewRecorder() + request := httptest.NewRequest(method, "/api/v1/scope-rules", nil) + request.AddCookie(viewer) + handler.ServeHTTP(result, request) + if result.Code != http.StatusForbidden { + t.Fatalf("viewer %s scope rules = %d, want %d", method, result.Code, http.StatusForbidden) + } + } + // engineer may create batches but not authorize production rec = httptest.NewRecorder() req = httptest.NewRequest(http.MethodPost, "/api/v1/release-change-sets", strings.NewReader(`{"name":"x"}`)) @@ -417,6 +427,9 @@ func TestClassifyRoleMatrix(t *testing.T) { http.MethodPost + " /api/v1/release-batches/rb-1/seal": roleEngineer, http.MethodPost + " /api/v1/test-publications/start": roleEngineer, http.MethodGet + " /api/v1/release-plan": roleViewer, + http.MethodGet + " /api/v1/scope-rules": roleViewer, + http.MethodPut + " /api/v1/scope-rules": roleEngineer, + http.MethodDelete + " /api/v1/scope-rules": roleEngineer, } for key, want := range cases { fields := strings.Fields(key) diff --git a/scripts/ci_checks.py b/scripts/ci_checks.py index 3aa219b..56a8ad4 100644 --- a/scripts/ci_checks.py +++ b/scripts/ci_checks.py @@ -249,6 +249,8 @@ def checks() -> list[Check]: [g, "test", "-count=1", "-json", "./..."], env=env, judge=judge_go_tests), Check("tests-web", "前端测试(vitest,必须真跑)", [py3, "scripts/ci_checks.py", "--internal-web-test"], env=env), + Check("tests-e2e", "Chromium 浏览器回归(CI 必需)", ["make", "tests-e2e-ci"], + env=env, required_in=("ci",), note="本地用 make tests-e2e,复用已安装的浏览器"), Check("contracts", "版本与变更记录契约(不许 SKIP;开发构建不许自称正式版)", [py3, "scripts/ci_checks.py", "--internal-contracts", g], env=env), Check("version", "CHANGELOG 与版本记录(四类小节无空栏目 + 正式 tag 与提交内版本一致)", @@ -356,8 +358,7 @@ def cmd_internal_web_test() -> int: def cmd_internal_contracts(go: str) -> int: """版本契约:结构 + 注入身份必须**真 PASS**(SKIP 判红), 并且**开发构建不许自称正式版**(负例:标成 release ⇒ 必须红)。""" - qa_rc = subprocess.run(py("-m", "unittest", "discover", "-s", "scripts", "-p", "test_qa_env.py", "-v"), - cwd=ROOT).returncode + qa_rc = subprocess.run(["make", "test-tooling"], cwd=ROOT).returncode if qa_rc: return qa_rc snapshot_rc = subprocess.run(py("scripts/development_snapshot.py","--self-test"),cwd=ROOT).returncode @@ -442,6 +443,7 @@ CHECK_MANIFEST: dict[str, tuple[str, ...]] = { "build-web": ("local", "ci"), "tests-go": ("local", "ci"), "tests-web": ("local", "ci"), + "tests-e2e": ("ci",), "contracts": ("local", "ci"), "version": ("local", "ci"), "security-secrets": ("local", "ci"), @@ -635,6 +637,14 @@ def main(argv: list[str]) -> int: print(f"\n── [{c.id}] {c.title} ──\n {'✅' if status == SUCCESS else ('⏭️' if status == SKIPPED else '❌')} {reason}") continue + if args.profile not in c.required_in: + reason = f"{args.profile} 档案不要求此项。{c.note}".strip() + results.append({"id": c.id, "title": c.title, "required_in": list(c.required_in), + "status": SKIPPED, "reason": reason, "command": "", + "exit_code": None, "duration_s": 0}) + print(f"\n── [{c.id}] {c.title} ──\n ⏭️ {reason}") + continue + print(f"\n── [{c.id}] {c.title} ──") print(" $ " + " ".join(c.argv)) t0 = time.time() diff --git a/scripts/qa_env.py b/scripts/qa_env.py index 0d2c226..2e1f39f 100644 --- a/scripts/qa_env.py +++ b/scripts/qa_env.py @@ -316,7 +316,7 @@ def qa_run(commit, env, db_env, versions, state): wait_service(web, WEB_PORT, '/', 'html', web_log) wait_service(web, WEB_PORT, '/health', 'ok', web_log) report('services', 'API /readyz、Web 首页与 Vite /health 代理通过') - run_step(['make', 'tests-go', 'tests-web'], 'tests', env, 300) + run_step(['make', 'tests-go', 'tests-web', 'tests-e2e'], 'tests', env, 300) check_checkout(commit) if api.poll() is not None or web.poll() is not None or not probe(WEB_PORT, '/health', 'ok'): raise QAError(f'服务在测试期间失去健康状态,日志目录 {log_dir}') diff --git a/scripts/test_changed.py b/scripts/test_changed.py new file mode 100644 index 0000000..782680c --- /dev/null +++ b/scripts/test_changed.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python3 +"""Select verification commands for staged changes.""" + +import argparse +from pathlib import Path, PurePosixPath +import re +import shlex +import subprocess +import sys + +ROOT = Path(__file__).resolve().parents[1] + + +def select_commands(paths, removed=()): + removed = set(removed) + full_go = full_web = browser = tooling = False + go_packages, web_tests = set(), set() + for name in paths: + path = PurePosixPath(name) + if name.startswith('docs/') or path.suffix == '.md': + continue + if name.startswith('web/e2e/') or name == 'web/playwright.config.ts': + browser = True + elif name.startswith('web/'): + if (name not in removed and name.startswith('web/src/') and + re.fullmatch(r'[\w./-]+\.test\.[jt]sx?', name)): + web_tests.add(str(path.relative_to('web'))) + else: + full_web = browser = True + elif name.startswith(('scripts/', '.githooks/')) or name == 'Makefile': + tooling = full_go = full_web = browser = True + elif path.name.endswith('_test.go') and name not in removed: + go_packages.add('./' + path.parent.as_posix()) + elif path.suffix == '.go' or name in ('go.mod', 'go.sum') or name.startswith(('internal/', 'cmd/')): + full_go = True + else: + full_go = full_web = browser = True + commands = [] + if tooling: + commands.append(['make', 'test-tooling']) + if full_go: + commands.append(['make', 'tests-go']) + elif go_packages: + commands.append(['go', 'test', '-count=1', *sorted(go_packages)]) + if full_web: + commands.append(['make', 'tests-web']) + elif web_tests: + commands.append(['npm', '--prefix', 'web', 'test', '--', *sorted(web_tests)]) + if browser: + commands.append(['make', 'tests-e2e']) + return commands + + +def staged_names(*args): + result = subprocess.check_output(['git', 'diff', '--cached', '--no-renames', '--name-only', '-z', *args], cwd=ROOT) + return [name for name in result.decode('utf-8', errors='surrogateescape').split('\0') if name] + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--list', action='store_true', help='print selected commands only') + args = parser.parse_args() + commands = select_commands(staged_names(), staged_names('--diff-filter=D')) + if not commands: + print('暂无需要执行测试的暂存文件') + return 0 + if not args.list: + unstaged = subprocess.check_output(['git', 'diff', '--name-only', '-z'], cwd=ROOT) + paths = [name for name in unstaged.decode('utf-8', errors='surrogateescape').split('\0') if name] + if select_commands(paths): + print('还有未暂存的代码或配置,先整理工作区后再检查暂存改动', file=sys.stderr) + return 1 + for cmd in commands: + print(shlex.join(cmd), flush=True) + if not args.list: + code = subprocess.run(cmd, cwd=ROOT, check=False).returncode + if code: + return code + return 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/scripts/test_test_changed.py b/scripts/test_test_changed.py new file mode 100644 index 0000000..5ee255d --- /dev/null +++ b/scripts/test_test_changed.py @@ -0,0 +1,31 @@ +import unittest + +from test_changed import select_commands + + +class SelectionTests(unittest.TestCase): + def test_documentation_only(self): + self.assertEqual(select_commands(['docs/DEVELOPMENT_AND_TESTING.md']), []) + + def test_go_test_only_selects_its_package(self): + self.assertEqual(select_commands(['internal/governance/a_test.go']), + [['go', 'test', '-count=1', './internal/governance']]) + + def test_go_source_and_migration_fall_back_to_suite(self): + for name in ('internal/governance/model.go', 'internal/platform/postgres/migrations/0099_qa.up.sql'): + self.assertEqual(select_commands([name]), [['make', 'tests-go']]) + + def test_frontend_test_only_selects_its_file(self): + self.assertEqual(select_commands(['web/src/scopeRules.test.ts']), + [['npm', '--prefix', 'web', 'test', '--', 'src/scopeRules.test.ts']]) + + def test_shared_frontend_code_covers_browser_suite(self): + self.assertEqual(select_commands(['web/src/scopeRules.ts']), + [['make', 'tests-web'], ['make', 'tests-e2e']]) + + def test_e2e_only_runs_browser_suite(self): + self.assertEqual(select_commands(['web/e2e/scope-rules.spec.ts']), [['make', 'tests-e2e']]) + + def test_unsafe_unknown_paths_fall_back_to_all_relevant_suites(self): + self.assertEqual(select_commands(['new-runtime.conf']), + [['make', 'tests-go'], ['make', 'tests-web'], ['make', 'tests-e2e']]) diff --git a/web/.prettierignore b/web/.prettierignore index 6922fe1..49e04eb 100644 --- a/web/.prettierignore +++ b/web/.prettierignore @@ -1,4 +1,6 @@ node_modules/ dist/ +test-results/ +playwright-report/ *.tsbuildinfo package-lock.json \ No newline at end of file diff --git a/web/e2e/scope-rules.html b/web/e2e/scope-rules.html new file mode 100644 index 0000000..6cd9d67 --- /dev/null +++ b/web/e2e/scope-rules.html @@ -0,0 +1,11 @@ + + +
+ +