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): raise coverage back to 100% (#22358)

Arvin Xu6f897ab91ab5
diff --git a/packages/database/src/models/__tests__/goalGraph.findGoalsByTaskIds.test.ts b/packages/database/src/models/__tests__/goalGraph.findGoalsByTaskIds.test.ts
index 6e68a894e90..93b362d9181 100644
--- a/packages/database/src/models/__tests__/goalGraph.findGoalsByTaskIds.test.ts
+++ b/packages/database/src/models/__tests__/goalGraph.findGoalsByTaskIds.test.ts
@@ -120,6 +120,29 @@ describe('GoalGraphModel.findGoalsByTaskIds', () => {
expect(goals.get(leaf.id)?.id).toBe(nearer.id);
});
+ it('keeps the goal it already resolved when a carrier reaches the same root again', async () => {
+ const task = await taskModel.create({ instruction: 'self-carried' });
+
+ // Bound to the task and carried by it at once: the row is considered twice
+ // for the same root, at the same depth and the same timestamp. The second
+ // look must not replace the first.
+ const goal = await goalModel.create({
+ subjectId: task.id,
+ subjectType: 'task',
+ title: 'Bound and carried',
+ });
+ const node = await graphModel.createNode(goal.id, { kind: 'task', title: 'Bound' });
+ await graphModel.bindTask(goal.id, node!.id, task.id);
+
+ const goals = await graphModel.findGoalsByTaskIds([task.id]);
+
+ expect(goals.get(task.id)).toEqual({
+ id: goal.id,
+ status: goal.status,
+ title: 'Bound and carried',
+ });
+ });
+
it('ignores a goal carried by a task outside the walked chain', async () => {
const task = await taskModel.create({ instruction: 'bound' });
const elsewhere = await taskModel.create({ instruction: 'carrier outside the chain' });
diff --git a/packages/database/src/models/__tests__/goalGraph.test.ts b/packages/database/src/models/__tests__/goalGraph.test.ts
index d454b63ea32..369ced86e55 100644
--- a/packages/database/src/models/__tests__/goalGraph.test.ts
+++ b/packages/database/src/models/__tests__/goalGraph.test.ts
@@ -373,6 +373,37 @@ describe('GoalGraphModel', () => {
expect(rowOf(accepted.id)).toMatchObject({ status: 'accepted' });
});
+ it('skips the write when the settled status already matches, and settles nothing outside its scope', async () => {
+ // `verifying` is not closed by retirement, so the settle just writes the
+ // status it was handed. When the row already holds it there is no update
+ // and no `completedAt`; a row outside the caller's scope is not seen at all.
+ const task = await new TaskModel(serverDB, userId).create({ instruction: 'Settle no-op' });
+ const [waiting] = await serverDB
+ .insert(acceptances)
+ .values({ status: 'verifying', subjectId: task.id, subjectType: 'task', userId })
+ .returning();
+
+ const acceptanceModel = new AcceptanceModel(serverDB, userId);
+ expect(await acceptanceModel.settleTaskPolicyStatus(waiting.id, 'verifying')).toBe('verifying');
+
+ const [row] = await serverDB.select().from(acceptances).where(eq(acceptances.id, waiting.id));
+ expect(row).toMatchObject({ completedAt: null, status: 'verifying' });
+
+ // Unknown id and another user's row both resolve to undefined.
+ expect(
+ await acceptanceModel.settleTaskPolicyStatus(
+ '00000000-0000-4000-8000-000000000000',
+ 'delivered',
+ ),
+ ).toBeUndefined();
+ expect(
+ await new AcceptanceModel(serverDB, otherUserId).settleTaskPolicyStatus(
+ waiting.id,
+ 'delivered',
+ ),
+ ).toBeUndefined();
+ });
+
it('treats a same-status write as a no-op instead of churning the graph', async () => {
// Re-activating an already-active node must not bump `updatedAt` or append
// an event: a coordinator retry loop does this on every tick, and the
diff --git a/packages/database/src/models/__tests__/workspaceMember.test.ts b/packages/database/src/models/__tests__/workspaceMember.test.ts
index b394ba84531..c62bdc9dcf4 100644
--- a/packages/database/src/models/__tests__/workspaceMember.test.ts
+++ b/packages/database/src/models/__tests__/workspaceMember.test.ts
@@ -1,6 +1,6 @@
import { INVITATION_EXPIRY_DAYS } from '@lobechat/const';
import { and, eq } from 'drizzle-orm';
-import { afterEach, beforeEach, describe, expect, it } from 'vitest';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { getTestDB } from '../../core/getTestDB';
import {
@@ -37,6 +37,7 @@ beforeEach(async () => {
});
afterEach(async () => {
+ vi.restoreAllMocks();
await serverDB.delete(users);
});
@@ -741,6 +742,20 @@ describe('WorkspaceMemberModel', () => {
expect(await model.countLinkInvitations(workspaceId)).toBe(1);
});
+ it('countLinkInvitations degrades to 0 when the aggregate returns no row', async () => {
+ // `count()` always yields exactly one row, so the `?? 0` is a driver-level
+ // guard: a backend that answers with no row must not hand `undefined` to
+ // the per-workspace cap check.
+ const spy = vi.spyOn(serverDB, 'select').mockReturnValueOnce({
+ from: () => ({ where: async () => [] }),
+ } as unknown as ReturnType<LobeChatDatabase['select']>);
+
+ expect(
+ await new WorkspaceMemberModel(serverDB, inviterId).countLinkInvitations(workspaceId),
+ ).toBe(0);
+ expect(spy).toHaveBeenCalledTimes(1);
+ });
+
it('revokeLinkInvitation revokes only that live link of that workspace', async () => {
const model = new WorkspaceMemberModel(serverDB, inviterId);
const a = await model.createLinkInvitation({ expiresAt: null, workspaceId });