Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

chore(ci): add smoke test to catch runtime failures (#1307)

Corentin Thomassetcfd18d20814a
diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml
index f8add1b2..e9b69432 100644
--- a/.github/workflows/ci.yaml
+++ b/.github/workflows/ci.yaml
@@ -48,5 +48,9 @@ jobs:
- name: Build the apps
run: pnpm -r --parallel -F "./apps/*" build
+ # Boot the built server bundle to catch runtime-only failures the build step cannot detect
+ - name: Smoke test the server
+ run: pnpm -F "@papra/app-server" test:smoke
+
- name: Ensure no non-excluded files are changed for the whole repo
run: git diff --exit-code > /dev/null || (echo "After running the CI, some un-committed changes were detected. Please ensure cleanness before merging." && exit 1)
diff --git a/apps/papra-server/package.json b/apps/papra-server/package.json
index a65b3f8d..0041b60b 100644
--- a/apps/papra-server/package.json
+++ b/apps/papra-server/package.json
@@ -25,6 +25,7 @@
"test:int": "vitest run .int.test.ts",
"test:int:watch": "vitest watch '**/*.int.test.ts'",
"test:int:pull-images": "tsx --env-file-if-exists=.env src/scripts/pull-test-container-images.script.ts | crowlog-pretty",
+ "test:smoke": "tsx src/scripts/smoke-test.script.ts",
"typecheck": "tsc --noEmit",
"migrate:up": "tsx --env-file-if-exists=.env src/scripts/migrate-up.script.ts | crowlog-pretty",
"migrate:down": "tsx --env-file-if-exists=.env src/scripts/migrate-down.script.ts | crowlog-pretty",
diff --git a/apps/papra-server/src/scripts/smoke-test.script.ts b/apps/papra-server/src/scripts/smoke-test.script.ts
new file mode 100644
index 00000000..9721827c
--- /dev/null
+++ b/apps/papra-server/src/scripts/smoke-test.script.ts
@@ -0,0 +1,109 @@
+import type { ChildProcess } from 'node:child_process';
+import { spawn } from 'node:child_process';
+import { once } from 'node:events';
+import { mkdtempSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import process from 'node:process';
+import { setTimeout as sleep } from 'node:timers/promises';
+import { runScript } from './commons/run-script';
+
+const SMOKE_TEST_PORT = 14522;
+const READINESS_TIMEOUT_MS = 10_000;
+const POLL_INTERVAL_MS = 250;
+const SHUTDOWN_GRACE_MS = 5_000;
+const PING_URL = `http://127.0.0.1:${SMOKE_TEST_PORT}/api/ping`;
+
+// Thrown when the server is not yet accepting connections; the only case worth retrying.
+class ServerNotReadyYetError extends Error {}
+
+async function pingServer(): Promise<void> {
+ let response: Response;
+
+ try {
+ response = await fetch(PING_URL);
+ } catch (error) {
+ // Connection refused/reset while the server is still booting: keep polling.
+ throw new ServerNotReadyYetError('Server not accepting connections yet', { cause: error });
+ }
+
+ const body = (await response.json()) as { status: string } | undefined;
+
+ if (response.status !== 200 || body?.status !== 'ok') {
+ throw new Error(
+ `Unexpected /api/ping response: status=${response.status} body=${JSON.stringify(body)}`,
+ );
+ }
+}
+
+async function waitForServerReady({ child }: { child: ChildProcess }): Promise<void> {
+ const deadline = Date.now() + READINESS_TIMEOUT_MS;
+
+ while (Date.now() < deadline) {
+ if (child.exitCode !== null) {
+ throw new Error(
+ `Server process exited early with code ${child.exitCode} before becoming ready`,
+ );
+ }
+
+ try {
+ await pingServer();
+ return;
+ } catch (error) {
+ // Only transient connection errors are retried; parsing/HTTP/runtime errors surface immediately.
+ if (!(error instanceof ServerNotReadyYetError)) {
+ throw error;
+ }
+ }
+
+ await sleep(POLL_INTERVAL_MS);
+ }
+
+ throw new Error(`Server did not become ready within ${READINESS_TIMEOUT_MS}ms`);
+}
+
+// Terminate the child, escalating to SIGKILL if it does not exit within the grace period,
+// so a stubborn process can never hang the CI job.
+async function terminateServer({ child }: { child: ChildProcess }): Promise<void> {
+ if (child.exitCode !== null || child.signalCode !== null) {
+ return;
+ }
+
+ const exited = once(child, 'exit');
+ child.kill('SIGTERM');
+
+ const timedOut = await Promise.race([
+ exited.then(() => false),
+ sleep(SHUTDOWN_GRACE_MS).then(() => true),
+ ]);
+
+ if (timedOut) {
+ child.kill('SIGKILL');
+ await exited;
+ }
+}
+
+await runScript({ scriptName: 'smoke-test' }, async ({ logger }) => {
+ const storageRoot = mkdtempSync(join(tmpdir(), 'papra-smoke-'));
+
+ const child = spawn(process.execPath, ['dist/index.js'], {
+ stdio: ['ignore', 'inherit', 'inherit'],
+ env: {
+ ...process.env,
+ NODE_ENV: 'production',
+ AUTH_SECRET: 'smoke-test-secret-not-used-for-real-auth',
+ PROCESS_MODE: 'web',
+ PORT: String(SMOKE_TEST_PORT),
+ DATABASE_URL: 'file::memory:',
+ DOCUMENT_STORAGE_FILESYSTEM_ROOT: storageRoot,
+ },
+ });
+
+ try {
+ await waitForServerReady({ child });
+ logger.info({ port: SMOKE_TEST_PORT }, 'Built server booted and /api/ping returned ok');
+ } finally {
+ await terminateServer({ child });
+ rmSync(storageRoot, { recursive: true, force: true });
+ }
+});