Files
SnapOtter/playwright.analytics-local.config.ts
SnapOtterandGitHub d10d0f544f fix: release QA hardening across processing, media, security, and CI gates (#649)
A release-readiness QA pass over the whole product. The commits split into
defects a user would hit and gates that were reporting green while measuring
nothing.

## Fixes that change behaviour

Rate limiting was bypassable on every install: TRUST_PROXY defaulted to true, so
request.ip came from a client-set header and a forged X-Forwarded-For got past
the login limiter. The default is now a private-network trust list.

A transient Postgres outage stranded in-flight jobs, leaving finished output on
disk with no row pointing at it. A reconciler now resolves those rows and adopts
the bytes rather than dropping the work.

A Redis connection that moved to a new address wedged every read-blocked
consumer, so completions stopped signalling while health still answered 200.
Socket timeouts plus subscriber pings recover it.

Installing more than one AI bundle left the shared venv multi-versioned and
silently broke three tools. The installer now reconciles distributions to one
version each.

Converting an image to JXL at quality 1 through 4 returned a 500, because
libjxl 0.7 rejects the distance those values compute. The quality is floored at
what the encoder honours. A missing ffmpeg was also reported to the user as a
corrupt upload; it now says the engine is unavailable.

RAW uploads reached an unpatched LibRaw on arm64, so it is built from source at
0.22.2, and the release scan was split so it can fail on an unfixed critical
instead of hiding it behind ignore-unfixed.

## Gates that could not fail

Two mutation lanes ran zero mutants because Stryker crawled the gitignored docs
build; coverage discarded its whole report on any failing test; the lint gate
skipped root tests, scripts, and two workspaces; and several generated matrices
counted a host missing ffmpeg as a passing tool. Each now measures what it
claims.

Full evidence and the outstanding release items are tracked locally and are not
part of this branch.
2026-07-27 15:37:30 +08:00

165 lines
5.6 KiB
TypeScript

import { randomBytes, randomInt } from "node:crypto";
import path from "node:path";
import { defineConfig, devices } from "@playwright/test";
import { resolvePlaywrightBackingState } from "./tests/playwright-backing-state.mjs";
function resolveRunId(): string {
const runId = process.env.PLAYWRIGHT_RUN_ID ?? `${process.pid}_${randomBytes(4).toString("hex")}`;
if (!/^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/.test(runId)) {
throw new Error(
`PLAYWRIGHT_RUN_ID must be 1-64 letters, digits, underscores, or hyphens and start with a letter or digit, received ${JSON.stringify(runId)}`,
);
}
process.env.PLAYWRIGHT_RUN_ID = runId;
return runId;
}
type E2eEndpoint = {
port: number;
url: string;
};
function parsePort(value: string, envName: string): number {
if (!/^\d+$/.test(value)) {
throw new Error(`${envName} must be an integer port, received "${value}"`);
}
const port = Number(value);
if (!Number.isSafeInteger(port) || port < 1024 || port > 65_535) {
throw new Error(`${envName} must be between 1024 and 65535, received "${value}"`);
}
return port;
}
function randomPort(min: number): number {
return randomInt(min, min + 10_000);
}
function resolveEndpoint(kind: "API" | "WEB", defaultPortFloor: number): E2eEndpoint {
const portEnvName = `PLAYWRIGHT_${kind}_PORT`;
const urlEnvName = `PLAYWRIGHT_${kind}_URL`;
const urlOverride = process.env[urlEnvName] ?? (kind === "API" ? process.env.API_URL : undefined);
const parsedOverride = urlOverride ? new URL(urlOverride) : undefined;
const port = process.env[portEnvName]
? parsePort(process.env[portEnvName], portEnvName)
: parsedOverride?.port
? parsePort(parsedOverride.port, urlEnvName)
: randomPort(defaultPortFloor);
const parsedUrl = parsedOverride ?? new URL(`http://127.0.0.1:${port}`);
if (parsedUrl.protocol !== "http:") {
throw new Error(`${urlEnvName} must use http, received "${parsedUrl.protocol}"`);
}
if (!["127.0.0.1", "localhost"].includes(parsedUrl.hostname)) {
throw new Error(`${urlEnvName} must use a loopback host, received "${parsedUrl.hostname}"`);
}
if (
parsedUrl.username ||
parsedUrl.password ||
parsedUrl.pathname !== "/" ||
parsedUrl.search ||
parsedUrl.hash
) {
throw new Error(`${urlEnvName} must be an origin without credentials, path, query, or hash`);
}
if (parsePort(parsedUrl.port || "80", urlEnvName) !== port) {
throw new Error(`${portEnvName} must match the port in ${urlEnvName}`);
}
const url = parsedUrl.origin;
process.env[portEnvName] = String(port);
process.env[urlEnvName] = url;
return { port, url };
}
const runId = resolveRunId();
const runRoot = path.join(__dirname, "test-results", "e2e-analytics-runs", runId);
const authFile = path.join(runRoot, "auth", "analytics-local-user.json");
process.env.PLAYWRIGHT_RUN_ROOT = runRoot;
process.env.PLAYWRIGHT_AUTH_FILE = authFile;
const apiEndpoint = resolveEndpoint("API", 20_000);
const webEndpoint = resolveEndpoint("WEB", 30_000);
process.env.API_URL = apiEndpoint.url;
const E2E_PG_BASE_URL =
process.env.E2E_PG_BASE_URL || "postgres://snapotter:snapotter@localhost:5432/snapotter";
const backingState = resolvePlaywrightBackingState({
postgresBaseUrl: E2E_PG_BASE_URL,
redisUrl: process.env.REDIS_URL ?? "redis://localhost:6379",
runId,
scope: "analytics-local",
});
export default defineConfig({
testDir: "./tests/e2e-analytics",
timeout: 30_000,
expect: { timeout: 10_000 },
fullyParallel: false,
retries: 0,
workers: 1,
outputDir: path.join(runRoot, "playwright-output"),
reporter: [["html", { open: "never", outputFolder: path.join(runRoot, "playwright-report") }]],
use: {
baseURL: webEndpoint.url,
screenshot: "only-on-failure",
trace: "retain-on-failure",
},
projects: [
{
name: "setup",
testMatch: /(?:^|[/\\])auth\.setup\.ts$/,
},
{
name: "chromium",
use: {
...devices["Desktop Chrome"],
storageState: authFile,
},
dependencies: ["setup"],
},
],
webServer: [
{
// NOTE: use the `start` script, not `dev`. The `dev` script hard-codes
// `PORT=13490`, which would override the isolated API endpoint below and
// collide with a developer's running dev server. `start` reads PORT from
// the env, so the PORT set in this webServer.env block is honored.
command: `node tests/playwright-api-lifecycle.mjs ${runId} analytics-local -- pnpm --filter @snapotter/api start`,
url: `${apiEndpoint.url}/api/v1/health`,
reuseExistingServer: false,
gracefulShutdown: { signal: "SIGTERM", timeout: 30_000 },
env: {
AUTH_ENABLED: "true",
DEFAULT_USERNAME: "admin",
DEFAULT_PASSWORD: "admin",
RATE_LIMIT_PER_MIN: "50000",
SKIP_MUST_CHANGE_PASSWORD: "true",
ANALYTICS_ENABLED: "true",
// Force the compile-time bake ON (dev/test only) so the effective
// analytics state is enabled by default and the opt-out toggle can be
// exercised end to end. bakedEnabled() honors this when
// NODE_ENV !== "production".
ANALYTICS_BAKED_OVERRIDE: "on",
DATABASE_URL: backingState.databaseUrl,
E2E_PG_BASE_URL: backingState.postgresBaseUrl,
REDIS_URL: backingState.redisUrl,
BULLMQ_PREFIX: backingState.bullmqPrefix,
PORT: String(apiEndpoint.port),
},
timeout: 30_000,
},
{
command: "pnpm --filter @snapotter/web dev",
url: webEndpoint.url,
reuseExistingServer: false,
env: {
PORT: String(webEndpoint.port),
VITE_API_URL: apiEndpoint.url,
},
timeout: 30_000,
},
],
});
export { authFile };