// Runnable smoke test for the JS/TS originclient SDK. // // Without ORIGIN_SERVICE_KEY set, it spins up a local mock check-in server // and exercises the client against it — no network, no credentials needed. // With ORIGIN_SERVICE_KEY set, it performs a real check-in against // ORIGIN_URL (or Origin's public endpoint) so the SDK can be smoke-tested // end-to-end against a live server. // // Run `npm run build` first (this imports the compiled dist/ output). // Usage: node examples/testclient.mjs [--watch] import { createServer } from "node:http"; import { OriginClient } from "../dist/index.js"; function envOr(key, def) { return process.env[key] || def; } async function startMockServer() { return new Promise((resolve) => { const server = createServer((req, res) => { let raw = ""; req.on("data", (chunk) => { raw += chunk; }); req.on("end", () => { const payload = JSON.parse(raw || "{}"); console.log( `mock server received check-in: type=${payload.type} name=${payload.name} ` + `install_id=${payload.unique_install_id} key=${req.headers["x-origin-service-key"]}`, ); res.writeHead(200, { "Content-Type": "application/json" }); res.end( JSON.stringify({ success: true, service_instance_id: 1, unique_install_id: payload.unique_install_id, status: "provisioning", pinged_at: "2026-08-01T00:00:00Z", }), ); }); }); server.listen(0, "127.0.0.1", () => { const address = server.address(); resolve({ server, url: `http://127.0.0.1:${address.port}` }); }); }); } async function main() { const watch = process.argv.includes("--watch"); const config = { url: process.env.ORIGIN_URL, serviceKey: process.env.ORIGIN_SERVICE_KEY, type: envOr("ORIGIN_TYPE", "icy2-testclient"), version: envOr("ORIGIN_VERSION", "0.0.0-test"), name: envOr("ORIGIN_NAME", "icy2-testclient-local"), site: process.env.ORIGIN_SITE, environment: process.env.ORIGIN_ENVIRONMENT, logger: { warn: (msg, err) => console.warn(`[warn] ${msg}:`, err) }, }; let mockServer; if (!config.serviceKey) { console.log("ORIGIN_SERVICE_KEY not set — using a local mock check-in server"); const { server, url } = await startMockServer(); mockServer = server; config.url = url; config.serviceKey = "test-key"; config.installId = "test-install-id"; } if (process.env.ORIGIN_INTERVAL_HOURS) { config.intervalHours = Number(process.env.ORIGIN_INTERVAL_HOURS); } const client = new OriginClient(config); try { if (watch) { console.log("watching — Ctrl-C to stop"); client.start(); await new Promise((resolve) => { process.on("SIGINT", resolve); process.on("SIGTERM", resolve); }); client.stop(); return; } const resp = await client.checkinOnce(); console.log(JSON.stringify(resp, null, 2)); } finally { mockServer?.close(); } } main().catch((err) => { console.error("checkin failed:", err); process.exitCode = 1; });