Files
wehub-resource-sync 426e9eeabd
Voice Workbench / headless workbench (mocked backends) (push) Has been cancelled
Voice Workbench / real acoustic lane (nightly, provisioned only) (push) Has been cancelled
ci / test (push) Has been cancelled
ci / lint-and-format (push) Has been cancelled
ci / build (push) Has been cancelled
ci / dev-startup (push) Has been cancelled
gitleaks / gitleaks (push) Has been cancelled
Markdown Links / Relative Markdown Links (push) Has been cancelled
Quality (Extended) / Homepage Build (PR smoke) (push) Has been cancelled
Quality (Extended) / Comment-only diff guard (push) Has been cancelled
Quality (Extended) / Format + Type Safety Ratchet (push) Has been cancelled
Quality (Extended) / Develop Gate (secret scan + UI determinism) (push) Has been cancelled
Quality (Extended) / Develop Gate (lint) (push) Has been cancelled
Chat shell gestures / Chat shell gesture + parity e2e (push) Has been cancelled
Cloud Gateway Discord / Test (push) Has been cancelled
Benchmark Bridge Tests / benchmark (bunx @biomejs/biome check packages/lifeops-bench/src, benchmark-lint) (push) Has been cancelled
Benchmark Bridge Tests / benchmark (bunx vitest run --config packages/lifeops-bench/vitest.config.ts --root packages/lifeops-bench --passWithNoTests, benchmark-tests) (push) Has been cancelled
Build Agent Image / build-and-push (push) Has been cancelled
Dev Smoke / bun run dev onboarding chat (push) Has been cancelled
Dev Smoke / Vite HMR dependency-level smoke (push) Has been cancelled
Electrobun Submodule Guard / electrobun gitlink is fetchable (push) Has been cancelled
Publish @elizaos/example-code / check_npm (push) Has been cancelled
Publish @elizaos/example-code / publish_npm (push) Has been cancelled
Publish @elizaos/plugin-elizacloud / verify_version (push) Has been cancelled
Publish @elizaos/plugin-elizacloud / publish_npm (push) Has been cancelled
Sandbox Live Smoke / Sandbox live smoke (push) Has been cancelled
Snap Build & Test / Build Snap (amd64) (push) Has been cancelled
Snap Build & Test / Build Snap (arm64) (push) Has been cancelled
Test Packaging / elizaos CLI global-install smoke (node + bun) (push) Has been cancelled
Cloud Gateway Webhook / Test (push) Has been cancelled
Cloud Tests / lint-and-types (push) Has been cancelled
Cloud Tests / unit-tests (push) Has been cancelled
Cloud Tests / integration-tests (push) Has been cancelled
Cloud Tests / e2e-tests (push) Has been cancelled
CodeQL Advanced / Analyze (javascript-typescript) (push) Has been cancelled
Deploy Apps Worker (Product 2) / Determine environment (push) Has been cancelled
Deploy Apps Worker (Product 2) / Deploy apps worker to apps-control host (${{ needs.determine-env.outputs.environment }}) (push) Has been cancelled
Deploy Eliza Provisioning Worker / Determine environment (push) Has been cancelled
Deploy Eliza Provisioning Worker / Deploy worker to Hetzner host (${{ needs.determine-env.outputs.environment }} @ ${{ needs.determine-env.outputs.deployment_sha }}) (push) Has been cancelled
Dev Smoke / Classify changed paths (push) Has been cancelled
supply-chain / sbom (push) Has been cancelled
supply-chain / vulnerability-scan (push) Has been cancelled
Build, Push & Deploy to Phala Cloud / build-and-push (push) Has been cancelled
Test Packaging / Validate Packaging Configs (push) Has been cancelled
Test Packaging / Build & Test PyPI Package (push) Has been cancelled
Test Packaging / PyPI on Python ${{ matrix.python }} (push) Has been cancelled
Test Packaging / Pack & Test JS Tarballs (push) Has been cancelled
UI Fixture E2E / ui-fixture-e2e (push) Has been cancelled
UI Fixture E2E / fixture-e2e (push) Has been cancelled
UI Story Gate / story-gate (push) Has been cancelled
vault-ci / test (macos-latest) (push) Has been cancelled
vault-ci / test (ubuntu-latest) (push) Has been cancelled
vault-ci / test (windows-latest) (push) Has been cancelled
vault-ci / app-core wiring tests (push) Has been cancelled
verify-patches / verify patches/CHECKSUMS.sha256 (push) Has been cancelled
Voice Benchmark Smoke / voice-emotion fixture smoke (push) Has been cancelled
Voice Benchmark Smoke / voiceagentbench fixture smoke (push) Has been cancelled
Voice Benchmark Smoke / voicebench-quality unit smoke (push) Has been cancelled
Voice Benchmark Smoke / voicebench TypeScript unit (no audio) (push) Has been cancelled
Voice Benchmark Smoke / voice bench smoke summary (push) Has been cancelled
Windows CI / windows ([bun run --cwd packages/app-core test bun run --cwd packages/elizaos test bun run --cwd packages/cloud/shared test], app-and-cli) (push) Has been cancelled
Windows CI / windows ([bun run --cwd packages/scenario-runner test bun run --cwd packages/vault test bun run --cwd packages/security test bun run --cwd plugins/plugin-coding-tools test], framework-packages) (push) Has been cancelled
Windows CI / windows ([bun run --cwd plugins/plugin-elizacloud test bun run --cwd plugins/plugin-discord test bun run --cwd plugins/plugin-anthropic test bun run --cwd plugins/plugin-openai test bun run --cwd plugins/plugin-app-control test bun run --cwd plugins/pl… (push) Has been cancelled
Windows CI / windows ([node packages/scripts/run-turbo.mjs run build --filter=@elizaos/core --filter=@elizaos/shared --filter=@elizaos/agent --concurrency=4 node packages/scripts/run-bash-linux-only.mjs scripts/verify-riscv64-buildpaths.sh node packages/scripts/run… (push) Has been cancelled
Windows CI / windows ([node packages/scripts/run-turbo.mjs run typecheck --filter=@elizaos/core --filter=@elizaos/shared --filter=@elizaos/cloud-shared --concurrency=4 bun run --cwd packages/core test bun run --cwd packages/shared test], core-runtime, 75) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:43:05 +08:00

397 lines
12 KiB
TypeScript

/**
* `KNOWLEDGE_GRAPH` action — unit tests.
*
* Mocks `@elizaos/agent` (`hasOwnerAccess` + `resolveKnowledgeGraphService`)
* so the suite exercises the action's op dispatch against a fake
* EntityStore/RelationshipStore without a DB. Asserts create / read / list /
* log_interaction / set_identity / set_relationship / merge dispatch onto the
* right store method with the right shape, plus the owner-access and
* missing-op/-field failure gates.
*/
import type {
HandlerOptions,
IAgentRuntime,
Memory,
UUID,
} from "@elizaos/core";
import type { Entity, EntityIdentity, Relationship } from "@elizaos/shared";
import { beforeEach, describe, expect, it, vi } from "vitest";
const mocks = vi.hoisted(() => ({
hasOwnerAccess: vi.fn(async () => true),
resolveKnowledgeGraphService: vi.fn(),
}));
vi.mock("@elizaos/agent", () => ({
hasOwnerAccess: mocks.hasOwnerAccess,
resolveKnowledgeGraphService: mocks.resolveKnowledgeGraphService,
}));
import { entityAction } from "../src/actions/entity.ts";
function makeEntity(overrides: Partial<Entity> = {}): Entity {
return {
entityId: "ent_1",
type: "person",
preferredName: "Alice",
identities: [],
state: {},
tags: [],
visibility: "owner_agent_admin",
createdAt: "2026-01-01T00:00:00.000Z",
updatedAt: "2026-01-01T00:00:00.000Z",
...overrides,
};
}
function makeRelationship(overrides: Partial<Relationship> = {}): Relationship {
return {
relationshipId: "rel_1",
fromEntityId: "self",
toEntityId: "ent_1",
type: "manages",
state: {},
evidence: ["user_chat"],
confidence: 1,
source: "user_chat",
status: "active",
createdAt: "2026-01-01T00:00:00.000Z",
updatedAt: "2026-01-01T00:00:00.000Z",
...overrides,
};
}
type FakeStores = {
entityStore: {
upsert: ReturnType<typeof vi.fn>;
get: ReturnType<typeof vi.fn>;
list: ReturnType<typeof vi.fn>;
recordInteraction: ReturnType<typeof vi.fn>;
observeIdentity: ReturnType<typeof vi.fn>;
merge: ReturnType<typeof vi.fn>;
};
relationshipStore: {
upsert: ReturnType<typeof vi.fn>;
list: ReturnType<typeof vi.fn>;
};
};
function makeStores(): FakeStores {
return {
entityStore: {
upsert: vi.fn(async (input: Record<string, unknown>) =>
makeEntity(input as Partial<Entity>),
),
get: vi.fn(async () => makeEntity()),
list: vi.fn(async () => [makeEntity()]),
recordInteraction: vi.fn(async () => undefined),
observeIdentity: vi.fn(async () => ({ entity: makeEntity() })),
merge: vi.fn(async () => makeEntity()),
},
relationshipStore: {
upsert: vi.fn(async (input: Record<string, unknown>) =>
makeRelationship(input as Partial<Relationship>),
),
list: vi.fn(async () => [makeRelationship()]),
},
};
}
function makeService(stores: FakeStores) {
return {
getEntityStore: vi.fn(() => stores.entityStore),
getRelationshipStore: vi.fn(() => stores.relationshipStore),
};
}
function makeRuntime(): IAgentRuntime {
return {
agentId: "agent-rel-test" as UUID,
} as unknown as IAgentRuntime;
}
function makeMessage(text = "graph op"): Memory {
return {
id: "msg-1" as UUID,
entityId: "owner-1" as UUID,
roomId: "room-1" as UUID,
content: { text },
} as Memory;
}
async function call(
parameters: Record<string, unknown>,
options: Partial<HandlerOptions> = {},
) {
return entityAction.handler(
makeRuntime(),
makeMessage(),
undefined,
{ ...options, parameters } as unknown as HandlerOptions,
async () => undefined,
);
}
describe("KNOWLEDGE_GRAPH action", () => {
let stores: FakeStores;
beforeEach(() => {
vi.clearAllMocks();
mocks.hasOwnerAccess.mockResolvedValue(true);
stores = makeStores();
mocks.resolveKnowledgeGraphService.mockReturnValue(makeService(stores));
});
it("is named KNOWLEDGE_GRAPH (not ENTITY)", () => {
expect(entityAction.name).toBe("KNOWLEDGE_GRAPH");
});
it("denies non-owners", async () => {
mocks.hasOwnerAccess.mockResolvedValueOnce(false);
const result = await call({ op: "list" });
expect(result?.success).toBe(false);
expect(result?.data).toMatchObject({ error: "PERMISSION_DENIED" });
expect(stores.entityStore.list).not.toHaveBeenCalled();
});
it("fails when the service is unavailable", async () => {
mocks.resolveKnowledgeGraphService.mockReturnValueOnce(null);
const result = await call({ op: "list" });
expect(result?.success).toBe(false);
expect(result?.data).toMatchObject({ error: "SERVICE_UNAVAILABLE" });
});
it("rejects an unknown / missing op", async () => {
const result = await call({ op: "not_a_real_op" });
expect(result?.success).toBe(false);
expect(result?.data).toMatchObject({ error: "MISSING_OP" });
});
it("create dispatches entityStore.upsert with the kind + name", async () => {
const result = await call({
op: "create",
kind: "organization",
name: "Acme",
});
expect(result?.success).toBe(true);
expect(stores.entityStore.upsert).toHaveBeenCalledTimes(1);
expect(stores.entityStore.upsert).toHaveBeenCalledWith(
expect.objectContaining({ type: "organization", preferredName: "Acme" }),
);
});
it("create defaults kind to person and fails without a name", async () => {
const ok = await call({ op: "create", name: "Bob" });
expect(stores.entityStore.upsert).toHaveBeenCalledWith(
expect.objectContaining({ type: "person", preferredName: "Bob" }),
);
expect(ok?.success).toBe(true);
const bad = await call({ op: "create" });
expect(bad?.success).toBe(false);
expect(bad?.data).toMatchObject({ error: "MISSING_FIELDS" });
});
it("read fetches by id and surfaces NOT_FOUND", async () => {
const result = await call({ op: "read", entityId: "ent_1" });
expect(stores.entityStore.get).toHaveBeenCalledWith("ent_1");
expect(result?.success).toBe(true);
stores.entityStore.get.mockResolvedValueOnce(null);
const missing = await call({ op: "read", entityId: "ent_x" });
expect(missing?.success).toBe(false);
expect(missing?.data).toMatchObject({ error: "NOT_FOUND" });
});
it("list passes the kind filter + limit", async () => {
await call({ op: "list", kind: "person", limit: 10 });
expect(stores.entityStore.list).toHaveBeenCalledWith(
expect.objectContaining({ type: "person", limit: 10 }),
);
});
it("log_interaction records on the entity", async () => {
const result = await call({
op: "log_interaction",
entityId: "ent_1",
platform: "telegram",
direction: "inbound",
summary: "called about the deal",
});
expect(result?.success).toBe(true);
expect(stores.entityStore.recordInteraction).toHaveBeenCalledWith(
"ent_1",
expect.objectContaining({
platform: "telegram",
direction: "inbound",
summary: "called about the deal",
}),
);
});
it("set_identity observes but does not verify without trusted proof", async () => {
const observed = makeEntity({
identities: [
{
platform: "slack",
handle: "@pat",
verified: false,
confidence: 1,
addedAt: "2026-01-01T00:00:00.000Z",
addedVia: "platform_observation",
evidence: ["user_chat"],
} satisfies EntityIdentity,
],
});
stores.entityStore.observeIdentity.mockResolvedValueOnce({
entity: observed,
mergedFrom: ["ent_2"],
});
const result = await call({
op: "set_identity",
platform: "slack",
handle: "@pat",
});
expect(result?.success).toBe(false);
expect(result?.data).toMatchObject({
error: "IDENTITY_VERIFICATION_REQUIRED",
mergedFrom: ["ent_2"],
});
expect(stores.entityStore.observeIdentity).toHaveBeenCalledWith(
expect.objectContaining({
platform: "slack",
handle: "@pat",
confidence: 1,
}),
);
expect(stores.entityStore.upsert).not.toHaveBeenCalled();
});
it("set_identity marks verified only with trusted matching proof", async () => {
const observed = makeEntity({
identities: [
{
platform: "slack",
handle: "@pat",
verified: false,
confidence: 1,
addedAt: "2026-01-01T00:00:00.000Z",
addedVia: "platform_observation",
evidence: ["user_chat"],
} satisfies EntityIdentity,
],
});
stores.entityStore.observeIdentity.mockResolvedValueOnce({
entity: observed,
mergedFrom: ["ent_2"],
});
const result = await call(
{
op: "set_identity",
platform: "slack",
handle: "@pat",
},
{
identityVerification: {
platform: "slack",
handle: "@pat",
verified: true,
evidence: "slack_oauth_subject_match",
},
} as Partial<HandlerOptions>,
);
expect(result?.success).toBe(true);
const upsertArg = stores.entityStore.upsert.mock.calls.at(
-1,
)?.[0] as Entity;
const reasserted = upsertArg.identities.find(
(identity) => identity.platform === "slack" && identity.handle === "@pat",
);
expect(reasserted?.verified).toBe(true);
expect(reasserted?.evidence).toEqual([
"user_chat",
"slack_oauth_subject_match",
]);
expect(result?.data).toMatchObject({ mergedFrom: ["ent_2"] });
});
it("set_identity fails without platform + handle", async () => {
const result = await call({ op: "set_identity", platform: "slack" });
expect(result?.success).toBe(false);
expect(result?.data).toMatchObject({ error: "MISSING_FIELDS" });
expect(stores.entityStore.observeIdentity).not.toHaveBeenCalled();
});
it("set_relationship upserts a typed edge, defaulting from to self", async () => {
const result = await call({
op: "set_relationship",
toEntityId: "ent_1",
relationshipType: "manages",
});
expect(result?.success).toBe(true);
expect(stores.relationshipStore.upsert).toHaveBeenCalledWith(
expect.objectContaining({
fromEntityId: "self",
toEntityId: "ent_1",
type: "manages",
source: "user_chat",
confidence: 1,
}),
);
});
it("set_relationship honors an explicit fromEntityId", async () => {
await call({
op: "set_relationship",
fromEntityId: "ent_2",
toEntityId: "ent_1",
relationshipType: "works_at",
});
expect(stores.relationshipStore.upsert).toHaveBeenCalledWith(
expect.objectContaining({ fromEntityId: "ent_2", toEntityId: "ent_1" }),
);
});
it("set_relationship fails without target + type", async () => {
const result = await call({ op: "set_relationship", toEntityId: "ent_1" });
expect(result?.success).toBe(false);
expect(result?.data).toMatchObject({ error: "MISSING_FIELDS" });
expect(stores.relationshipStore.upsert).not.toHaveBeenCalled();
});
it("merge folds sources into a target", async () => {
stores.entityStore.merge.mockResolvedValueOnce(
makeEntity({ entityId: "ent_1", preferredName: "Alice" }),
);
const result = await call({
op: "merge",
entityId: "ent_1",
sourceEntityIds: ["ent_2", "ent_3"],
});
expect(result?.success).toBe(true);
expect(stores.entityStore.merge).toHaveBeenCalledWith("ent_1", [
"ent_2",
"ent_3",
]);
expect(result?.data).toMatchObject({ sourceEntityIds: ["ent_2", "ent_3"] });
});
it("merge fails without a target + at least one source", async () => {
const noSources = await call({ op: "merge", entityId: "ent_1" });
expect(noSources?.success).toBe(false);
expect(noSources?.data).toMatchObject({ error: "MISSING_FIELDS" });
expect(stores.entityStore.merge).not.toHaveBeenCalled();
});
it("accepts the `action` alias for the op field", async () => {
const result = await call({ action: "create", name: "Carol" });
expect(result?.success).toBe(true);
expect(stores.entityStore.upsert).toHaveBeenCalled();
});
});