[PR #5345] [MERGED] Test-quality audit: fix 2 prod bugs, strengthen weak tests, add mutation/fuzz/CI tooling #5852

Closed
opened 2026-11-02 04:04:33 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/5345
Author: @MHSanaei
Created: 6/15/2026
Status: ✅ Merged
Merged: 6/15/2026
Merged by: @MHSanaei

Base: main ← Head: chore/test-audit


📝 Commits (10+)

  • a10cb76 test(audit): add gremlins/rapid/coverage tooling + AUDIT.md scaffold
  • 06ec03e test(audit): hygiene sweep (race-clean except logger global; Finding #2) + smell inventory
  • 9d4062b test(audit): cover untested error/edge branches (TLS proxy+pin, migration tag cleanup=Finding #1)
  • 90d2a57 test(audit): strengthen internal/sub link tests (dedup key, TLS/Reality mapping, clash well-formedness)
  • 3df4b7a test(audit): property (rapid) + fuzz tests for joinHostPort/userinfo/pin/ParseLink
  • 35a52d9 test(audit): tighten frontend subSortIndex rejection assertions + wire coverage
  • 144b7b2 ci(audit): add shuffle gate + non-blocking race job (Finding #2) + fuzz-smoke; document mutation policy
  • c40bb92 chore(audit): gitignore frontend coverage output
  • c216cbe test(audit): exhaustive whole-repo pass — strengthen 5 weak/fake tests (netproxy, CSP, modal per-protocol loops, schema coercions)
  • 0803f81 docs(contributing): add Testing section (conventions, race/shuffle, fuzz, mutation policy); drop AUDIT.md ledger

📊 Changes

37 files changed (+2580 additions, -330 deletions)

View changed files

📝 .github/workflows/ci.yml (+34 -1)
➕ .github/workflows/mutation.yml (+62 -0)
📝 CONTRIBUTING.md (+43 -0)
📝 frontend/.gitignore (+1 -0)
📝 frontend/package-lock.json (+171 -0)
📝 frontend/package.json (+2 -1)
➖ frontend/src/test/__snapshots__/inbound-form-modal.test.tsx.snap (+0 -161)
➖ frontend/src/test/__snapshots__/outbound-form-modal.test.tsx.snap (+0 -141)
📝 frontend/src/test/inbound-form-adapter.test.ts (+12 -2)
📝 frontend/src/test/inbound-form-modal.test.tsx (+19 -4)
📝 frontend/src/test/outbound-form-modal.test.tsx (+21 -3)
📝 frontend/src/test/protocols.test.ts (+32 -0)
📝 go.mod (+1 -0)
📝 go.sum (+2 -0)
➕ internal/config/config_mutation_test.go (+75 -0)
📝 internal/logger/logger.go (+22 -5)
➕ internal/logger/logger_test.go (+30 -0)
➕ internal/mtproto/manager_mutation_test.go (+99 -0)
📝 internal/sub/clash_service_test.go (+72 -0)
➕ internal/sub/mutation_audit_test.go (+484 -0)

...and 17 more files

📄 Description

Summary

A test-quality audit of the existing suites: hunt for "fake green" tests (pass but don't guard behavior), strengthen them, and back it with objective mutation testing (gremlins). The deliverable is stronger tests — every strengthening was verified by mutating the production line and confirming the test fails.

No production logic was changed to satisfy a test. The only non-test prod changes are 2 genuine bug fixes (below) and one redundant dead-branch removal.

Production bug fixes (found by the audit)

  • logger data race — internal/logger: the in-memory logBuffer (written by addToBuffer, read by GetLogs) had no synchronization, so concurrent logging (cron jobs, the websocket hub, HTTP handlers) raced. Guarded with a sync.Mutex. The whole repo is now clean under go test -race -shuffle.
  • migration no-op — internal/web/service/inbound_migration.go: the legacy 0.0.0.0: tag cleanup used tx.Raw(...), which builds but never executes a non-SELECT statement, so the prefix was never stripped. Changed to tx.Exec(...).

Test strengthening (mutation efficacy, before → after, gremlins)

Package Before After
internal/util/common 66.7% 100%
internal/util/netsafe 80% 100%
internal/web/middleware 90% 100%
internal/web/runtime 79.3% 97.7%
internal/xray 84.8% 97.3%
internal/mtproto 81.8% 85.2%
internal/sub 72.0% 84.2%
internal/util/link 59.5% 79.8%
internal/database 89.4% 89.4%

Highlights: share-link field mapping (TLS/Reality), the dedup key, clash reality/tcp config, JSON-subscription mux/finalmask, node baseURL scheme/port-bounds + egress/content-type, xray hot-diff/process branches, link parser helpers. Remaining survivors are a documented long tail + equivalent mutants (e.g. timeout constants) that shouldn't be chased.

New tests & tooling

  • Property tests (pgregory.net/rapid): joinHostPort bracketing, encodeUserinfo round-trip, DecodeCertPin format-agnosticism, splitLinkLines.
  • Native fuzz: FuzzParseLink (share-link parser) and FuzzDecodeCertPin — seed corpora run under plain go test; timed exploration runs as a CI smoke step.
  • Frontend: tightened over-broad Zod/schema assertions and fixed two vacuous per-protocol snapshot loops (they captured identical DOM for every protocol); wired @vitest/coverage-v8.

CI

  • go-test now runs -shuffle=on -count=1.
  • New race job (-race -shuffle -count=1) — a hard gate (the repo is race-clean after the logger fix).
  • New fuzz-smoke job on the critical parsers.
  • New mutation.yml — nightly/on-demand scoped gremlins, results uploaded as artifacts (informational, not a gate).

Docs

  • CONTRIBUTING.md gains a Testing section: conventions, how to run race/shuffle/coverage/fuzz, and the scoped mutation-testing policy.

Verification

  • go build ./... ✅ · go test -race -shuffle=on -count=1 ./... → 0 FAIL ✅ · go vet ✅
  • Frontend test (537) + typecheck + lint ✅
  • Mutation scores above measured per the table (clean Docker golang:1.26 runs).

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/5345 **Author:** [@MHSanaei](https://github.com/MHSanaei) **Created:** 6/15/2026 **Status:** ✅ Merged **Merged:** 6/15/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `chore/test-audit` --- ### 📝 Commits (10+) - [`a10cb76`](https://github.com/MHSanaei/3x-ui/commit/a10cb7625da227435c9bbf156c4a0ab313e0eb9d) test(audit): add gremlins/rapid/coverage tooling + AUDIT.md scaffold - [`06ec03e`](https://github.com/MHSanaei/3x-ui/commit/06ec03eef89081d33a33d928566e4db637995729) test(audit): hygiene sweep (race-clean except logger global; Finding #2) + smell inventory - [`9d4062b`](https://github.com/MHSanaei/3x-ui/commit/9d4062b4ee29e8207250f1f0ba003e81f18b726c) test(audit): cover untested error/edge branches (TLS proxy+pin, migration tag cleanup=Finding #1) - [`90d2a57`](https://github.com/MHSanaei/3x-ui/commit/90d2a57b417ac4308b56bb9138e609c4cc8ae762) test(audit): strengthen internal/sub link tests (dedup key, TLS/Reality mapping, clash well-formedness) - [`3df4b7a`](https://github.com/MHSanaei/3x-ui/commit/3df4b7a2e4732ada6840b0e634b05b65c078bb31) test(audit): property (rapid) + fuzz tests for joinHostPort/userinfo/pin/ParseLink - [`35a52d9`](https://github.com/MHSanaei/3x-ui/commit/35a52d985b29b630af46ad56b22675697ad512d8) test(audit): tighten frontend subSortIndex rejection assertions + wire coverage - [`144b7b2`](https://github.com/MHSanaei/3x-ui/commit/144b7b2549fd31fbe95887cdbf1fdda847637bcf) ci(audit): add shuffle gate + non-blocking race job (Finding #2) + fuzz-smoke; document mutation policy - [`c40bb92`](https://github.com/MHSanaei/3x-ui/commit/c40bb928fa5f3aa7eeef34592cbb9ed3b4bca964) chore(audit): gitignore frontend coverage output - [`c216cbe`](https://github.com/MHSanaei/3x-ui/commit/c216cbeee067dba46a40852f0bcab80da3f663da) test(audit): exhaustive whole-repo pass — strengthen 5 weak/fake tests (netproxy, CSP, modal per-protocol loops, schema coercions) - [`0803f81`](https://github.com/MHSanaei/3x-ui/commit/0803f81948443aeee737a44b5eca8e5897df53fa) docs(contributing): add Testing section (conventions, race/shuffle, fuzz, mutation policy); drop AUDIT.md ledger ### 📊 Changes **37 files changed** (+2580 additions, -330 deletions) <details> <summary>View changed files</summary> 📝 `.github/workflows/ci.yml` (+34 -1) ➕ `.github/workflows/mutation.yml` (+62 -0) 📝 `CONTRIBUTING.md` (+43 -0) 📝 `frontend/.gitignore` (+1 -0) 📝 `frontend/package-lock.json` (+171 -0) 📝 `frontend/package.json` (+2 -1) ➖ `frontend/src/test/__snapshots__/inbound-form-modal.test.tsx.snap` (+0 -161) ➖ `frontend/src/test/__snapshots__/outbound-form-modal.test.tsx.snap` (+0 -141) 📝 `frontend/src/test/inbound-form-adapter.test.ts` (+12 -2) 📝 `frontend/src/test/inbound-form-modal.test.tsx` (+19 -4) 📝 `frontend/src/test/outbound-form-modal.test.tsx` (+21 -3) 📝 `frontend/src/test/protocols.test.ts` (+32 -0) 📝 `go.mod` (+1 -0) 📝 `go.sum` (+2 -0) ➕ `internal/config/config_mutation_test.go` (+75 -0) 📝 `internal/logger/logger.go` (+22 -5) ➕ `internal/logger/logger_test.go` (+30 -0) ➕ `internal/mtproto/manager_mutation_test.go` (+99 -0) 📝 `internal/sub/clash_service_test.go` (+72 -0) ➕ `internal/sub/mutation_audit_test.go` (+484 -0) _...and 17 more files_ </details> ### 📄 Description ## Summary A test-quality audit of the existing suites: hunt for "fake green" tests (pass but don't guard behavior), strengthen them, and back it with objective **mutation testing** (gremlins). The deliverable is *stronger tests* — every strengthening was verified by mutating the production line and confirming the test fails. **No production logic was changed to satisfy a test.** The only non-test prod changes are 2 genuine bug fixes (below) and one redundant dead-branch removal. ## Production bug fixes (found by the audit) - **logger data race** — `internal/logger`: the in-memory `logBuffer` (written by `addToBuffer`, read by `GetLogs`) had no synchronization, so concurrent logging (cron jobs, the websocket hub, HTTP handlers) raced. Guarded with a `sync.Mutex`. The whole repo is now clean under `go test -race -shuffle`. - **migration no-op** — `internal/web/service/inbound_migration.go`: the legacy `0.0.0.0:` tag cleanup used `tx.Raw(...)`, which builds but never executes a non-SELECT statement, so the prefix was never stripped. Changed to `tx.Exec(...)`. ## Test strengthening (mutation efficacy, before → after, gremlins) | Package | Before | After | |---|---|---| | `internal/util/common` | 66.7% | **100%** | | `internal/util/netsafe` | 80% | **100%** | | `internal/web/middleware` | 90% | **100%** | | `internal/web/runtime` | 79.3% | **97.7%** | | `internal/xray` | 84.8% | **97.3%** | | `internal/mtproto` | 81.8% | **85.2%** | | `internal/sub` | 72.0% | **84.2%** | | `internal/util/link` | 59.5% | **79.8%** | | `internal/database` | 89.4% | 89.4% | Highlights: share-link field mapping (TLS/Reality), the dedup key, clash reality/tcp config, JSON-subscription mux/finalmask, node `baseURL` scheme/port-bounds + egress/content-type, xray hot-diff/process branches, link parser helpers. Remaining survivors are a documented long tail + equivalent mutants (e.g. timeout constants) that shouldn't be chased. ## New tests & tooling - **Property tests** (`pgregory.net/rapid`): `joinHostPort` bracketing, `encodeUserinfo` round-trip, `DecodeCertPin` format-agnosticism, `splitLinkLines`. - **Native fuzz**: `FuzzParseLink` (share-link parser) and `FuzzDecodeCertPin` — seed corpora run under plain `go test`; timed exploration runs as a CI smoke step. - **Frontend**: tightened over-broad Zod/schema assertions and fixed two vacuous per-protocol snapshot loops (they captured identical DOM for every protocol); wired `@vitest/coverage-v8`. ## CI - `go-test` now runs `-shuffle=on -count=1`. - New **`race`** job (`-race -shuffle -count=1`) — a hard gate (the repo is race-clean after the logger fix). - New **`fuzz-smoke`** job on the critical parsers. - New **`mutation.yml`** — nightly/on-demand scoped gremlins, results uploaded as artifacts (informational, not a gate). ## Docs - `CONTRIBUTING.md` gains a **Testing** section: conventions, how to run race/shuffle/coverage/fuzz, and the scoped mutation-testing policy. ## Verification - `go build ./...` ✅ · `go test -race -shuffle=on -count=1 ./...` → 0 FAIL ✅ · `go vet` ✅ - Frontend `test` (537) + `typecheck` + `lint` ✅ - Mutation scores above measured per the table (clean Docker `golang:1.26` runs). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:04:33 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#5852
No description provided.