Files
elizaos--eliza/plugins/plugin-personal-assistant/test/knowledge-graph-service.real.e2e.test.ts
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

213 lines
7.2 KiB
TypeScript

/**
* Real-DB integration test for the runtime KnowledgeGraphService.
*
* `relationships-graph.e2e.test.ts` (sibling file) already covers the
* EntityStore.upsert + RelationshipStore (upsert / observe / retire / cadence)
* surface directly on real PGLite, so this file does NOT duplicate that. It
* covers the runtime-service layer the production agent uses instead of
* constructing the stores by hand:
*
* - `resolveKnowledgeGraphService` returns the registered service.
* - `service.getEntityStore(...)` round-trips upsert → get against the DB.
* - `EntityStore.observeIdentity` (create + same-handle fold) — not covered
* by the relationships test.
* - `EntityStore.merge` folds a source entity into a target and rewrites the
* relationship edges that pointed at the source.
* - `service.getRelationshipStore(...)` upserts an edge and lists it back.
*
* The service is registered by the "eliza" plugin in production; here we
* register a minimal test plugin carrying `knowledgeGraphSchema` (so the
* app_lifeops graph tables migrate) + `KnowledgeGraphService`, then drive the
* real PGLite-backed runtime.
*/
import {
KnowledgeGraphService,
knowledgeGraphSchema,
resolveKnowledgeGraphService,
} from "@elizaos/agent";
import type { AgentRuntime, Plugin } from "@elizaos/core";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import {
createRealTestRuntime,
type RealTestRuntimeResult,
} from "../../../packages/test/helpers/real-runtime.ts";
import { SELF_ENTITY_ID } from "../src/lifeops/entities/types";
/**
* Minimal stand-in for the production "eliza" plugin: registers the
* knowledge-graph schema (creates the app_lifeops graph tables on init) and the
* KnowledgeGraphService so `resolveKnowledgeGraphService` finds it.
*/
const knowledgeGraphPlugin: Plugin = {
name: "eliza",
description: "Test-only knowledge-graph schema + service registration.",
schema: knowledgeGraphSchema,
services: [KnowledgeGraphService],
};
describe("KnowledgeGraphService — real PGLite", () => {
let runtime: AgentRuntime;
let testResult: RealTestRuntimeResult;
const agentScope = "knowledge-graph-service-tests";
beforeAll(async () => {
testResult = await createRealTestRuntime({
characterName: agentScope,
plugins: [knowledgeGraphPlugin],
});
runtime = testResult.runtime;
}, 180_000);
afterAll(async () => {
await testResult?.cleanup();
});
it("resolves the registered service from the runtime", () => {
const service = resolveKnowledgeGraphService(runtime);
expect(service).toBeInstanceOf(KnowledgeGraphService);
});
it("upserts an entity through the service store and reads it back", async () => {
const service = resolveKnowledgeGraphService(runtime);
expect(service).not.toBeNull();
const entityStore = service?.getEntityStore(agentScope);
expect(entityStore).toBeTruthy();
if (!entityStore) return;
await entityStore.ensureSelf();
const pat = await entityStore.upsert({
type: "person",
preferredName: "Pat",
identities: [],
tags: ["colleague"],
visibility: "owner_agent_admin",
state: {},
});
expect(pat.entityId).toBeTruthy();
const fetched = await entityStore.get(pat.entityId);
expect(fetched?.preferredName).toBe("Pat");
expect(fetched?.tags).toContain("colleague");
});
it("observeIdentity creates an entity then folds a same-handle observation", async () => {
const service = resolveKnowledgeGraphService(runtime);
const entityStore = service?.getEntityStore(agentScope);
if (!entityStore) throw new Error("entity store unavailable");
const created = await entityStore.observeIdentity({
platform: "discord",
handle: "robin#1234",
displayName: "Robin",
evidence: ["seen in #general"],
confidence: 0.9,
});
expect(created.entity.entityId).toBeTruthy();
expect(created.mergedFrom).toBeUndefined();
expect(
created.entity.identities.some((i) => i.handle === "robin#1234"),
).toBe(true);
// Re-observing the same platform+handle folds onto the same entity.
const second = await entityStore.observeIdentity({
platform: "discord",
handle: "robin#1234",
displayName: "Robin",
evidence: ["seen again"],
confidence: 0.95,
});
expect(second.entity.entityId).toBe(created.entity.entityId);
expect(second.mergedFrom).toEqual([created.entity.entityId]);
});
it("merge folds a source entity into a target and rewrites its edges", async () => {
const service = resolveKnowledgeGraphService(runtime);
if (!service) throw new Error("service unavailable");
const entityStore = service.getEntityStore(agentScope);
const relationshipStore = service.getRelationshipStore(agentScope);
await entityStore.ensureSelf();
const target = await entityStore.upsert({
type: "person",
preferredName: "Alex (canonical)",
identities: [],
tags: [],
visibility: "owner_agent_admin",
state: {},
});
const duplicate = await entityStore.upsert({
type: "person",
preferredName: "Alex (dupe)",
identities: [],
tags: [],
visibility: "owner_agent_admin",
state: {},
});
// An edge points at the duplicate; merge must rewrite it to the target.
const edge = await relationshipStore.upsert({
fromEntityId: SELF_ENTITY_ID,
toEntityId: duplicate.entityId,
type: "knows",
metadata: {},
state: {},
evidence: ["pre-merge"],
confidence: 0.8,
source: "user_chat",
});
const merged = await service
.getEntityStore(agentScope)
.merge(target.entityId, [duplicate.entityId]);
expect(merged.entityId).toBe(target.entityId);
// The duplicate row is gone.
expect(await entityStore.get(duplicate.entityId)).toBeNull();
// The edge now points at the target (rewritten during merge).
const edges = await relationshipStore.list({
fromEntityId: SELF_ENTITY_ID,
});
const rewritten = edges.find(
(e) => e.relationshipId === edge.relationshipId,
);
expect(rewritten?.toEntityId).toBe(target.entityId);
});
it("getRelationshipStore upserts an edge and lists it back", async () => {
const service = resolveKnowledgeGraphService(runtime);
if (!service) throw new Error("service unavailable");
const entityStore = service.getEntityStore(agentScope);
const relationshipStore = service.getRelationshipStore(agentScope);
await entityStore.ensureSelf();
const sam = await entityStore.upsert({
type: "person",
preferredName: "Sam",
identities: [],
tags: [],
visibility: "owner_agent_admin",
state: {},
});
const rel = await relationshipStore.upsert({
fromEntityId: SELF_ENTITY_ID,
toEntityId: sam.entityId,
type: "colleague_of",
metadata: { cadenceDays: 14 },
state: {},
evidence: ["seed"],
confidence: 0.9,
source: "user_chat",
});
const edges = await relationshipStore.list({
fromEntityId: SELF_ENTITY_ID,
toEntityId: sam.entityId,
});
expect(
edges.find((e) => e.relationshipId === rel.relationshipId),
).toBeTruthy();
});
});