Something went wrong. Try again.
A free, open-source, self-hosted place to write a novel.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960import { describe, expect, it } from 'vitest';import { conflictResponse, failure, readPrecondition } from './save-request.ts';
describe('save request preconditions', () => { it.each([ null, undefined, false, 7, 'text', [], {}, { baseRevision: '2' }, { baseRevision: 1.5 }, { baseRevision: Infinity } ])('rejects a malformed save body: %s', (payload) => { expect(readPrecondition(payload)).toBeNull(); });
it('accepts an integer revision and only the boolean true as a replacement request', () => { expect(readPrecondition({ baseRevision: 0 })).toEqual({ baseRevision: 0, replace: false }); expect(readPrecondition({ baseRevision: 12, replace: true })).toEqual({ baseRevision: 12, replace: true }); expect(readPrecondition({ baseRevision: 12, replace: 'true' })).toEqual({ baseRevision: 12, replace: false }); });});
describe('save responses', () => { it('returns the current document in a conflict response', async () => { const response = conflictResponse({ revision: 8, body: { type: 'doc', content: [{ type: 'text', text: 'New words' }] }, text: 'New words', words: 2, savedAt: new Date('2025-05-06T07:08:09.000Z') }); expect(response.status).toBe(409); expect(await response.json()).toEqual({ code: 'conflict', message: 'This was saved somewhere else since you opened it.', revision: 8, body: { type: 'doc', content: [{ type: 'text', text: 'New words' }] }, text: 'New words', words: 2, savedAt: '2025-05-06T07:08:09.000Z' }); });
it('returns a requested failure status and message as JSON', async () => { const response = failure(404, 'This scene is no longer here.'); expect(response.status).toBe(404); expect(await response.json()).toEqual({ message: 'This scene is no longer here.' }); });});