Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions backend/__tests__/unit/models/AttentionItem.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const mongoose = require('mongoose');
const AttentionItem = require('../../../models/AttentionItem');
const { setupMongoDb, closeMongoDb, clearMongoDb } = require('../../utils/testUtils');

describe('AttentionItem', () => {
const recipient = new mongoose.Types.ObjectId();
const pod = new mongoose.Types.ObjectId();

it('keeps a recipient/source fact unique and persists source snapshots', async () => {
await AttentionItem.create({
recipientUserId: recipient,
podId: pod,
kind: 'mention',
source: { type: 'message', id: '42' },
title: 'Ada mentioned you',
detail: '@sam please review',
messageId: '42',
threadRootId: '41',
});

await expect(AttentionItem.create({
recipientUserId: recipient,
podId: pod,
kind: 'mention',
source: { type: 'message', id: '42' },
title: 'duplicate',
})).rejects.toMatchObject({ code: 11000 });

const row = await AttentionItem.findOne({ recipientUserId: recipient }).lean();
expect(row).toMatchObject({
status: 'open', kind: 'mention', messageId: '42', threadRootId: '41',
source: { type: 'message', id: '42' },
});
});

it('accepts a task source as a recipient-owned board attention fact', async () => {
const row = new AttentionItem({
recipientUserId: recipient,
podId: pod,
kind: 'decision',
source: { type: 'task', id: 'task-1:update-1' },
title: 'Choose a deploy shape',
});

await expect(row.validate()).resolves.toBeUndefined();
});
});
beforeAll(async () => { await setupMongoDb(); });
afterAll(async () => { await closeMongoDb(); });
afterEach(async () => { await clearMongoDb(); });
35 changes: 35 additions & 0 deletions backend/__tests__/unit/routes/tasksApi.status-vocabulary.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ jest.mock('../../../services/taskEventService', () => ({
emitTaskUpdated: (...args) => mockEmitTaskUpdated(...args),
}));

const mockRecordTaskAttention = jest.fn();
const mockResolveTaskAttention = jest.fn();
jest.mock('../../../services/attentionItemService', () => ({
recordTaskAttention: (...args) => mockRecordTaskAttention(...args),
resolveTaskAttention: (...args) => mockResolveTaskAttention(...args),
}));

const tasksApi = require('../../../routes/tasksApi');

const app = express();
Expand All @@ -61,6 +68,10 @@ beforeEach(() => {
mockFindOneAndUpdate.mockReset();
mockFindOneAndUpdate.mockResolvedValue({ taskId: 'TASK-001', status: 'claimed' });
mockEmitTaskUpdated.mockReset();
mockRecordTaskAttention.mockReset();
mockRecordTaskAttention.mockResolvedValue(undefined);
mockResolveTaskAttention.mockReset();
mockResolveTaskAttention.mockResolvedValue(undefined);
});

