Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

test(database): cover GoalTraceModel and VerifyReportModel edge paths (#19862)

Arvin Xuf5613309bdf5
diff --git a/packages/database/src/models/__tests__/goalTrace.test.ts b/packages/database/src/models/__tests__/goalTrace.test.ts
new file mode 100644
index 00000000000..19e3dddb7a6
--- /dev/null
+++ b/packages/database/src/models/__tests__/goalTrace.test.ts
@@ -0,0 +1,147 @@
+// @vitest-environment node
+import { afterEach, beforeEach, describe, expect, it } from 'vitest';
+
+import { getTestDB } from '../../core/getTestDB';
+import { goalTraces, users } from '../../schemas';
+import type { LobeChatDatabase } from '../../type';
+import { GoalModel } from '../goal';
+import { GoalTraceModel } from '../goalTrace';
+
+const serverDB: LobeChatDatabase = await getTestDB();
+
+const userId = 'goal-trace-model-test-user-id';
+const otherUserId = 'goal-trace-model-test-user-2';
+
+const goalModel = new GoalModel(serverDB, userId);
+const goalTraceModel = new GoalTraceModel(serverDB, userId);
+const otherGoalTraceModel = new GoalTraceModel(serverDB, otherUserId);
+
+beforeEach(async () => {
+ await serverDB.delete(users);
+ await serverDB.insert(users).values([{ id: userId }, { id: otherUserId }]);
+});
+
+afterEach(async () => {
+ await serverDB.delete(goalTraces);
+ await serverDB.delete(users);
+});
+
+describe('GoalTraceModel', () => {
+ describe('upsert', () => {
+ it('should insert a trace row for a goal', async () => {
+ const goal = await goalModel.create({ title: 'Traceable goal' });
+
+ const row = await goalTraceModel.upsert({
+ advancesTotal: 3,
+ goalId: goal.id,
+ startedAt: new Date('2026-01-01T00:00:00.000Z'),
+ ticksTotal: 5,
+ });
+
+ expect(row.goalId).toBe(goal.id);
+ expect(row.advancesTotal).toBe(3);
+ expect(row.ticksTotal).toBe(5);
+ });
+
+ it('should rewrite the whole row when the goal is advanced again', async () => {
+ const goal = await goalModel.create({ title: 'Rewritten goal' });
+
+ await goalTraceModel.upsert({ advancesTotal: 1, goalId: goal.id });
+
+ const updated = await goalTraceModel.upsert({
+ advancesTotal: 7,
+ finalStatus: 'completed',
+ goalId: goal.id,
+ totalCost: 1.25,
+ });
+
+ expect(updated.advancesTotal).toBe(7);
+ expect(updated.finalStatus).toBe('completed');
+
+ // 1:1 with the goal — the second write must not append a second row.
+ const rows = await serverDB.select().from(goalTraces);
+ expect(rows).toHaveLength(1);
+ });
+ });
+
+ describe('findById', () => {
+ it('should return the trace joined with the goal title', async () => {
+ const goal = await goalModel.create({ title: 'Joined goal' });
+ await goalTraceModel.upsert({ findingsTotal: 2, goalId: goal.id });
+
+ const found = await goalTraceModel.findById(goal.id);
+
+ expect(found?.goalId).toBe(goal.id);
+ expect(found?.findingsTotal).toBe(2);
+ expect(found?.title).toBe('Joined goal');
+ });
+
+ it('should read another user goal as absent', async () => {
+ const goal = await goalModel.create({ title: 'Private goal' });
+ await goalTraceModel.upsert({ goalId: goal.id });
+
+ expect(await otherGoalTraceModel.findById(goal.id)).toBeUndefined();
+ });
+
+ it('should return undefined when the goal has no trace', async () => {
+ const goal = await goalModel.create({ title: 'Untraced goal' });
+
+ expect(await goalTraceModel.findById(goal.id)).toBeUndefined();
+ expect(await goalTraceModel.findById('non-existent-goal')).toBeUndefined();
+ });
+ });
+
+ describe('list', () => {
+ it('should return owned traces with the newest run first', async () => {
+ const older = await goalModel.create({ title: 'Older run' });
+ const newer = await goalModel.create({ title: 'Newer run' });
+
+ await goalTraceModel.upsert({
+ goalId: older.id,
+ startedAt: new Date('2026-01-01T00:00:00.000Z'),
+ });
+ await goalTraceModel.upsert({
+ goalId: newer.id,
+ startedAt: new Date('2026-02-01T00:00:00.000Z'),
+ });
+
+ const list = await goalTraceModel.list();
+
+ expect(list.map((item) => item.title)).toEqual(['Newer run', 'Older run']);
+ });
+
+ it('should apply the requested limit', async () => {
+ const older = await goalModel.create({ title: 'Limited older' });
+ const newer = await goalModel.create({ title: 'Limited newer' });
+
+ await goalTraceModel.upsert({
+ goalId: older.id,
+ startedAt: new Date('2026-01-01T00:00:00.000Z'),
+ });
+ await goalTraceModel.upsert({
+ goalId: newer.id,
+ startedAt: new Date('2026-02-01T00:00:00.000Z'),
+ });
+
+ const list = await goalTraceModel.list(1);
+
+ expect(list).toHaveLength(1);
+ expect(list[0].title).toBe('Limited newer');
+ });
+
+ it('should not list traces owned by another user', async () => {
+ const goal = await goalModel.create({ title: 'Other owner goal' });
+ await goalTraceModel.upsert({ goalId: goal.id });
+
+ // Start from a clean slate so the assertion is not polluted by other cases.
+ await serverDB.delete(goalTraces);
+
+ const goal2 = await goalModel.create({ title: 'Owned again' });
+ await goalTraceModel.upsert({ goalId: goal2.id });
+
+ const list = await otherGoalTraceModel.list();
+
+ expect(list).toEqual([]);
+ });
+ });
+});
diff --git a/packages/database/src/models/__tests__/verifyReport.test.ts b/packages/database/src/models/__tests__/verifyReport.test.ts
index e494096d942..71afb421bf0 100644
--- a/packages/database/src/models/__tests__/verifyReport.test.ts
+++ b/packages/database/src/models/__tests__/verifyReport.test.ts
@@ -178,4 +178,109 @@ describe('VerifyReportModel', () => {
await new VerifyRunModel(serverDB, userId).delete(verifyRunId);
expect(await model.findByRun(verifyRunId)).toBeUndefined();
});
+
+ it('reads a report by id and hides it from other users', async () => {
+ const model = new VerifyReportModel(serverDB, userId);
+ const created = await model.upsertByRun({
+ failedChecks: 0,
+ passedChecks: 1,
+ totalChecks: 1,
+ uncertainChecks: 0,
+ verdict: 'passed',
+ verifyRunId,
+ });
+
+ expect((await model.findById(created.id))?.id).toBe(created.id);
+ expect(await model.findById('00000000-0000-0000-0000-000000000000')).toBeUndefined();
+ expect(
+ await new VerifyReportModel(serverDB, 'someone-else').findById(created.id),
+ ).toBeUndefined();
+ });
+
+ it('reads the reports of several rounds in one query', async () => {
+ const model = new VerifyReportModel(serverDB, userId);
+ const secondOperationId = 'verify-report-test-op-2';
+
+ await new AgentOperationModel(serverDB, userId).recordStart({ operationId: secondOperationId });
+ const secondRun = await new VerifyRunModel(serverDB, userId).ensureForOperation(
+ secondOperationId,
+ );
+
+ const first = await model.upsertByRun({
+ failedChecks: 0,
+ passedChecks: 1,
+ totalChecks: 1,
+ uncertainChecks: 0,
+ verdict: 'passed',
+ verifyRunId,
+ });
+ const second = await model.upsertByRun({
+ failedChecks: 1,
+ passedChecks: 0,
+ totalChecks: 1,
+ uncertainChecks: 0,
+ verdict: 'failed',
+ verifyRunId: secondRun.id,
+ });
+
+ // an empty round chain short-circuits instead of querying
+ expect(await model.findByRuns([])).toEqual([]);
+ expect(await new VerifyReportModel(serverDB, 'someone-else').findByRuns([verifyRunId])).toEqual(
+ [],
+ );
+
+ const reports = await model.findByRuns([verifyRunId, secondRun.id]);
+ expect(reports.map((report) => report.id).sort()).toEqual([first.id, second.id].sort());
+ });
+
+ it('updates and deletes a report it owns', async () => {
+ const model = new VerifyReportModel(serverDB, userId);
+ const created = await model.upsertByRun({
+ failedChecks: 0,
+ passedChecks: 1,
+ summary: 'Original summary',
+ totalChecks: 1,
+ uncertainChecks: 0,
+ verdict: 'passed',
+ verifyRunId,
+ });
+
+ await model.update(created.id, { summary: 'Edited by hand' });
+ expect((await model.findById(created.id))?.summary).toBe('Edited by hand');
+
+ await model.delete(created.id);
+ expect(await model.findById(created.id)).toBeUndefined();
+ });
+
+ describe('upsert conflict guard', () => {
+ // The conflicting row is excluded by the ownership `setWhere`, so no row comes
+ // back. Drive that path with a stubbed driver — `assertRunOwned` sees the run.
+ const createStubDB = (ownedRunIds: string[]) => {
+ const stub = {
+ insert: () => ({
+ values: () => ({ onConflictDoUpdate: () => ({ returning: async () => [] }) }),
+ }),
+ select: () => ({
+ from: () => ({ where: () => ({ limit: async () => ownedRunIds.map((id) => ({ id })) }) }),
+ }),
+ };
+
+ return stub as unknown as LobeChatDatabase;
+ };
+
+ it('throws when the conflicting report is out of the caller scope', async () => {
+ const model = new VerifyReportModel(createStubDB(['stub-run']), userId);
+
+ await expect(
+ model.upsertByRun({
+ failedChecks: 0,
+ passedChecks: 1,
+ totalChecks: 1,
+ uncertainChecks: 0,
+ verdict: 'passed',
+ verifyRunId: 'stub-run',
+ }),
+ ).rejects.toThrow('not found in the current workspace');
+ });
+ });
});