Files
openclaw/test/scripts/clawhub-fixture-server.test.ts
Peter Steinberger fe261b0f59 chore(tooling): typecheck root test/** with a dedicated tsgo lane (#104475)
* chore(types): add declaration files for scripts/lib and scripts/e2e modules

* chore(types): add declaration files for top-level script modules (a-m)

* chore(types): add declaration files for top-level script modules (n-z)

* test: use a non-secret-shaped gateway token fixture

* test: type ci workflow guard helpers for the root test lane

* chore(tooling): typecheck root test/** with a dedicated tsgo lane

- test/tsconfig/tsconfig.test.root.json: root-test program (strict unused checks,
  fixtures excluded; two Docker E2E clients that import built dist/** stay out,
  same rationale as the scripts/e2e exclusion in tsconfig.scripts.json)
- tsgo:test:root wired into tsgo:test, check:test-types, scripts/check.mjs, and
  the ci.yml test-types shard, mirroring the tsgo:scripts lane (#104348)
- changed-lane routing: test/**/*.ts (excluding fixtures) and the lane tsconfig
  now trigger 'typecheck test root' in check:changed; previously test/ paths ran
  lint only, so harness type errors surfaced first in CI (#104287 envDir case)
- burn down all 1071 latent type errors in the program: precise param/local
  types across test/scripts, test/vitest, test/e2e, and transitive scripts/e2e
  program members; 205 sibling .d.mts declaration files for imported .mjs
  modules (committed separately); zero any, zero ts-expect-error
- resolve the pre-existing testing star-export ambiguity in
  scripts/e2e/parallels/common.ts with an explicit re-export

Closes #104388

* chore(types): correct declaration fidelity per structured review

- re-derive 51 .d.mts files from implementation data flow instead of
  initializers: fix a wrong never return (runTestProjectsDelegation returns
  the child), add encoding-sensitive exec/spawn overloads (plain-gh), restore
  the full release profile union, make parsed paths string | null, add missing
  parseArgs fields via help/non-help unions, add a missing sibling declaration
  (budget-number-args), drop 15 unused lint directives
- precise install-record/tuple typing removes the type-aware oxlint
  regressions the first declarations caused in scripts/e2e implementations
- route .mts declaration edits under test/ to the testRoot lane and reference
  the test-root project from tsconfig.projects.json so tsgo:all covers it
  (closes both review findings against the lane wiring)

* chore(scripts): keep telegram runner dist typing structural for the boundary guard

* chore(types): declare runtime pack and gateway readiness exports added on main

* test: pin the importTargetPlan form of the plugin-contract plan import

The guard expectation still referenced the raw await import( form that
7ae5996bb3 (#103975) replaced with the importTargetPlan fallback helper;
the assertion fails on current main.
2026-07-11 06:15:41 -07:00

128 lines
4.6 KiB
TypeScript

// ClawHub Fixture Server tests cover the local package fixture HTTP contract.
import { spawn, spawnSync, type ChildProcessByStdio } from "node:child_process";
import { existsSync, readFileSync } from "node:fs";
import path from "node:path";
import type { Readable } from "node:stream";
import { setTimeout as delay } from "node:timers/promises";
import { afterEach, describe, expect, it } from "vitest";
import { cleanupTempDirs, makeTempDir } from "../helpers/temp-dir.js";
const SCRIPT_PATH = "scripts/e2e/lib/clawhub-fixture-server.cjs";
const PACKAGE_NAME = "@openclaw/kitchen-sink";
const PACKAGE_PATH = `/api/v1/packages/${encodeURIComponent(PACKAGE_NAME)}`;
const KITCHEN_SINK_VERSION = "0.2.5";
const tempDirs: string[] = [];
type FixtureServerChild = ChildProcessByStdio<null, Readable, Readable>;
const servers: FixtureServerChild[] = [];
afterEach(async () => {
await Promise.all(servers.splice(0).map(stopServer));
cleanupTempDirs(tempDirs);
});
function collectStream(stream: NodeJS.ReadableStream) {
let text = "";
stream.setEncoding("utf8");
stream.on("data", (chunk: string) => {
text += chunk;
});
return () => text;
}
async function stopServer(child: FixtureServerChild) {
if (child.exitCode !== null || child.signalCode !== null) {
return;
}
const exited = new Promise<void>((resolve) => {
child.once("exit", () => resolve());
});
child.kill("SIGTERM");
await Promise.race([exited, delay(1_000)]);
if (child.exitCode === null && child.signalCode === null) {
child.kill("SIGKILL");
await exited;
}
}
async function startFixtureServer(profile: string) {
const root = makeTempDir(tempDirs, "openclaw-clawhub-fixture-server-");
const portFile = path.join(root, "port");
const child = spawn(process.execPath, [SCRIPT_PATH, profile, portFile], {
cwd: process.cwd(),
env: { ...process.env },
stdio: ["ignore", "pipe", "pipe"],
});
const readStdout = collectStream(child.stdout);
const readStderr = collectStream(child.stderr);
servers.push(child);
for (let attempt = 0; attempt < 100; attempt += 1) {
if (existsSync(portFile)) {
const port = Number(readFileSync(portFile, "utf8"));
if (Number.isInteger(port) && port > 0) {
return { baseUrl: `http://127.0.0.1:${port}` };
}
}
if (child.exitCode !== null) {
throw new Error(`fixture server exited early: stdout=${readStdout()} stderr=${readStderr()}`);
}
await delay(25);
}
throw new Error(`fixture server did not write a port: stderr=${readStderr()}`);
}
async function fetchJson(baseUrl: string, requestPath: string) {
const response = await fetch(`${baseUrl}${requestPath}`);
expect(response.status).toBe(200);
return response.json();
}
describe("ClawHub fixture server", () => {
it("serves package metadata and npm-pack artifacts for kitchen-sink fixtures", async () => {
const { baseUrl } = await startFixtureServer("kitchen-sink-plugin");
const packageDetail = await fetchJson(baseUrl, PACKAGE_PATH);
expect(packageDetail.package.name).toBe(PACKAGE_NAME);
expect(packageDetail.package.latestVersion).toBe(KITCHEN_SINK_VERSION);
expect(packageDetail.package.artifact.format).toBe("tgz");
const versionDetail = await fetchJson(
baseUrl,
`${PACKAGE_PATH}/versions/${KITCHEN_SINK_VERSION}/artifact`,
);
expect(versionDetail.artifact).toMatchObject({
artifactKind: "npm-pack",
packageName: PACKAGE_NAME,
source: "clawhub",
version: KITCHEN_SINK_VERSION,
});
const artifactResponse = await fetch(
`${baseUrl}${PACKAGE_PATH}/versions/${KITCHEN_SINK_VERSION}/artifact/download`,
);
expect(artifactResponse.status).toBe(200);
expect(artifactResponse.headers.get("x-clawhub-artifact-type")).toBe("npm-pack-tarball");
expect(artifactResponse.headers.get("x-clawhub-artifact-sha256")).toMatch(/^[a-f0-9]{64}$/u);
expect(Buffer.from(await artifactResponse.arrayBuffer()).length).toBeGreaterThan(100);
const missingResponse = await fetch(`${baseUrl}/missing`);
expect(missingResponse.status).toBe(404);
const methodResponse = await fetch(`${baseUrl}${PACKAGE_PATH}`, { method: "POST" });
expect(methodResponse.status).toBe(405);
});
it("rejects missing startup arguments before binding a fixture server", () => {
const result = spawnSync(process.execPath, [SCRIPT_PATH], {
cwd: process.cwd(),
encoding: "utf8",
env: { ...process.env },
});
expect(result.status).toBe(1);
expect(result.stderr).toContain(
"usage: clawhub-fixture-server.cjs <kitchen-sink-plugin|plugins> <port-file>",
);
});
});