describe('PATCH /:podId/:taskId status vocabulary', () => {
Expand Down Expand Up @@ -91,6 +102,30 @@ describe('PATCH /:podId/:taskId status vocabulary', () => {
expect(mockFindOneAndUpdate.mock.calls[0][1].$set.status).toBe('blocked');
});

test('materializes a blocked row at the status-write boundary', async () => {
const task = { taskId: 'TASK-001', status: 'blocked', podId: POD_ID, updates: [] };
mockFindOneAndUpdate.mockResolvedValueOnce(task);

const res = await request(app)
.patch(`/api/v1/tasks/${POD_ID}/TASK-001`)
.send({ status: 'blocked' });

expect(res.status).toBe(200);
expect(mockRecordTaskAttention).toHaveBeenCalledWith(task, { includeBlocked: true });
});

test('resolves task attention when a task leaves blocked state', async () => {
const task = { taskId: 'TASK-001', status: 'done', podId: POD_ID, updates: [] };
mockFindOneAndUpdate.mockResolvedValueOnce(task);

const res = await request(app)
.patch(`/api/v1/tasks/${POD_ID}/TASK-001`)
.send({ status: 'done' });

expect(res.status).toBe(200);
expect(mockResolveTaskAttention).toHaveBeenCalledWith(task);
});

test('rejects unknown statuses with the vocabulary in the error', async () => {
const res = await request(app)
.patch(`/api/v1/tasks/${POD_ID}/TASK-001`)
Expand Down
22 changes: 21 additions & 1 deletion backend/__tests__/unit/routes/tasksApi.updateRenewsLease.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ jest.mock('../../../models/User', () => ({

jest.mock('../../../services/githubAppService', () => ({ isPatConfigured: jest.fn(() => false) }));
jest.mock('../../../services/taskEventService', () => ({ emitTaskUpdated: jest.fn() }));
const mockRecordTaskAttention = jest.fn();
jest.mock('../../../services/attentionItemService', () => ({
recordTaskAttention: (...args) => mockRecordTaskAttention(...args),
}));

const Task = require('../../../models/Task');
const tasksApi = require('../../../routes/tasksApi');
Expand Down Expand Up @@ -125,7 +129,11 @@ afterAll(async () => {
if (mongod) await mongod.stop();
});

beforeEach(async () => { await Task.deleteMany({}); });
beforeEach(async () => {
await Task.deleteMany({});
mockRecordTaskAttention.mockReset();
mockRecordTaskAttention.mockResolvedValue(undefined);
});

describe('POST /updates renews a holder-authored lease', () => {
it('the holder\'s note pushes the lease out by a full period', async () => {
Expand Down Expand Up @@ -159,6 +167,18 @@ describe('POST /updates renews a holder-authored lease', () => {
expect(row.updates.map((u) => u.text)).toContain('reviewed at 9f91b9b0');
});

it('hands every progress note to the task-attention source writer', async () => {
await seed({ claimedBy: 'holder', claimedAt: new Date(), claimExpiresAt: new Date(Date.now() + LEASE_MS) });

const res = await postUpdate('holder', 'TASK-001', 'Ready for Sam\'s ruling.');

expect(res.status).toBe(200);
expect(mockRecordTaskAttention).toHaveBeenCalledWith(expect.objectContaining({
taskId: 'TASK-001',
updates: expect.arrayContaining([expect.objectContaining({ text: 'Ready for Sam\'s ruling.' })]),
}));
});

it('an ALREADY-LAPSED holder still renews — the row was theirs and they are working', async () => {
const claimedAt = new Date(Date.now() - 90 * MIN);
const before = new Date(claimedAt.getTime() + LEASE_MS);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const mockGetOpenQueue = jest.fn();
const mockPodFind = jest.fn();
const mockTaskFind = jest.fn();

jest.mock('../../../models/Pod', () => ({ find: (...args) => mockPodFind(...args) }));
jest.mock('../../../models/User', () => ({}));
jest.mock('../../../models/Activity', () => ({}));
jest.mock('../../../models/Summary', () => ({}));
jest.mock('../../../models/Post', () => ({}));
jest.mock('../../../models/Task', () => ({ find: (...args) => mockTaskFind(...args) }));
jest.mock('../../../services/attentionItemService', () => ({
getOpenQueue: (...args) => mockGetOpenQueue(...args),
}));

const ActivityService = require('../../../services/activityService');
const chain = (value) => ({ select: () => ({ lean: async () => value }) });
const taskChain = (value) => ({
select: () => ({ sort: () => ({ limit: () => ({ lean: async () => value }) }) }),
});

describe('ActivityService.getDecisionQueue', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('reads the recipient-owned AttentionItem projection without a source-store fallback', async () => {
const queue = { items: [{ id: '42', kind: 'mention' }], count: 1, composePodId: 'pod-1' };
mockGetOpenQueue.mockResolvedValue(queue);

await expect(ActivityService.getDecisionQueue('507f191e810c19729de860ea')).resolves.toBe(queue);
expect(mockGetOpenQueue).toHaveBeenCalledWith('507f191e810c19729de860ea');
});

it('keeps the recap fallback scoped to the requested pod', async () => {
mockPodFind.mockReturnValue(chain([{ _id: 'pod-1', name: 'Current pod' }]));
mockTaskFind.mockReturnValue(taskChain([]));
mockGetOpenQueue.mockResolvedValue({
items: [
{ id: 'first', podId: 'pod-1', createdAt: new Date() },
{ id: 'other', podId: 'pod-2', createdAt: new Date() },
],
count: 2,
composePodId: null,
});
const feed = jest.spyOn(ActivityService, 'getUserFeed').mockResolvedValue({ activities: [] });

const recap = await ActivityService.getRecap('507f191e810c19729de860ea', { podId: 'pod-1' });

expect(recap.needsYou).toEqual([expect.objectContaining({ id: 'first' })]);
feed.mockRestore();
});
});
144 changes: 0 additions & 144 deletions backend/__tests__/unit/services/activityService.decisionQueue.test.js

This file was deleted.

Loading
Loading