/
malklopp
/
opencode-course
Обзор
Документация
Войти
/
malklopp
/
opencode-course
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
practice/test/app.test.js
102 строки
3 KB
malklop
Курс изучения OpenCode: модули 0-5, учебный Express-проект, README
10 авг 2026, 22:38
10 авг 2026, 22:38
aa4d518
Код
Авторство
О чём код?
const { test, before, after } = require('node:test') const assert = require('node:assert/strict') const app = require('../src/app') let server let base before(async () => { server = app.listen(0) await new Promise((resolve) => server.once('listening', resolve)) base = `http://localhost:${server.address().port}` }) after(() => { server.close() }) async function request(path, options = {}) { const res = await fetch(base + path, { headers: { 'content-type': 'application/json' }, ...options, }) const body = res.status === 204 ? null : await res.json() return { status: res.status, body } } test('health returns ok', async () => { const { status, body } = await request('/health') assert.equal(status, 200) assert.equal(body.status, 'ok') }) test('creates a note and returns 201', async () => { const { status, body } = await request('/notes', { method: 'POST', body: JSON.stringify({ title: 'Hello', body: 'World' }), }) assert.equal(status, 201) assert.ok(body.id) assert.equal(body.title, 'Hello') assert.equal(body.body, 'World') }) test('rejects note without title', async () => { const { status, body } = await request('/notes', { method: 'POST', body: JSON.stringify({ body: 'no title' }), }) assert.equal(status, 400) assert.ok(body.error) }) test('lists created notes', async () => { const created = await request('/notes', { method: 'POST', body: JSON.stringify({ title: 'List me' }), }) const { status, body } = await request('/notes') assert.equal(status, 200) assert.ok(body.some((note) => note.id === created.body.id)) }) test('gets a note by id', async () => { const created = await request('/notes', { method: 'POST', body: JSON.stringify({ title: 'Fetch me' }), }) const { status, body } = await request(`/notes/${created.body.id}`) assert.equal(status, 200) assert.equal(body.title, 'Fetch me') }) test('returns 404 for missing note', async () => { const { status, body } = await request('/notes/does-not-exist') assert.equal(status, 404) assert.ok(body.error) }) test('updates a note', async () => { const created = await request('/notes', { method: 'POST', body: JSON.stringify({ title: 'Old', body: 'a' }), }) const { status, body } = await request(`/notes/${created.body.id}`, { method: 'PUT', body: JSON.stringify({ title: 'New' }), }) assert.equal(status, 200) assert.equal(body.title, 'New') assert.equal(body.body, 'a') }) test('deletes a note', async () => { const created = await request('/notes', { method: 'POST', body: JSON.stringify({ title: 'Delete me' }), }) const { status } = await request(`/notes/${created.body.id}`, { method: 'DELETE' }) assert.equal(status, 204) const missing = await request(`/notes/${created.body.id}`) assert.equal(missing.status, 404) })