Skip to content
Open
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
20 changes: 20 additions & 0 deletions task-api/DAY1_SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Day 1 Test and Coverage Summary

- Command run: npm run coverage
- Test suites: 2 failed, 2 total
- Tests: 4 failed, 28 passed, 32 total

Comment on lines +1 to +6
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DAY1_SUMMARY.md currently documents a npm run coverage run with failing test suites/tests. If this file is meant to reflect the current state of the PR, please update it to match the current (passing) results or clearly label it as an historical snapshot to avoid confusing readers/CI triage.

Suggested change
# Day 1 Test and Coverage Summary
- Command run: npm run coverage
- Test suites: 2 failed, 2 total
- Tests: 4 failed, 28 passed, 32 total
# Day 1 Test and Coverage Summary (Historical Snapshot)
> This file records the Day 1 `npm run coverage` results and bugs found at that time.
> It is a historical snapshot, not the current PR/CI status.
- Command run on Day 1: npm run coverage
- Test suites on Day 1: 2 failed, 2 total
- Tests on Day 1: 4 failed, 28 passed, 32 total

Copilot uses AI. Check for mistakes.
## Coverage

- Statements: 94.77%
- Branches: 89.33%
- Functions: 92.3%
- Lines: 94.26%

## Bugs Found by Tests

1. Status filter bug: partial value like do matches tasks due to substring logic.
2. Pagination bug: page 1 starts at wrong offset and skips first items.

![alt text](<Screenshot 2026-04-15 164037.png>)
![alt text](<Screenshot 2026-04-15 164021.png>)
34 changes: 34 additions & 0 deletions task-api/DAY2_SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Day 2

## Bug Fix (Part B)

Primary bug fixed: pagination offset.

- Expected: `page=1&limit=2` should return the first two tasks.
- Fix: changed offset to `(page - 1) * limit` and guarded invalid page/limit values.

Additional fix applied: status filtering now uses exact match instead of substring match.

## Assign Endpoint (Part C)

Added endpoint: `PATCH /tasks/:id/assign`

### Behavior

- Stores assignee on task as `assignee`.
- Returns updated task with `200` when assignment succeeds.
- Returns `404` when task is not found.

### Validation Decisions

1. Empty or missing assignee
- Returns `400` with `assignee is required and must be a non-empty string`.

2. Already assigned task
- Returns `409` with `Task is already assigned`.
- Rationale: prevents accidental overwrite of ownership.

3. Normalization
- Assignee is stored as trimmed text (`assignee.trim()`).

![alt text](<Screenshot 2026-04-15 165034.png>)
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DAY2_SUMMARY.md embeds a local screenshot file. If you keep screenshots in the repo, consider optimizing/compressing them (or using Git LFS) to avoid large binary files in source control; otherwise link to an external artifact.

Suggested change
![alt text](<Screenshot 2026-04-15 165034.png>)
Screenshot omitted from the repository to avoid embedding a local binary asset in source control; attach or link it as an external artifact if needed.

Copilot uses AI. Check for mistakes.
Binary file added task-api/Screenshot 2026-04-15 164021.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added task-api/Screenshot 2026-04-15 164037.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added task-api/Screenshot 2026-04-15 165034.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions task-api/api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const app = require('../src/app');

module.exports = app;
21 changes: 20 additions & 1 deletion task-api/src/routes/tasks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const express = require('express');
const router = express.Router();
const taskService = require('../services/taskService');
const { validateCreateTask, validateUpdateTask } = require('../utils/validators');
const { validateCreateTask, validateUpdateTask, validateAssignTask } = require('../utils/validators');

