Private
Public Access
Add runnable smoke-test clients for each SDK
CI / js (push) Successful in 18s
CI / go (push) Successful in 36s
CI / rust (push) Successful in 2m3s
CI / js (push) Successful in 18s
CI / go (push) Successful in 36s
CI / rust (push) Successful in 2m3s
Each package gets a small example program that exercises the real client: with no ORIGIN_SERVICE_KEY set it runs against a local mock check-in server, otherwise it checks in against ORIGIN_URL/Origin's public endpoint. Wired into CI (mock mode) as a regression check, and documented per README. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -45,3 +45,14 @@ check-in and observe the result/error.
|
||||
`hostname()`, `outboundIp()`, `containerId()`, `orchestrator()`, and
|
||||
`resolveMachineId()` are exported standalone for callers that want the
|
||||
detection logic without the HTTP client.
|
||||
|
||||
## Manual smoke test
|
||||
|
||||
```bash
|
||||
npm run example # single check-in, local mock server
|
||||
node examples/testclient.mjs --watch # loops via start() until Ctrl-C
|
||||
```
|
||||
|
||||
Set `ORIGIN_SERVICE_KEY` (and optionally `ORIGIN_URL`, `ORIGIN_TYPE`,
|
||||
`ORIGIN_VERSION`, `ORIGIN_NAME`, `ORIGIN_SITE`, `ORIGIN_ENVIRONMENT`,
|
||||
`ORIGIN_INTERVAL_HOURS`) to check in against a real endpoint instead.
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
// 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;
|
||||
});
|
||||
+2
-1
@@ -21,7 +21,8 @@
|
||||
"scripts": {
|
||||
"build": "tsc -p tsconfig.json",
|
||||
"test": "vitest run",
|
||||
"typecheck": "tsc --noEmit"
|
||||
"typecheck": "tsc --noEmit",
|
||||
"example": "npm run build && node examples/testclient.mjs"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.10.0",
|
||||
|
||||
Reference in New Issue
Block a user