Refactor edge agent test with waitFor utility and update GitHub Actions workflow
- Introduced `waitFor` utility in edge agent tests for more reliable condition polling. - Adjusted test assertions to use `waitFor` for verifying agent polling activity. - Added separate GitHub Actions jobs for `edge-agent` and `edge-broker` to improve test isolation.
This commit is contained in:
@@ -43,6 +43,52 @@ jobs:
|
||||
if-no-files-found: warn
|
||||
retention-days: 1
|
||||
|
||||
edge-agent:
|
||||
name: Edge Agent (required)
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 22
|
||||
cache: npm
|
||||
cache-dependency-path: services/edge-agent/package-lock.json
|
||||
|
||||
- name: Install dependencies
|
||||
working-directory: services/edge-agent
|
||||
run: npm ci
|
||||
|
||||
- name: Run edge agent tests
|
||||
working-directory: services/edge-agent
|
||||
run: npm test
|
||||
|
||||
edge-broker:
|
||||
name: Edge Broker (required)
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 22
|
||||
cache: npm
|
||||
cache-dependency-path: services/edge-broker/package-lock.json
|
||||
|
||||
- name: Install dependencies
|
||||
working-directory: services/edge-broker
|
||||
run: npm ci
|
||||
|
||||
- name: Run edge broker tests
|
||||
working-directory: services/edge-broker
|
||||
run: npm test
|
||||
|
||||
integration:
|
||||
name: Integration (advisory)
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
@@ -22,6 +22,20 @@ import {
|
||||
const execFile = promisify(execFileCallback);
|
||||
const agentEntryPath = fileURLToPath(new URL("../dist/agent.mjs", import.meta.url));
|
||||
|
||||
async function waitFor(predicate, { timeoutMs = 1000, intervalMs = 10, description = "condition" } = {}) {
|
||||
const deadline = Date.now() + timeoutMs;
|
||||
|
||||
while (Date.now() < deadline) {
|
||||
if (predicate()) {
|
||||
return;
|
||||
}
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, intervalMs));
|
||||
}
|
||||
|
||||
throw new Error(`Timed out waiting for ${description}`);
|
||||
}
|
||||
|
||||
test("claimIfNeeded persists claimed gateway credentials", async () => {
|
||||
const tempDir = await mkdtemp(path.join(os.tmpdir(), "edge-agent-"));
|
||||
const configPath = path.join(tempDir, "config.json");
|
||||
@@ -179,7 +193,7 @@ test("startAgent reports API polling metadata, executes polled commands, and upl
|
||||
apiUrl: "https://api.example.test",
|
||||
gatewayId: 42,
|
||||
agentToken: "agent-token",
|
||||
heartbeatIntervalSeconds: 60,
|
||||
heartbeatIntervalSeconds: 0.05,
|
||||
commandPollTimeoutSeconds: 0,
|
||||
commandPollRetryDelayMs: 5,
|
||||
shellActionPollTimeoutSeconds: 0,
|
||||
@@ -388,7 +402,18 @@ test("startAgent reports API polling metadata, executes polled commands, and upl
|
||||
assert.equal("broker_connected" in heartbeats[0].body.metadata, false);
|
||||
assert.equal("discovery_status" in heartbeats[0].body, false);
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, 120));
|
||||
await waitFor(
|
||||
() =>
|
||||
commandResultPosts.length === 1 &&
|
||||
shellActionResults.length === 2 &&
|
||||
shellEventPosts.length > 0 &&
|
||||
heartbeats.some(
|
||||
(heartbeat) =>
|
||||
heartbeat.body.metadata.system_metrics &&
|
||||
typeof heartbeat.body.metadata.system_metrics.latency_ms === "number"
|
||||
),
|
||||
{ timeoutMs: 1000, description: "agent polling activity and follow-up heartbeat" }
|
||||
);
|
||||
|
||||
assert.equal(commandResultPosts.length, 1);
|
||||
assert.equal(commandResultPosts[0].body.ok, true);
|
||||
|
||||
Reference in New Issue
Block a user