router.get('/stats', (req, res) => {
const stats = taskService.getStats();
Expand Down Expand Up @@ -69,4 +69,23 @@ router.patch('/:id/complete', (req, res) => {
res.json(task);
});

router.patch('/:id/assign', (req, res) => {
const error = validateAssignTask(req.body);
if (error) {
return res.status(400).json({ error });
}

const existing = taskService.findById(req.params.id);
if (!existing) {
return res.status(404).json({ error: 'Task not found' });
}

if (existing.assignee) {
return res.status(409).json({ error: 'Task is already assigned' });
}

const task = taskService.update(req.params.id, { assignee: req.body.assignee.trim() });
res.json(task);
});

module.exports = router;
18 changes: 14 additions & 4 deletions task-api/src/services/taskService.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ const getAll = () => [...tasks];

const findById = (id) => tasks.find((t) => t.id === id);

const getByStatus = (status) => tasks.filter((t) => t.status.includes(status));
const getByStatus = (status) => tasks.filter((t) => t.status === status);

const getPaginated = (page, limit) => {
const offset = page * limit;
return tasks.slice(offset, offset + limit);
const pageNum = Number.isInteger(page) && page > 0 ? page : 1;
const limitNum = Number.isInteger(limit) && limit > 0 ? limit : 10;
const offset = (pageNum - 1) * limitNum;
return tasks.slice(offset, offset + limitNum);
};

const getStats = () => {
Expand All @@ -28,13 +30,21 @@ const getStats = () => {
return { ...counts, overdue };
};

const create = ({ title, description = '', status = 'todo', priority = 'medium', dueDate = null }) => {
const create = ({
title,
description = '',
status = 'todo',
priority = 'medium',
dueDate = null,
assignee = null,
}) => {
const task = {
id: uuidv4(),
title,
description,
status,
priority,
assignee,
dueDate,
completedAt: null,
createdAt: new Date().toISOString(),
Expand Down
9 changes: 8 additions & 1 deletion task-api/src/utils/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,11 @@ const validateUpdateTask = (body) => {
return null;
};

module.exports = { validateCreateTask, validateUpdateTask };
const validateAssignTask = (body) => {
if (!body || typeof body.assignee !== 'string' || body.assignee.trim() === '') {
return 'assignee is required and must be a non-empty string';
}
return null;
};

module.exports = { validateCreateTask, validateUpdateTask, validateAssignTask };
137 changes: 137 additions & 0 deletions task-api/tests/taskService.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
const taskService = require('../src/services/taskService');

describe('taskService unit tests', () => {
beforeEach(() => {
taskService._reset();
});

test('create should persist a task with defaults', () => {
const task = taskService.create({ title: 'Write tests' });

expect(task.id).toEqual(expect.any(String));
expect(task.title).toBe('Write tests');
expect(task.description).toBe('');
expect(task.status).toBe('todo');
expect(task.priority).toBe('medium');
expect(task.dueDate).toBeNull();
expect(task.completedAt).toBeNull();
expect(task.createdAt).toEqual(expect.any(String));
expect(taskService.getAll()).toHaveLength(1);
});

test('create should persist provided fields', () => {
const dueDate = new Date(Date.now() + 3600000).toISOString();
const task = taskService.create({
title: 'High priority work',
description: 'Do this now',
status: 'in_progress',
priority: 'high',
dueDate,
});

expect(task.description).toBe('Do this now');
expect(task.status).toBe('in_progress');
expect(task.priority).toBe('high');
expect(task.dueDate).toBe(dueDate);
});

test('getAll should return a copy of task list', () => {
taskService.create({ title: 'Original' });

const snapshot = taskService.getAll();
snapshot.push({ id: 'fake' });

expect(taskService.getAll()).toHaveLength(1);
});

test('findById should return matching task and undefined for unknown id', () => {
const created = taskService.create({ title: 'Lookup' });

expect(taskService.findById(created.id)).toMatchObject({ title: 'Lookup' });
expect(taskService.findById('missing-id')).toBeUndefined();
});

test('getByStatus should return only exact status matches', () => {
taskService.create({ title: 'Todo item', status: 'todo' });
taskService.create({ title: 'Done item', status: 'done' });

const result = taskService.getByStatus('todo');

expect(result).toHaveLength(1);
expect(result[0].title).toBe('Todo item');
});

test('getByStatus should not treat partial status as a valid match', () => {
taskService.create({ title: 'Todo item', status: 'todo' });
taskService.create({ title: 'Done item', status: 'done' });

expect(taskService.getByStatus('do')).toEqual([]);
});

test('getPaginated should use 1-based page numbers', () => {
for (let i = 1; i <= 5; i++) {
taskService.create({ title: `Task ${i}` });
}

const pageOne = taskService.getPaginated(1, 2).map((task) => task.title);
const pageThree = taskService.getPaginated(3, 2).map((task) => task.title);

expect(pageOne).toEqual(['Task 1', 'Task 2']);
expect(pageThree).toEqual(['Task 5']);
});

test('getStats should include status counts and overdue count', () => {
const past = new Date(Date.now() - 86400000).toISOString();
const future = new Date(Date.now() + 86400000).toISOString();

taskService.create({ title: 'A', status: 'todo', dueDate: past });
taskService.create({ title: 'B', status: 'in_progress', dueDate: future });
taskService.create({ title: 'C', status: 'done', dueDate: past });

expect(taskService.getStats()).toEqual({
todo: 1,
in_progress: 1,
done: 1,
overdue: 1,
});
});

test('update should patch an existing task and return null for unknown id', () => {
const created = taskService.create({ title: 'Before', priority: 'low' });

const updated = taskService.update(created.id, {
title: 'After',
priority: 'high',
status: 'in_progress',
});

expect(updated).toMatchObject({
id: created.id,
title: 'After',
priority: 'high',
status: 'in_progress',
});
expect(taskService.update('missing-id', { title: 'Nope' })).toBeNull();
});

test('remove should delete existing task and return false for unknown id', () => {
const created = taskService.create({ title: 'Delete me' });

expect(taskService.remove(created.id)).toBe(true);
expect(taskService.findById(created.id)).toBeUndefined();
expect(taskService.remove('missing-id')).toBe(false);
});

test('completeTask should mark task done and set completedAt timestamp', () => {
const created = taskService.create({ title: 'Finish me', status: 'todo' });

const completed = taskService.completeTask(created.id);

expect(completed).toMatchObject({
id: created.id,
status: 'done',
});
expect(completed.completedAt).toEqual(expect.any(String));
expect(taskService.completeTask('missing-id')).toBeNull();
});
});
Loading
Loading