Files
AgentCompany/tests/unit/wakeup-lifecycle.test.ts
T
ZacharyZhang-NY 118e3d8795 Remove final AppDatabase pass-through methods with ESLint guard (C1 part 3/3)
Migrate all remaining pass-through call sites (social, skills, budget,
entityCrud, documents, knowledge, automation, wakeup, backup, threads,
settings, snapshot, connectors, workspaces, activity) to their owning
domain modules. `AppDatabase` now carries only three methods:

  - createHireRequest (multi-module composite: agents + approvalsMod)
  - rollbackConfigRevision (multi-module composite: documents + entityCrud)
  - queryAll (raw-SQL escape hatch, @internal)

The vault/env-binding infrastructure previously passed argument-by-
argument into `entityCrud.saveSecret`/`resolveSecretEnv` now binds at
module construction, letting callers invoke `db.entityCrud.saveSecret({...})`
without carrying vault plumbing.

Structural types that narrowed AppDatabase to a bag of flat methods
(`ConnectorModelPolicyDeps` callers, `CapabilityMatchDb`, test mocks in
budget-enforcement / budget-incidents / task-quality-gates /
failure-escalation / task-quality-gates-sweep) are reshaped to the new
namespace. `connectorPolicyDepsFromDb` replaces the old pattern of
handing the whole AppDatabase to `validateConnectorModelSelection`.

A new ESLint rule in eslint.config.mjs blocks new top-level methods on
AppDatabase so the pass-through layer cannot silently grow back.
`AppDatabase.ts` dropped from 193 lines to 58; `AppDatabaseBase` lost
the 270-line trailing pass-through block.
2026-04-20 13:31:39 -04:00

144 lines
4.8 KiB
TypeScript

import { join } from "node:path";
import { tmpdir } from "node:os";
import { mkdtemp, rm } from "node:fs/promises";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("electron", () => ({
safeStorage: {
isEncryptionAvailable: () => false,
getSelectedStorageBackend: () => "basic_text",
encryptString: (value: string) => Buffer.from(value, "utf8"),
decryptString: (value: Buffer) => value.toString("utf8"),
},
}));
import { AppDatabase } from "../../src/main/database";
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function createTestCompany(db: AppDatabase): string {
return db.company.saveCompany({
name: "Wakeup Test Company",
description: "Wakeup lifecycle tests",
status: "active",
});
}
function createTestConnector(db: AppDatabase): void {
db.connectorsCrud.saveConnector({
id: "openai-codex",
model: "gpt-5",
envBindingText: "",
notes: "Test connector",
});
}
function createTestWorkspace(db: AppDatabase, companyId: string): string {
return db.workspacesMod.saveWorkspace({
companyId,
name: "Wakeup Workspace",
localPath: "/tmp/wakeup-workspace",
repoUrl: "https://github.com/example/wakeup",
repoRef: "main",
isPrimary: true,
});
}
function createTestAgent(db: AppDatabase, companyId: string, workspaceId: string, name = "Agent"): string {
return db.agents.saveAgent({
companyId,
name,
role: "IC",
title: name,
status: "active",
reportsTo: null,
connectorId: "openai-codex",
workspaceId,
model: null,
capabilities: "Executes tasks",
budgetMonthlyUsd: 100,
});
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
describe("wakeup lifecycle", () => {
let tempDir: string;
let db: AppDatabase;
beforeEach(async () => {
tempDir = await mkdtemp(join(tmpdir(), "agentcompany-wakeup-"));
db = new AppDatabase(tempDir);
db.init();
});
afterEach(async () => {
db.close();
await rm(tempDir, { recursive: true, force: true });
});
it("enqueueWakeupRequest creates new queued wakeup", () => {
const companyId = createTestCompany(db);
createTestConnector(db);
const workspaceId = createTestWorkspace(db, companyId);
const agentId = createTestAgent(db, companyId, workspaceId);
db.wakeup.enqueueWakeupRequest(companyId, agentId, "task.created", "New task assigned");
const wakes = db.wakeup.listWakeupRequests(companyId);
expect(wakes.length).toBe(1);
expect(wakes[0].agentId).toBe(agentId);
expect(wakes[0].status).toBe("queued");
expect(wakes[0].coalescedCount).toBe(0);
const triggers = JSON.parse(wakes[0].triggerDetail!);
expect(triggers).toHaveLength(1);
expect(triggers[0].trigger).toBe("task.created");
expect(triggers[0].detail).toBe("New task assigned");
});
it("enqueueWakeupRequest coalesces into existing queued wakeup for same agent", () => {
const companyId = createTestCompany(db);
createTestConnector(db);
const workspaceId = createTestWorkspace(db, companyId);
const agentId = createTestAgent(db, companyId, workspaceId);
db.wakeup.enqueueWakeupRequest(companyId, agentId, "task.created", "First task");
db.wakeup.enqueueWakeupRequest(companyId, agentId, "task.status_changed", "Second trigger");
const wakes = db.wakeup.listWakeupRequests(companyId);
// D-07: Per-agent scope -- only one wakeup row should exist
expect(wakes.length).toBe(1);
expect(wakes[0].coalescedCount).toBe(1);
const triggers = JSON.parse(wakes[0].triggerDetail!);
expect(triggers).toHaveLength(2);
expect(triggers[0].trigger).toBe("task.created");
expect(triggers[1].trigger).toBe("task.status_changed");
});
it("listWakeupRequests returns rows filtered by companyId and optional agentId", () => {
const companyId = createTestCompany(db);
createTestConnector(db);
const workspaceId = createTestWorkspace(db, companyId);
const agent1Id = createTestAgent(db, companyId, workspaceId, "Agent1");
const agent2Id = createTestAgent(db, companyId, workspaceId, "Agent2");
db.wakeup.enqueueWakeupRequest(companyId, agent1Id, "task.created", "Agent1 task");
db.wakeup.enqueueWakeupRequest(companyId, agent2Id, "task.created", "Agent2 task");
// List all for company
const allWakes = db.wakeup.listWakeupRequests(companyId);
expect(allWakes.length).toBe(2);
// Filter by agentId
const agent1Wakes = db.wakeup.listWakeupRequests(companyId, { agentId: agent1Id });
expect(agent1Wakes.length).toBe(1);
expect(agent1Wakes[0].agentId).toBe(agent1Id);
});
});