Files
pikasTech-HWLAB/internal/agent/agentrun-dispatch.test.mjs
T
2026-06-02 20:56:36 +08:00

179 lines
8.9 KiB
JavaScript

import assert from "node:assert/strict";
import test from "node:test";
import {
DEFAULT_UNIDESK_MAIN_SERVER_ENV,
createHwlabAgentRunDispatchAssembly,
dispatchHwlabAgentRun
} from "./agentrun-dispatch.mjs";
const commitId = "8da27c83fa56d87e78e488a819afe898cf97c62b";
test("HWLAB AgentRun assembly grants GitHub and UniDesk SSH through toolCredentials", () => {
const assembly = createHwlabAgentRunDispatchAssembly({
traceId: "trc_hwlab_agentrun_001",
prompt: "检查 HWLAB 工作区并给出状态。",
commitId,
conversationId: "conv_001",
sessionId: "sess_001",
threadId: "thread_001",
unideskMainServerIp: "https://unidesk.example.test"
});
assert.equal(assembly.runPayload.tenantId, "hwlab");
assert.equal(assembly.runPayload.projectId, "pikasTech/HWLAB");
assert.equal(assembly.runPayload.providerId, "G14");
assert.equal(assembly.runPayload.backendProfile, "deepseek");
assert.equal(assembly.runPayload.workspaceRef.branch, "v0.2");
assert.equal(assembly.runPayload.resourceBundleRef.commitId, commitId);
assert.deepEqual(assembly.runPayload.resourceBundleRef.toolAliases.map((item) => item.name), ["hwpod", "unidesk-ssh"]);
assert.equal(assembly.runPayload.resourceBundleRef.toolAliases[0].path, "tools/device-pod-cli.mjs");
assert.deepEqual(assembly.runPayload.resourceBundleRef.promptRefs, [
{ name: "hwlab-v02-runtime", path: "internal/agent/prompts/hwlab-v02-runtime.md", inject: "thread-start", required: true }
]);
assert.deepEqual(assembly.runPayload.resourceBundleRef.skillRefs, [
{ name: "device-pod-cli", path: "skills/device-pod-cli/SKILL.md", required: true, aggregateAs: "device-pod-cli" },
{ name: "hwlab-agent-runtime", path: "skills/hwlab-agent-runtime/SKILL.md", required: true, aggregateAs: "hwlab-agent-runtime" }
]);
assert.deepEqual(assembly.boundaries.promptRefs, ["hwlab-v02-runtime"]);
assert.deepEqual(assembly.boundaries.skillRefs, ["device-pod-cli", "hwlab-agent-runtime"]);
assert.equal(assembly.boundaries.toolAliases.includes("hwpod"), true);
assert.equal(assembly.boundaries.toolAliases.includes("unidesk-ssh"), true);
assert.equal(assembly.commandPayload.payload.traceId, "trc_hwlab_agentrun_001");
assert.equal(assembly.commandPayload.payload.threadId, "thread_001");
const secretScope = assembly.runPayload.executionPolicy.secretScope;
assert.equal(secretScope.allowCredentialEcho, false);
assert.equal(secretScope.providerCredentials[0].profile, "deepseek");
assert.equal(secretScope.providerCredentials[0].secretRef.name, "agentrun-v01-provider-deepseek");
const tools = secretScope.toolCredentials;
assert.equal(tools.some((item) => item.tool === "github" && item.projection.envName === "GH_TOKEN"), true);
assert.equal(tools.some((item) => item.tool === "unidesk-ssh" && item.projection.envName === "UNIDESK_SSH_CLIENT_TOKEN"), true);
const transientEnv = assembly.runnerJobPayload.transientEnv;
assert.equal(transientEnv.find((item) => item.name === DEFAULT_UNIDESK_MAIN_SERVER_ENV)?.value, "https://unidesk.example.test");
assert.equal(transientEnv.some((item) => item.name === "UNIDESK_SSH_CLIENT_TOKEN"), false);
assert.equal(assembly.boundaries.unideskSshCredentialSource, "toolCredentials");
assert.equal(assembly.boundaries.githubCredentialSource, "toolCredentials");
});
test("HWLAB AgentRun assembly rejects reusable credentials in transientEnv", () => {
assert.throws(
() => createHwlabAgentRunDispatchAssembly({
traceId: "trc_hwlab_agentrun_bad_env",
prompt: "bad env",
commitId,
unideskMainServerIp: "https://unidesk.example.test",
transientEnv: [{ name: "UNIDESK_SSH_CLIENT_TOKEN", value: "test-unidesk-ssh-client-token" }]
}),
/must use AgentRun tool\/provider credential assembly/u
);
});
test("HWLAB AgentRun assembly allows explicit resource tool aliases", () => {
const assembly = createHwlabAgentRunDispatchAssembly({
traceId: "trc_hwlab_agentrun_alias_override",
prompt: "alias override",
commitId,
unideskMainServerIp: "https://unidesk.example.test",
toolAliases: [{ name: "status-tool", path: "tools/status.mjs", kind: "node-script" }]
});
assert.deepEqual(assembly.runPayload.resourceBundleRef.toolAliases, [{ name: "status-tool", path: "tools/status.mjs", kind: "node-script" }]);
});
test("HWLAB AgentRun assembly allows explicit resource prompt and skill refs", () => {
const assembly = createHwlabAgentRunDispatchAssembly({
traceId: "trc_hwlab_agentrun_prompt_skill_override",
prompt: "prompt skill override",
commitId,
unideskMainServerIp: "https://unidesk.example.test",
promptRefs: [{ name: "custom-prompt", path: "internal/agent/prompts/custom.md", inject: "thread-start", required: true }],
skillRefs: [{ name: "custom-skill", path: "skills/custom-skill/SKILL.md", required: true, aggregateAs: "custom-skill" }]
});
assert.deepEqual(assembly.runPayload.resourceBundleRef.promptRefs, [{ name: "custom-prompt", path: "internal/agent/prompts/custom.md", inject: "thread-start", required: true }]);
assert.deepEqual(assembly.runPayload.resourceBundleRef.skillRefs, [{ name: "custom-skill", path: "skills/custom-skill/SKILL.md", required: true, aggregateAs: "custom-skill" }]);
});
test("HWLAB AgentRun assembly does not duplicate explicit UniDesk main-server env", () => {
const assembly = createHwlabAgentRunDispatchAssembly({
traceId: "trc_hwlab_agentrun_explicit_unidesk",
prompt: "explicit unidesk main server",
commitId,
unideskMainServerIp: "https://unidesk.example.test",
transientEnv: [{ name: DEFAULT_UNIDESK_MAIN_SERVER_ENV, value: "https://explicit.example.test", sensitive: false }]
});
const entries = assembly.runnerJobPayload.transientEnv.filter((item) => item.name === DEFAULT_UNIDESK_MAIN_SERVER_ENV);
assert.equal(entries.length, 1);
assert.equal(entries[0].value, "https://explicit.example.test");
});
test("HWLAB AgentRun assembly requires full commit and main server when UniDesk SSH is enabled", () => {
assert.throws(
() => createHwlabAgentRunDispatchAssembly({ traceId: "trc_bad_commit", prompt: "bad", commitId: "HEAD", unideskMainServerIp: "https://unidesk.example.test" }),
/full 40-character/u
);
assert.throws(
() => createHwlabAgentRunDispatchAssembly({ traceId: "trc_no_unidesk", prompt: "bad", commitId }),
/unideskMainServerIp is required/u
);
});
test("dispatchHwlabAgentRun posts run command and runner job with commandId", async () => {
const requests = [];
const fetchImpl = async (url, init) => {
const request = { url, method: init.method, body: JSON.parse(init.body) };
requests.push(request);
if (url.endsWith("/api/v1/runs")) return jsonResponse({ id: "run_hwlab_001" });
if (url.endsWith("/api/v1/runs/run_hwlab_001/commands")) return jsonResponse({ id: "cmd_hwlab_001" });
if (url.endsWith("/api/v1/runs/run_hwlab_001/runner-jobs")) return jsonResponse({ id: "job_hwlab_001", jobName: "agentrun-v01-runner-selftest" });
return jsonResponse({ error: "unexpected path" }, { status: 404 });
};
const result = await dispatchHwlabAgentRun({
fetchImpl,
traceId: "trc_hwlab_dispatch_001",
prompt: "dispatch smoke",
commitId,
unideskMainServerIp: "https://unidesk.example.test"
});
assert.equal(result.run.id, "run_hwlab_001");
assert.equal(result.command.id, "cmd_hwlab_001");
assert.equal(result.runnerJob.jobName, "agentrun-v01-runner-selftest");
assert.deepEqual(requests.map((item) => new URL(item.url).pathname), [
"/api/v1/runs",
"/api/v1/runs/run_hwlab_001/commands",
"/api/v1/runs/run_hwlab_001/runner-jobs"
]);
assert.equal(requests[2].body.commandId, "cmd_hwlab_001");
assert.equal(requests[2].body.transientEnv.some((item) => item.name === "UNIDESK_SSH_CLIENT_TOKEN"), false);
});
test("dispatchHwlabAgentRun unwraps AgentRun manager response envelopes", async () => {
const fetchImpl = async (url, init) => {
if (url.endsWith("/api/v1/runs")) return jsonResponse({ ok: true, data: { id: "run_hwlab_envelope" } });
if (url.endsWith("/api/v1/runs/run_hwlab_envelope/commands")) return jsonResponse({ ok: true, data: { id: "cmd_hwlab_envelope" } });
if (url.endsWith("/api/v1/runs/run_hwlab_envelope/runner-jobs")) return jsonResponse({ ok: true, data: { id: "job_hwlab_envelope", jobName: "agentrun-v01-runner-envelope" } });
return jsonResponse({ ok: false, failureKind: "unexpected-path" }, { status: 404 });
};
const result = await dispatchHwlabAgentRun({
fetchImpl,
traceId: "trc_hwlab_dispatch_envelope",
prompt: "dispatch envelope smoke",
commitId,
unideskMainServerIp: "https://unidesk.example.test"
});
assert.equal(result.run.id, "run_hwlab_envelope");
assert.equal(result.command.id, "cmd_hwlab_envelope");
assert.equal(result.runnerJob.jobName, "agentrun-v01-runner-envelope");
});
function jsonResponse(body, { status = 200 } = {}) {
return {
ok: status >= 200 && status < 300,
status,
text: async () => JSON.stringify(body)
};
}