Test navigateur isolé du réseau de la machine ; zstd seulement si Node le sait

Le conteneur Playwright partageait le réseau de la machine : quand d'autres
conteneurs démarraient, Chromium abandonnait ses chargements
(net::ERR_NETWORK_CHANGED). Il a maintenant son réseau, et le vrai serveur
tourne à côté de lui. Ce Node (20) n'a pas zstd : le serveur renvoyait 500.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Pierre MARTIN
2026-11-23 03:00:34 +01:00
co-authored by Claude Opus 5.5
parent b037fbd67f
commit 442b3f0a56
4 changed files with 42 additions and 12 deletions
+6 -8
View File
@@ -110,15 +110,13 @@ test-browser: $(PLAYWRIGHT) $(QR_READER) ## Parcours Playwright de l'app, captur
@tmp="$$(mktemp -d)"; data="$$tmp/data"; site="$$tmp/build/site"; \ @tmp="$$(mktemp -d)"; data="$$tmp/data"; site="$$tmp/build/site"; \
trap 'kill $$pid 2>/dev/null; rm -rf -- "$$tmp"' EXIT; \ trap 'kill $$pid 2>/dev/null; rm -rf -- "$$tmp"' EXIT; \
$(MAKE) --no-print-directory site SITE_BUILD="$$tmp/build" >/dev/null || exit 1; \ $(MAKE) --no-print-directory site SITE_BUILD="$$tmp/build" >/dev/null || exit 1; \
port="$$(node -e 'const s=require("node:net").createServer().listen(0,"127.0.0.1",()=>{console.log(s.address().port);s.close()})')"; \ mkdir -p "$$data"; \
PORT="$$port" SITE_DIR="$$site" DATA_DIR="$$data" PUBLIC_URL="http://127.0.0.1:$$port" \ docker run --rm --init --ipc=host --network bridge -u "$$(id -u):$$(id -g)" -e HOME=/tmp \
node server/main.js >"$$tmp/server.log" 2>&1 & pid=$$!; \ -v "$(CURDIR)/$(APP_DIR):/repo/app:ro" -v "$(CURDIR)/server:/repo/server:ro" -v "$(CURDIR)/package.json:/repo/package.json:ro" \
for _ in $$(seq 50); do curl -sf -o /dev/null "http://127.0.0.1:$$port/" && break; sleep 0.1; done; \ -v "$(CURDIR)/tests/browser:/tests:ro" \
curl -sf -o /dev/null "http://127.0.0.1:$$port/" || { echo "Le serveur ne démarre pas :"; cat "$$tmp/server.log"; exit 1; }; \ -v "$$site:/site" -v "$$data:/data" \
docker run --rm --init --ipc=host --network host -u "$$(id -u):$$(id -g)" -e HOME=/tmp \
-v "$(CURDIR)/$(APP_DIR):/app:ro" -v "$(CURDIR)/tests/browser:/tests:ro" -v "$$site:/site" \
-v "$(CURDIR)/$(BROWSER_TOOLS):/tools:ro" -v "$(CURDIR)/build/shots:/shots" \ -v "$(CURDIR)/$(BROWSER_TOOLS):/tools:ro" -v "$(CURDIR)/build/shots:/shots" \
$(PLAYWRIGHT_IMAGE) node /tests/parcours.cjs /app /shots "http://127.0.0.1:$$port/" $(PLAYWRIGHT_IMAGE) sh /tests/run.sh
test-site: ## Construit l'image de production en local et vérifie ce qu'elle sert (Docker) test-site: ## Construit l'image de production en local et vérifie ce qu'elle sert (Docker)
@tests/test_site.sh @tests/test_site.sh
+8 -3
View File
@@ -34,12 +34,17 @@ const COMPRESSIBLE = /^(text\/|application\/json|application\/manifest\+json|ima
/** Comme Caddy : en dessous, la compression ne fait rien gagner. */ /** Comme Caddy : en dessous, la compression ne fait rien gagner. */
const MIN_COMPRESS = 512; const MIN_COMPRESS = 512;
/** zstd n'existe dans Node qu'à partir de 22.15 (l'image de production a Node 24). */
export const HAS_ZSTD = typeof (/** @type {any} */ (zlib)).zstdCompressSync === "function";
/** /**
* Encodage choisi d'après Accept-Encoding : zstd, sinon gzip, sinon aucun. * Encodage choisi d'après Accept-Encoding : zstd (si ce Node le sait), sinon gzip,
* sinon aucun.
* @param {string|undefined} header * @param {string|undefined} header
* @param {boolean} [zstd] ce Node sait compresser en zstd
* @returns {"zstd"|"gzip"|null} * @returns {"zstd"|"gzip"|null}
*/ */
export function pickEncoding(header) { export function pickEncoding(header, zstd = HAS_ZSTD) {
/** @type {Map<string, number>} */ /** @type {Map<string, number>} */
const accepted = new Map(); const accepted = new Map();
for (const part of (header ?? "").split(",")) { for (const part of (header ?? "").split(",")) {
@@ -49,7 +54,7 @@ export function pickEncoding(header) {
accepted.set(name, q ? Number(q[1]) : 1); accepted.set(name, q ? Number(q[1]) : 1);
} }
const ok = (/** @type {string} */ name) => (accepted.get(name) ?? accepted.get("*") ?? 0) > 0; const ok = (/** @type {string} */ name) => (accepted.get(name) ?? accepted.get("*") ?? 0) > 0;
if (ok("zstd")) return "zstd"; if (zstd && ok("zstd")) return "zstd";
if (ok("gzip")) return "gzip"; if (ok("gzip")) return "gzip";
return null; return null;
} }
+24
View File
@@ -0,0 +1,24 @@
#!/bin/sh
# Lancé par make test-browser dans le conteneur Playwright, sur son propre réseau
# (--network bridge) : il a ses interfaces à lui. Avant, le conteneur partageait le réseau de la machine
# (--network host) : quand d'autres conteneurs démarraient ou s'arrêtaient, Chromium
# voyait les interfaces changer et abandonnait ses chargements
# (net::ERR_NETWORK_CHANGED, page restée vide après un rechargement). Sans réseau du
# tout (--network none), Chromium se croit hors ligne et la synchro ne part pas.
# Le vrai serveur (synchro, MCP, site assemblé) tourne donc ici, à côté du navigateur.
set -eu
port=8787
PORT="$port" SITE_DIR=/site DATA_DIR=/data PUBLIC_URL="http://127.0.0.1:$port" \
node /repo/server/main.js >/data/server.log 2>&1 &
pid=$!
trap 'kill $pid 2>/dev/null || true' EXIT
# Attendre que le serveur réponde (pas de curl dans l'image : node suffit).
node -e '
const url = process.argv[1];
const until = Date.now() + 10000;
(async function wait() {
try { if ((await fetch(url)).ok) process.exit(0); } catch {}
if (Date.now() > until) process.exit(1);
setTimeout(wait, 100);
})();' "http://127.0.0.1:$port/" || { echo "Le serveur ne démarre pas :"; cat /data/server.log; exit 1; }
node /tests/parcours.cjs /repo/app /shots "http://127.0.0.1:$port/"
+4 -1
View File
@@ -50,7 +50,10 @@ test("site : compression zstd ou gzip des textes, pas des polices", async () =>
const f = /** @type {any} */ (await get("/police.woff2", "gzip")); const f = /** @type {any} */ (await get("/police.woff2", "gzip"));
assert.equal(f.headers["content-encoding"], undefined); assert.equal(f.headers["content-encoding"], undefined);
assert.equal(pickEncoding("gzip;q=0, zstd;q=0"), null); assert.equal(pickEncoding("gzip;q=0, zstd;q=0"), null);
assert.equal(pickEncoding("*"), "zstd"); assert.equal(pickEncoding("*", true), "zstd");
// Un Node sans zstd (avant 22.15) : gzip, plutôt qu'une erreur 500.
assert.equal(pickEncoding("zstd, gzip", false), "gzip");
assert.equal(pickEncoding("zstd", false), null);
assert.equal(pickEncoding(undefined), null); assert.equal(pickEncoding(undefined), null);
} finally { } finally {
await s.close(); await s.close();