125 lines
5.3 KiB
TypeScript
125 lines
5.3 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { mkdtemp, readFile } from "node:fs/promises";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { test } from "bun:test";
|
|
|
|
import { runHwlabCli } from "../src/hwlab-cli-lib.ts";
|
|
|
|
test("hwlab-cli client login uses Cloud Web auth and stores only cookie session", async () => {
|
|
const cwd = await mkdtemp(path.join(os.tmpdir(), "hwlab-cli-client-"));
|
|
const calls: any[] = [];
|
|
const result = await runHwlabCli([
|
|
"client",
|
|
"auth",
|
|
"login",
|
|
"--base-url",
|
|
"http://web.test",
|
|
"--username",
|
|
"admin",
|
|
"--password-env",
|
|
"HWLAB_PASSWORD"
|
|
], {
|
|
cwd,
|
|
env: { HWLAB_PASSWORD: "secret-password" },
|
|
fetchImpl: async (url, init) => {
|
|
calls.push({ url: String(url), init, body: JSON.parse(String(init?.body ?? "{}")) });
|
|
return new Response(JSON.stringify({ authenticated: true, user: { id: "usr_admin", username: "admin", role: "admin" }, expiresAt: "2026-05-30T12:00:00.000Z" }), {
|
|
status: 200,
|
|
headers: { "content-type": "application/json", "set-cookie": "hwlab_session=session-a; Path=/; HttpOnly" }
|
|
});
|
|
},
|
|
now: () => "2026-05-30T00:00:00.000Z"
|
|
});
|
|
|
|
assert.equal(result.exitCode, 0);
|
|
assert.equal(result.payload.action, "client.auth.login");
|
|
assert.equal(result.payload.cookieStored, true);
|
|
assert.equal(calls[0].url, "http://web.test/auth/login");
|
|
assert.deepEqual(calls[0].body, { username: "admin", password: "secret-password" });
|
|
assert.equal(JSON.stringify(result.payload).includes("secret-password"), false);
|
|
|
|
const session = JSON.parse(await readFile(path.join(cwd, ".state/hwlab-cli/session.json"), "utf8"));
|
|
assert.equal(session.baseUrl, "http://web.test");
|
|
assert.equal(session.cookie, "hwlab_session=session-a");
|
|
assert.equal(JSON.stringify(session).includes("secret-password"), false);
|
|
});
|
|
|
|
test("hwlab-cli client device-pods uses saved cookie and same Web API paths", async () => {
|
|
const cwd = await mkdtemp(path.join(os.tmpdir(), "hwlab-cli-client-"));
|
|
await runHwlabCli(["client", "auth", "login", "--base-url", "http://web.test", "--username", "admin", "--password", "pw"], {
|
|
cwd,
|
|
fetchImpl: async () => new Response(JSON.stringify({ authenticated: true, user: { username: "admin" } }), { status: 200, headers: { "set-cookie": "hwlab_session=session-a; Path=/" } })
|
|
});
|
|
const seen: any[] = [];
|
|
const result = await runHwlabCli(["client", "device-pods", "status", "device-pod-71-freq", "--base-url", "http://web.test"], {
|
|
cwd,
|
|
fetchImpl: async (url, init) => {
|
|
seen.push({ url: String(url), headers: init?.headers });
|
|
return new Response(JSON.stringify({ ok: true, devicePodId: "device-pod-71-freq", contractVersion: "device-pod-authority-v1", profileHash: "sha256:abc" }), { status: 200 });
|
|
}
|
|
});
|
|
|
|
assert.equal(result.exitCode, 0);
|
|
assert.equal(seen[0].url, "http://web.test/v1/device-pods/device-pod-71-freq/status");
|
|
assert.equal(seen[0].headers.cookie, "hwlab_session=session-a");
|
|
assert.equal(result.payload.route.path, "/v1/device-pods/device-pod-71-freq/status");
|
|
});
|
|
|
|
test("hwlab-cli client agent send submits async and polls result", async () => {
|
|
const calls: any[] = [];
|
|
const result = await runHwlabCli([
|
|
"client",
|
|
"agent",
|
|
"send",
|
|
"--base-url",
|
|
"http://web.test",
|
|
"--cookie",
|
|
"hwlab_session=session-a",
|
|
"--message",
|
|
"hello",
|
|
"--trace-id",
|
|
"trc_test",
|
|
"--conversation-id",
|
|
"cnv_test",
|
|
"--poll-interval-ms",
|
|
"1",
|
|
"--timeout-ms",
|
|
"1000"
|
|
], {
|
|
fetchImpl: async (url, init) => {
|
|
calls.push({ url: String(url), init, body: init?.body ? JSON.parse(String(init.body)) : null });
|
|
if (String(url).endsWith("/v1/agent/chat")) {
|
|
return new Response(JSON.stringify({ accepted: true, status: "running", traceId: "trc_test", resultUrl: "/v1/agent/chat/result/trc_test" }), { status: 202 });
|
|
}
|
|
return new Response(JSON.stringify({ status: "completed", traceId: "trc_test", conversationId: "cnv_test", message: { role: "assistant", content: "hi" } }), { status: 200 });
|
|
},
|
|
sleep: async () => {}
|
|
});
|
|
|
|
assert.equal(result.exitCode, 0);
|
|
assert.equal(calls[0].url, "http://web.test/v1/agent/chat");
|
|
assert.equal(calls[0].init.headers.prefer, "respond-async");
|
|
assert.equal(calls[0].body.shortConnection, true);
|
|
assert.equal(calls[1].url, "http://web.test/v1/agent/chat/result/trc_test");
|
|
assert.equal(result.payload.traceId, "trc_test");
|
|
assert.equal(result.payload.result.body.status, "completed");
|
|
});
|
|
|
|
test("hwlab-cli client workbench summary probes Cloud Web non-visual surfaces", async () => {
|
|
const seen: string[] = [];
|
|
const result = await runHwlabCli(["client", "workbench", "summary", "--base-url", "http://web.test", "--cookie", "session-a", "--pod-id", "device-pod-71-freq"], {
|
|
fetchImpl: async (url) => {
|
|
seen.push(String(url));
|
|
return new Response(JSON.stringify({ ok: true, status: "ok" }), { status: 200 });
|
|
}
|
|
});
|
|
|
|
assert.equal(result.exitCode, 0);
|
|
assert.equal(seen.includes("http://web.test/health/live"), true);
|
|
assert.equal(seen.includes("http://web.test/v1"), true);
|
|
assert.equal(seen.includes("http://web.test/v1/device-pods"), true);
|
|
assert.equal(seen.includes("http://web.test/v1/device-pods/device-pod-71-freq/status"), true);
|
|
assert.equal(result.payload.serviceRuntime, undefined);
|
|
});
|