mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 02:34:21 +00:00
feat(studio): a book saved by a newer Studio opens read-only instead of losing what it cannot read
- A book carries schemaRev. The reader that meets a newer one opens the book read-only with a reload banner and saves nothing, where it used to drop the elements it could not read on the next save. - The server refuses a save, a restore or a tool edit that would write an older revision over a newer book or one newer than itself (409 book_schema_newer), on REST and through every book tool alike. - normalizeBookDocument moves to book-normalize.ts. - Print waits for photos eagerly and up to 15 s for an export of more than 40 photos, and skips images without a source.
This commit is contained in:
@@ -620,6 +620,116 @@ describe('editBook', () => {
|
||||
});
|
||||
});
|
||||
|
||||
// -- Format revisions ---------------------------------------------------------
|
||||
|
||||
/*
|
||||
* A book written by a newer revision of the format than the writer knows. The
|
||||
* older writer has already dropped what it could not read, so its save, from
|
||||
* an old tab or a replayed offline copy, must not land; nor may the book tools
|
||||
* of a server that cannot read the book whole.
|
||||
*/
|
||||
describe('format revisions', () => {
|
||||
/** A journey whose stored book says it was written by revision `schemaRev`. */
|
||||
async function storedAt(schemaRev: number) {
|
||||
const { user } = createUser(testDb);
|
||||
const journey = createJourney(testDb, user.id);
|
||||
await insertRow(await sharedTestOrm(testDb), JourneyBooks, {
|
||||
journey: journey.id,
|
||||
title: 'T',
|
||||
document: JSON.stringify({ ...doc('newer'), schemaRev }),
|
||||
version: 1,
|
||||
});
|
||||
return { user, journey };
|
||||
}
|
||||
|
||||
const refusalOf = (thrown: unknown) =>
|
||||
thrown instanceof DomainError ? [thrown.getStatus(), thrown.code, thrown.toBody().code] : thrown;
|
||||
|
||||
it('JBOOK-011: refuses a save from a writer older than the stored book with 409 and the code, writing and sending nothing', async () => {
|
||||
const { user, journey } = await storedAt(2);
|
||||
const rec = await recordSends();
|
||||
try {
|
||||
const thrown = await catchDomainError(() =>
|
||||
books.saveBook(journey.id, user.id, { title: 'T', document: doc('older'), baseVersion: 1 }),
|
||||
);
|
||||
expect(refusalOf(thrown)).toEqual([409, 'book_schema_newer', 'book_schema_newer']);
|
||||
expect(thrown instanceof DomainError && thrown.toBody()).toMatchObject({
|
||||
error: 'Book was saved by a newer version of TREK Studio',
|
||||
schemaRev: 2,
|
||||
});
|
||||
expect(rec.sent).toEqual([]);
|
||||
const stored = await books.getBook(journey.id, user.id);
|
||||
expect([stored?.version, stored?.document.title, stored?.document.schemaRev]).toEqual([1, 'newer', 2]);
|
||||
} finally {
|
||||
rec.restore();
|
||||
}
|
||||
});
|
||||
|
||||
it('JBOOK-012: refuses a document newer than this server reads, first save and later save alike', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const journey = createJourney(testDb, user.id);
|
||||
const ahead = { ...doc('ahead'), schemaRev: 9 };
|
||||
const first = await catchDomainError(() => books.saveBook(journey.id, user.id, { title: 'T', document: ahead }));
|
||||
expect(refusalOf(first)).toEqual([409, 'book_schema_newer', 'book_schema_newer']);
|
||||
expect(await books.getBook(journey.id, user.id)).toBeNull();
|
||||
|
||||
await books.saveBook(journey.id, user.id, { title: 'T', document: doc('one') });
|
||||
const later = await catchDomainError(() =>
|
||||
books.saveBook(journey.id, user.id, { title: 'T', document: ahead, baseVersion: 1 }),
|
||||
);
|
||||
expect(refusalOf(later)).toEqual([409, 'book_schema_newer', 'book_schema_newer']);
|
||||
expect((await books.getBook(journey.id, user.id))?.document.title).toBe('one');
|
||||
});
|
||||
|
||||
it('JBOOK-013: a save at the stored revision or above it lands as before, and a book of today stores no revision', async () => {
|
||||
const { user, journey } = await storedAt(1);
|
||||
const same = await books.saveBook(journey.id, user.id, {
|
||||
title: 'T',
|
||||
document: { ...doc('same'), schemaRev: 1 },
|
||||
baseVersion: 1,
|
||||
});
|
||||
expect(same && 'record' in same && same.record.version).toBe(2);
|
||||
|
||||
const plain = await books.saveBook(journey.id, user.id, { title: 'T', document: doc('plain'), baseVersion: 2 });
|
||||
expect(plain && 'record' in plain && plain.record.version).toBe(3);
|
||||
const row = await findRow(await sharedTestOrm(testDb), JourneyBooks, { journey: journey.id });
|
||||
expect(JSON.parse(String(row?.document))).not.toHaveProperty('schemaRev');
|
||||
});
|
||||
|
||||
it('JBOOK-014: the book tools leave a book this server cannot read whole alone, before any edit runs', async () => {
|
||||
const { user, journey } = await storedAt(2);
|
||||
const rec = await recordSends();
|
||||
try {
|
||||
const edit = vi.fn(() => ({ title: 'T', document: normalizeBookDocument(doc('tool')), result: null }));
|
||||
const thrown = await catchDomainError(() => books.editBook(journey.id, user.id, edit));
|
||||
expect(refusalOf(thrown)).toEqual([409, 'book_schema_newer', 'book_schema_newer']);
|
||||
expect(thrown instanceof DomainError && thrown.mcpMessage).toMatch(/newer version of TREK/);
|
||||
expect(edit).not.toHaveBeenCalled();
|
||||
expect(rec.sent).toEqual([]);
|
||||
expect((await books.getBook(journey.id, user.id))?.document.title).toBe('newer');
|
||||
} finally {
|
||||
rec.restore();
|
||||
}
|
||||
});
|
||||
|
||||
it('JBOOK-015: an edit that hands back a document newer than this server reads is refused, the stored book untouched', async () => {
|
||||
const { user, journey } = await storedAt(1);
|
||||
const rec = await recordSends();
|
||||
try {
|
||||
const ahead = normalizeBookDocument({ ...doc('ahead'), schemaRev: 2 });
|
||||
const thrown = await catchDomainError(() =>
|
||||
books.editBook(journey.id, user.id, () => ({ title: 'T', document: ahead, result: null })),
|
||||
);
|
||||
expect(refusalOf(thrown)).toEqual([409, 'book_schema_newer', 'book_schema_newer']);
|
||||
expect(rec.sent).toEqual([]);
|
||||
const stored = await books.getBook(journey.id, user.id);
|
||||
expect([stored?.version, stored?.document.title, stored?.document.schemaRev]).toEqual([1, 'newer', 1]);
|
||||
} finally {
|
||||
rec.restore();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// -- Deleting -----------------------------------------------------------------
|
||||
|
||||
describe('deleting', () => {
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
* stale version, and forgets the draft; a journey without a book gets a new
|
||||
* one and no revision; restoring the revision puts the book back only at the
|
||||
* version the apply left, and never writes a revision that does not read
|
||||
* back; five revisions are kept.
|
||||
* back or that a newer TREK kept; five revisions are kept.
|
||||
*/
|
||||
import { JourneyBookRevisions } from '../../../../src/db/entities/JourneyBookRevisions.entity';
|
||||
import { JourneyEntryPhotos } from '../../../../src/db/entities/JourneyEntryPhotos.entity';
|
||||
@@ -321,6 +321,25 @@ describe('taking a design back', () => {
|
||||
expect(told()).toEqual([]);
|
||||
});
|
||||
|
||||
it('JBDSN-011: a revision kept by a newer TREK is refused, never put back stripped of what this one cannot read', async () => {
|
||||
const { journey, owner } = await crew();
|
||||
const answer = await designs.design(journey.id, owner.id);
|
||||
const applied = await designs.apply(journey.id, owner.id, { designId: answer.design_id });
|
||||
const revisionId = applied.revision_id ?? 0;
|
||||
const kept = await findRow(orm, JourneyBookRevisions, { id: revisionId });
|
||||
const newer = JSON.stringify({ ...JSON.parse(String(kept?.document)), schemaRev: 2 });
|
||||
await updateRows(orm, JourneyBookRevisions, { id: revisionId }, { document: newer });
|
||||
t.realtime.reset();
|
||||
|
||||
const back = { revisionId, expectedVersion: applied.version };
|
||||
expect(await refusal(() => designs.restoreRevision(journey.id, owner.id, back))).toEqual({
|
||||
status: 409,
|
||||
code: 'book_schema_newer',
|
||||
});
|
||||
expect(await version(journey.id, owner.id)).toBe(applied.version);
|
||||
expect(told()).toEqual([]);
|
||||
});
|
||||
|
||||
it('JBDSN-009: a journey keeps its newest five revisions', async () => {
|
||||
const { journey, owner } = await crew();
|
||||
const books = t.get(JourneyBookService);
|
||||
|
||||
Reference in New Issue
Block a user