Add nonblocking/asyn I/O operations
This commit is contained in:
@@ -1,16 +1,18 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import request from 'supertest';
|
||||
import { Readable } from 'node:stream';
|
||||
import app from '../app.js';
|
||||
|
||||
vi.mock('../db/notes.dao.js', () => ({
|
||||
getAllNotes: vi.fn(),
|
||||
streamAllNotes: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('../services/clustering.service.js', () => ({
|
||||
clusterNotes: vi.fn(),
|
||||
}));
|
||||
|
||||
import { getAllNotes } from '../db/notes.dao.js';
|
||||
import { getAllNotes, streamAllNotes } from '../db/notes.dao.js';
|
||||
import { clusterNotes } from '../services/clustering.service.js';
|
||||
|
||||
const MOCK_NOTES = [
|
||||
@@ -24,6 +26,8 @@ const MOCK_CLUSTERS = [
|
||||
{ label: 'Export Problems', noteIds: ['note_003'] },
|
||||
];
|
||||
|
||||
const rowStream = (rows) => Readable.from(rows, { objectMode: true });
|
||||
|
||||
describe('GET /v1/notes', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -31,7 +35,7 @@ describe('GET /v1/notes', () => {
|
||||
});
|
||||
|
||||
it('should return 200 and an array of notes', async () => {
|
||||
getAllNotes.mockResolvedValue(MOCK_NOTES);
|
||||
streamAllNotes.mockResolvedValue(rowStream(MOCK_NOTES));
|
||||
|
||||
const res = await request(app).get('/v1/notes');
|
||||
|
||||
@@ -40,8 +44,26 @@ describe('GET /v1/notes', () => {
|
||||
expect(Array.isArray(res.body)).toBe(true);
|
||||
});
|
||||
|
||||
it('should send JSON incrementally rather than buffering the row set', async () => {
|
||||
streamAllNotes.mockResolvedValue(rowStream(MOCK_NOTES));
|
||||
|
||||
const res = await request(app).get('/v1/notes');
|
||||
|
||||
expect(res.headers['content-type']).toMatch(/application\/json/);
|
||||
expect(res.headers['content-length']).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should return an empty array when there are no notes', async () => {
|
||||
streamAllNotes.mockResolvedValue(rowStream([]));
|
||||
|
||||
const res = await request(app).get('/v1/notes');
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body).toEqual([]);
|
||||
});
|
||||
|
||||
it('should return notes with expected properties', async () => {
|
||||
getAllNotes.mockResolvedValue(MOCK_NOTES);
|
||||
streamAllNotes.mockResolvedValue(rowStream(MOCK_NOTES));
|
||||
|
||||
const res = await request(app).get('/v1/notes');
|
||||
const note = res.body[0];
|
||||
@@ -55,7 +77,7 @@ describe('GET /v1/notes', () => {
|
||||
});
|
||||
|
||||
it('should return 500 when the database query fails', async () => {
|
||||
getAllNotes.mockRejectedValue(new Error('connection refused'));
|
||||
streamAllNotes.mockRejectedValue(new Error('connection refused'));
|
||||
|
||||
const res = await request(app).get('/v1/notes');
|
||||
|
||||
@@ -63,6 +85,19 @@ describe('GET /v1/notes', () => {
|
||||
expect(res.body).toHaveProperty('error');
|
||||
expect(res.body.error).toBe('Failed to load notes');
|
||||
});
|
||||
|
||||
it('should abort the response when the row stream fails mid-flight', async () => {
|
||||
const failing = new Readable({
|
||||
objectMode: true,
|
||||
read() {
|
||||
this.push(MOCK_NOTES[0]);
|
||||
this.destroy(new Error('connection lost'));
|
||||
},
|
||||
});
|
||||
streamAllNotes.mockResolvedValue(failing);
|
||||
|
||||
await expect(request(app).get('/v1/notes')).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /v1/notes/cluster', () => {
|
||||
@@ -101,7 +136,22 @@ describe('POST /v1/notes/cluster', () => {
|
||||
await request(app).post('/v1/notes/cluster');
|
||||
|
||||
expect(clusterNotes).toHaveBeenCalledOnce();
|
||||
expect(clusterNotes).toHaveBeenCalledWith(MOCK_NOTES);
|
||||
expect(clusterNotes).toHaveBeenCalledWith(
|
||||
MOCK_NOTES,
|
||||
expect.objectContaining({ signal: expect.any(AbortSignal) })
|
||||
);
|
||||
});
|
||||
|
||||
it('should pass a signal that is not aborted while the request is open', async () => {
|
||||
getAllNotes.mockResolvedValue(MOCK_NOTES);
|
||||
clusterNotes.mockImplementation(async (_notes, { signal }) => {
|
||||
expect(signal.aborted).toBe(false);
|
||||
return MOCK_CLUSTERS;
|
||||
});
|
||||
|
||||
const res = await request(app).post('/v1/notes/cluster');
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
});
|
||||
|
||||
it('should return 500 when clusterNotes (API call) fails', async () => {
|
||||
|
||||
Reference in New Issue
Block a user