@@ -9,4 +9,8 @@ app.use(express.json());
|
|||||||
|
|
||||||
app.use('/v1/notes', notesRoutes);
|
app.use('/v1/notes', notesRoutes);
|
||||||
|
|
||||||
|
app.use((req, res) => {
|
||||||
|
res.status(404).json({ error: `Requested path is invalid or does not exist: ${req.method} ${req.originalUrl}` });
|
||||||
|
});
|
||||||
|
|
||||||
export default app;
|
export default app;
|
||||||
|
|||||||
1292
backend/package-lock.json
generated
1292
backend/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -1,12 +1,13 @@
|
|||||||
{
|
{
|
||||||
"name": "congruity-backend",
|
"name": "kongruity-backend",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"description": "congruity AI clustering feature - backend for ProjectPilot",
|
"description": "kongruity AI stick note clustering feature - backend for ProjectPilot",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "server.js",
|
"main": "server.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node server.js",
|
"start": "node server.js",
|
||||||
"dev": "nodemon server.js"
|
"dev": "nodemon server.js",
|
||||||
|
"test": "vitest run"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@anthropic-ai/sdk": "^0.74.0",
|
"@anthropic-ai/sdk": "^0.74.0",
|
||||||
|
|||||||
90
backend/tests/Clustering.service.test.js
Normal file
90
backend/tests/Clustering.service.test.js
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||||
|
|
||||||
|
const { createMock } = vi.hoisted(() => {
|
||||||
|
return { createMock: vi.fn() };
|
||||||
|
});
|
||||||
|
|
||||||
|
vi.mock('@anthropic-ai/sdk', () => {
|
||||||
|
return {
|
||||||
|
default: class MockAnthropic {
|
||||||
|
constructor() {
|
||||||
|
this.messages = { create: createMock };
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
vi.mock('../.secrets.js', () => ({
|
||||||
|
anthropicApiKey: 'test-key-not-real',
|
||||||
|
}));
|
||||||
|
|
||||||
|
import { clusterNotes } from '../services/clustering.service.js';
|
||||||
|
|
||||||
|
const MOCK_NOTES = [
|
||||||
|
{ id: 'note_001', text: 'Login is broken' },
|
||||||
|
{ id: 'note_002', text: 'Export fails' },
|
||||||
|
];
|
||||||
|
|
||||||
|
const MOCK_CLUSTERS = [
|
||||||
|
{ label: 'Auth Issues', noteIds: ['note_001'] },
|
||||||
|
{ label: 'Export Issues', noteIds: ['note_002'] },
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
|
describe('clusterNotes service', () => {
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should call Anthropic messages.create with the correct model', async () => {
|
||||||
|
createMock.mockResolvedValue({
|
||||||
|
content: [{ text: JSON.stringify(MOCK_CLUSTERS) }],
|
||||||
|
});
|
||||||
|
|
||||||
|
await clusterNotes(MOCK_NOTES);
|
||||||
|
|
||||||
|
expect(createMock).toHaveBeenCalledOnce();
|
||||||
|
const callArgs = createMock.mock.calls[0][0];
|
||||||
|
expect(callArgs.model).toBe('claude-sonnet-4-20250514');
|
||||||
|
expect(callArgs.max_tokens).toBe(4096);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should include all note texts in prompt sent to the LLM API', async () => {
|
||||||
|
createMock.mockResolvedValue({
|
||||||
|
content: [{ text: JSON.stringify(MOCK_CLUSTERS) }],
|
||||||
|
});
|
||||||
|
|
||||||
|
await clusterNotes(MOCK_NOTES);
|
||||||
|
|
||||||
|
const prompt = createMock.mock.calls[0][0].messages[0].content;
|
||||||
|
expect(prompt).toContain('note_001');
|
||||||
|
expect(prompt).toContain('Login is broken');
|
||||||
|
expect(prompt).toContain('note_002');
|
||||||
|
expect(prompt).toContain('Export fails');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should parse and return the clustered JSON from the API response', async () => {
|
||||||
|
createMock.mockResolvedValue({
|
||||||
|
content: [{ text: JSON.stringify(MOCK_CLUSTERS) }],
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await clusterNotes(MOCK_NOTES);
|
||||||
|
|
||||||
|
expect(result).toEqual(MOCK_CLUSTERS);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw error when the API returns non-JSON', async () => {
|
||||||
|
createMock.mockResolvedValue({
|
||||||
|
content: [{ text: 'An unknown error occured when generting structured response.' }],
|
||||||
|
});
|
||||||
|
|
||||||
|
await expect(clusterNotes(MOCK_NOTES)).rejects.toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw error when Anthropic API authentication fails', async () => {
|
||||||
|
createMock.mockRejectedValue(new Error('401 Unauthorized'));
|
||||||
|
|
||||||
|
await expect(clusterNotes(MOCK_NOTES)).rejects.toThrow('401 Unauthorized');
|
||||||
|
});
|
||||||
|
});
|
||||||
135
backend/tests/Notes.test.js
Normal file
135
backend/tests/Notes.test.js
Normal file
@@ -0,0 +1,135 @@
|
|||||||
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||||
|
import request from 'supertest';
|
||||||
|
import app from '../app.js';
|
||||||
|
|
||||||
|
vi.mock('../services/clustering.service.js', () => ({
|
||||||
|
clusterNotes: vi.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock('fs/promises', () => ({
|
||||||
|
readFile: vi.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
import { clusterNotes } from '../services/clustering.service.js';
|
||||||
|
import { readFile } from 'fs/promises';
|
||||||
|
|
||||||
|
const MOCK_NOTES = [
|
||||||
|
{ id: 'note_001', text: 'Login flow feels confusing', x: 193, y: 191, author: 'user_5', color: 'yellow' },
|
||||||
|
{ id: 'note_002', text: 'Login flow is broken on mobile', x: 214, y: 281, author: 'user_9', color: 'yellow' },
|
||||||
|
{ id: 'note_003', text: 'Export takes too long', x: 798, y: 211, author: 'user_2', color: 'green' },
|
||||||
|
];
|
||||||
|
|
||||||
|
const MOCK_CLUSTERS = [
|
||||||
|
{ label: 'Login Issues', noteIds: ['note_001', 'note_002'] },
|
||||||
|
{ label: 'Export Problems', noteIds: ['note_003'] },
|
||||||
|
];
|
||||||
|
|
||||||
|
describe('GET /v1/notes', () => {
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return 200 and an array of notes', async () => {
|
||||||
|
readFile.mockResolvedValue(JSON.stringify(MOCK_NOTES));
|
||||||
|
|
||||||
|
const res = await request(app).get('/v1/notes');
|
||||||
|
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.body).toEqual(MOCK_NOTES);
|
||||||
|
expect(Array.isArray(res.body)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return notes with expected properties', async () => {
|
||||||
|
readFile.mockResolvedValue(JSON.stringify(MOCK_NOTES));
|
||||||
|
|
||||||
|
const res = await request(app).get('/v1/notes');
|
||||||
|
const note = res.body[0];
|
||||||
|
|
||||||
|
expect(note).toHaveProperty('id');
|
||||||
|
expect(note).toHaveProperty('text');
|
||||||
|
expect(note).toHaveProperty('x');
|
||||||
|
expect(note).toHaveProperty('y');
|
||||||
|
expect(note).toHaveProperty('author');
|
||||||
|
expect(note).toHaveProperty('color');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return 500 when the data file cannot be read', async () => {
|
||||||
|
readFile.mockRejectedValue(new Error('ENOENT: file not found'));
|
||||||
|
|
||||||
|
const res = await request(app).get('/v1/notes');
|
||||||
|
|
||||||
|
expect(res.status).toBe(500);
|
||||||
|
expect(res.body).toHaveProperty('error');
|
||||||
|
expect(res.body.error).toBe('Failed to load notes');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return 500 when data file is invalid JSON', async () => {
|
||||||
|
readFile.mockResolvedValue('{ this is not valid json }');
|
||||||
|
|
||||||
|
const res = await request(app).get('/v1/notes');
|
||||||
|
|
||||||
|
expect(res.status).toBe(500);
|
||||||
|
expect(res.body).toHaveProperty('error');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('POST /v1/notes/cluster', () => {
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return 200 and clustered results', async () => {
|
||||||
|
readFile.mockResolvedValue(JSON.stringify(MOCK_NOTES));
|
||||||
|
clusterNotes.mockResolvedValue(MOCK_CLUSTERS);
|
||||||
|
|
||||||
|
const res = await request(app).post('/v1/notes/cluster');
|
||||||
|
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.body).toEqual(MOCK_CLUSTERS);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return clusters with the expected shape (label, noteIds)', async () => {
|
||||||
|
readFile.mockResolvedValue(JSON.stringify(MOCK_NOTES));
|
||||||
|
clusterNotes.mockResolvedValue(MOCK_CLUSTERS);
|
||||||
|
|
||||||
|
const res = await request(app).post('/v1/notes/cluster');
|
||||||
|
const cluster = res.body[0];
|
||||||
|
|
||||||
|
expect(cluster).toHaveProperty('label');
|
||||||
|
expect(cluster).toHaveProperty('noteIds');
|
||||||
|
expect(typeof cluster.label).toBe('string');
|
||||||
|
expect(Array.isArray(cluster.noteIds)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should pass the loaded notes to clusterNotes', async () => {
|
||||||
|
readFile.mockResolvedValue(JSON.stringify(MOCK_NOTES));
|
||||||
|
clusterNotes.mockResolvedValue(MOCK_CLUSTERS);
|
||||||
|
|
||||||
|
await request(app).post('/v1/notes/cluster');
|
||||||
|
|
||||||
|
expect(clusterNotes).toHaveBeenCalledOnce();
|
||||||
|
expect(clusterNotes).toHaveBeenCalledWith(MOCK_NOTES);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return 500 when clusterNotes (API call) fails', async () => {
|
||||||
|
readFile.mockResolvedValue(JSON.stringify(MOCK_NOTES));
|
||||||
|
clusterNotes.mockRejectedValue(new Error('Anthropic API error'));
|
||||||
|
|
||||||
|
const res = await request(app).post('/v1/notes/cluster');
|
||||||
|
|
||||||
|
expect(res.status).toBe(500);
|
||||||
|
expect(res.body).toHaveProperty('error');
|
||||||
|
expect(res.body.error).toBe('Clustering failed');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return 500 when the data file cannot be read', async () => {
|
||||||
|
readFile.mockRejectedValue(new Error('ENOENT: file not found'));
|
||||||
|
|
||||||
|
const res = await request(app).post('/v1/notes/cluster');
|
||||||
|
|
||||||
|
expect(res.status).toBe(500);
|
||||||
|
expect(res.body).toHaveProperty('error');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -22,10 +22,10 @@
|
|||||||
.main-head {
|
.main-head {
|
||||||
font-size: 5rem;
|
font-size: 5rem;
|
||||||
color: #6dd6f4;
|
color: #6dd6f4;
|
||||||
margin: 2px 0px 24px 22px;
|
margin: 6px 0px 24px 22px;
|
||||||
font-family: "Sulphur Point", sans-serif;
|
font-family: "Sulphur Point", sans-serif;
|
||||||
font-weight: 500;
|
font-weight: 500;
|
||||||
letter-spacing: 3px;
|
letter-spacing: 4px;
|
||||||
text-decoration: underline;
|
text-decoration: underline;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user