Skip to content

Commit 8ccc736

Browse files
authored
test: add check-ui guards for behaviour a successful build does not demonstrate (#1641)
Fixes #1581. Part of #1578. Step 3 of 3, not required to boot. Depends on #1616 (the Vite cutover, now merged into `dev`). Three checks, wired as `make check-ui`, for properties that a green build, a working dev server and a compiling binary do not demonstrate. Each one was observed failing on the failure it guards before being relied on. **Asset paths, `make check-ui-assets`.** `script/check-built-assets.sh` builds the frontend and runs `TestGetStyleResolvesBuiltAssets` from step 1. Under `--self-check` it rewrites the built `index.html` with four shapes the server cannot use (a stylesheet link but no script `src`, a script but no stylesheet link, an inline-only script, and a non-stylesheet `<link rel="manifest">` that must not be miscounted as a stylesheet), asserts the check fails on each, and restores the real build afterwards, so a check that quietly stopped asserting anything is distinguishable from a passing one. It also fails if the Go test it depends on is ever renamed or removed, instead of silently matching nothing. **Non-default locale, `make check-ui-locales`.** `ui/scripts/check-locale-resolution.js` starts the real dev server from the real config and imports `ui/scripts/locale-probe.js`, which carries a copy of the application's own template-literal `@i18n` import, through the server's module runner. It asserts two separate properties, because only one of them is about resolution: the module graph resolves the alias and parses a language file that lives outside the frontend root, and a browser is allowed to fetch it, which depends on the dev server's filesystem allow-list. Checking only the first passes while every language 403s in a browser. It requires `zh_CN` and `en_US` to resolve to distinct translated content. **Plugin i18n order, `make check-ui-plugin-i18n`.** `ui/scripts/check-plugin-i18n-order.js` loads the plugin helper without touching the application's i18n bootstrap, so `i18next` is guaranteed uninitialised, registers a bundle, then initialises, and separately exercises the immediate path where an initialised instance already exists. Two assertions in each branch, because either alone passes while the feature is broken: registering must not throw, and the translations must actually be present afterwards. It also asserts exactly one `i18next` module is loaded, so the harness cannot silently measure an object nothing under test writes to. **All three fail closed.** Each check presets its result to failure and only reports success once it has proved it. The two Node scripts bound every server-creation, listen and close call with its own timeout and add an unreferenced whole-run watchdog, so a hang cannot drain the event loop into an accidental success instead of a reported failure. **Small follow-ups.** `src/components/Comment/index.scss` declares its properties before nested rules, clearing the one app-own Sass mixed-declarations warning the step 2 build printed; output is unchanged. `pages/403` is repointed at `pages/404/403`, an existing component, so that route renders instead of failing through the error boundary. The comments on `check-built-assets.sh` state the actual constraint (tag shape does not affect parsing; the check fails when a build is present but a required tag is missing). Agentic tooling did the mechanical work in this series; every change was reviewed by a human before being committed. ### Verification On `dev` at `6c00c788`, which includes steps 1 and 2, plus these commits: `pnpm install --frozen-lockfile` under the pinned `pnpm@9.7.0`; `make check-ui` green on all three targets; `./script/check-built-assets.sh --self-check` reports all four fixtures failing as intended and the real build restored, with `TestGetStyleResolvesBuiltAssets` passing again afterwards. The three targets print `--- PASS: TestGetStyleResolvesBuiltAssets`, `OK: zh_CN and en_US resolve at runtime to distinct translated resources, and zh_CN is fetchable over the dev server`, and `OK: plugin translations registered before i18next.init (deferred) and after i18next.init (immediate) both survive and are present`; the self-check reports `check failed as expected` for each of the four fixtures and ends with `==> self-check passed`. The build in this tree no longer prints the Sass mixed-declarations warning that step 2's build printed. None of this adds a CI job; the constraint for wiring one is in the tracking issue. The commits carry `(cherry picked from commit ...)` lines pointing at the branch behind #1567, where these checks were first reviewed.
1 parent f119282 commit 8ccc736

7 files changed

Lines changed: 673 additions & 2 deletions

File tree

‎Makefile‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: build clean ui
1+
.PHONY: build clean ui check-ui check-ui-assets check-ui-locales check-ui-plugin-i18n
22

33
VERSION=2.0.3
44
BIN=answer
@@ -47,6 +47,22 @@ check:
4747
test:
4848
@$(GO) test ./internal/repo/repo_test
4949

50+
# Frontend checks for behaviour a successful build does not demonstrate.
51+
# Each guards a runtime failure that leaves every build step reporting success.
52+
check-ui: check-ui-assets check-ui-locales check-ui-plugin-i18n
53+
54+
# The server reads the built asset paths out of index.html.
55+
check-ui-assets:
56+
@./script/check-built-assets.sh
57+
58+
# The app loads languages other than the default one through a dynamic import.
59+
check-ui-locales:
60+
@cd ui && pnpm check-locales
61+
62+
# Plugin translations register while modules evaluate, in bundler-decided order.
63+
check-ui-plugin-i18n:
64+
@cd ui && pnpm check-plugin-i18n
65+
5066
# clean all build result
5167
clean:
5268
@$(GO) clean ./...

‎script/check-built-assets.sh‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/bin/bash
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
19+
# Builds the frontend and asserts the server can still find the built assets
20+
# inside index.html. See internal/controller/template_controller_test.go for
21+
# why that is not implied by a successful build.
22+
#
23+
# --skip-build reuse an existing ui/build, do not rebuild
24+
# --self-check additionally rewrite ui/build/index.html so a script
25+
# or stylesheet tag is missing, and confirm the check
26+
# fails on the missing asset. Restores the real build
27+
# output afterwards.
28+
29+
set -euo pipefail
30+
31+
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
32+
INDEX_HTML="$REPO_ROOT/ui/build/index.html"
33+
TEST_PACKAGE="./internal/controller/"
34+
TEST_NAME="TestGetStyleResolvesBuiltAssets"
35+
36+
list_output="$(cd "$REPO_ROOT" && go test "$TEST_PACKAGE" -list "^${TEST_NAME}$" 2>&1)"
37+
if ! grep -qx "$TEST_NAME" <<<"$list_output"; then
38+
echo "no test named $TEST_NAME in $TEST_PACKAGE; go test -run with a stale/renamed test name matches nothing and still exits 0, which would turn this check into a silent no-op" >&2
39+
exit 1
40+
fi
41+
42+
skip_build=0
43+
self_check=0
44+
for arg in "$@"; do
45+
case "$arg" in
46+
--skip-build) skip_build=1 ;;
47+
--self-check) self_check=1 ;;
48+
*) echo "unknown option: $arg" >&2; exit 2 ;;
49+
esac
50+
done
51+
52+
run_check() {
53+
(cd "$REPO_ROOT" && go test -count=1 "$TEST_PACKAGE" -run "$TEST_NAME" "$@")
54+
}
55+
56+
if [ "$skip_build" -eq 0 ]; then
57+
echo "==> building frontend"
58+
(cd "$REPO_ROOT/ui" && pnpm build)
59+
fi
60+
61+
if [ ! -f "$INDEX_HTML" ]; then
62+
echo "no built index.html at $INDEX_HTML; run without --skip-build" >&2
63+
exit 1
64+
fi
65+
66+
echo "==> checking the server can parse the built asset tags"
67+
run_check -v
68+
69+
if [ "$self_check" -eq 0 ]; then
70+
exit 0
71+
fi
72+
73+
# Confirm the check actually fails when a required asset is missing from
74+
# the build output. Without this, a check that silently stopped asserting
75+
# anything would look identical to a passing one.
76+
backup="$(mktemp)"
77+
cp "$INDEX_HTML" "$backup"
78+
trap 'cp "$backup" "$INDEX_HTML"; rm -f "$backup"' EXIT
79+
80+
expect_failure() {
81+
local label="$1"
82+
local html="$2"
83+
printf '%s' "$html" > "$INDEX_HTML"
84+
echo "==> self-check: expecting failure on $label"
85+
if run_check >/dev/null 2>&1; then
86+
echo "SELF-CHECK FAILED: the check passed on $label, so it is not guarding anything" >&2
87+
exit 1
88+
fi
89+
echo " check failed as expected"
90+
}
91+
92+
expect_failure "stylesheet link but no script src" \
93+
'<!doctype html><html><head><meta charset="utf-8"/><link rel="stylesheet" crossorigin href="/static/css/index-e5f6a7b8.css"></head><body><div id="root"></div></body></html>'
94+
95+
expect_failure "script src but no stylesheet link" \
96+
'<!doctype html><html><head><meta charset="utf-8"/><script type="module" crossorigin src="/static/js/index-a1b2c3d4.js"></script></head><body><div id="root"></div></body></html>'
97+
98+
expect_failure "only an inline script, no src" \
99+
'<!doctype html><html><head><meta charset="utf-8"/><link rel="stylesheet" crossorigin href="/static/css/index-e5f6a7b8.css"></head><body><div id="root"></div><script>window.__inline=1;</script></body></html>'
100+
101+
expect_failure "manifest link but no stylesheet link" \
102+
'<!doctype html><html><head><meta charset="utf-8"/><script type="module" crossorigin src="/static/js/index-a1b2c3d4.js"></script><link rel="manifest" href="/manifest.json"></head><body><div id="root"></div></body></html>'
103+
104+
echo "==> self-check passed"

‎ui/package.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
"build:packages": "pnpm -r --filter=./src/plugins/* run build",
1313
"clean": "rm -rf node_modules && rm -rf src/plugins/**/node_modules",
1414
"analyze": "source-map-explorer 'build/static/js/*.js'",
15+
"check-locales": "node ./scripts/check-locale-resolution.js",
16+
"check-plugin-i18n": "node ./scripts/check-plugin-i18n-order.js",
1517
"setup-lint": "node scripts/setup-eslint.js && cd .. && husky install",
1618
"lint": "eslint . --cache --fix --ext .ts,.tsx",
1719
"prettier": "prettier --write \"src/**/*.{ts,tsx,css,scss,md}\"",

0 commit comments

Comments
 (0)