|
| 1 | +// @ts-check |
| 2 | +// Deleting a chat must not lose its messages when the state write fails. |
| 3 | +// |
| 4 | +// The transcript is the one thing that cannot be rebuilt, so the state that forgets the |
| 5 | +// chat goes to disk first. A failed write then leaves the chat and its messages both as |
| 6 | +// they were, and the caller can simply try again. This file fails if that order flips |
| 7 | +// back: the failure injection below replaces state.json with a directory of the same name, |
| 8 | +// so every write to it fails. |
| 9 | + |
| 10 | +import assert from 'node:assert/strict'; |
| 11 | +import { test } from 'node:test'; |
| 12 | +import { mkdtemp, mkdir, readFile, rm, writeFile } from 'node:fs/promises'; |
| 13 | +import { tmpdir } from 'node:os'; |
| 14 | +import { join } from 'node:path'; |
| 15 | + |
| 16 | +import { Store } from '../miniapp/node/store.mjs'; |
| 17 | + |
| 18 | +async function fixture(t) { |
| 19 | + const dir = await mkdtemp(join(tmpdir(), 'bot-delete-chat-')); |
| 20 | + t.after(() => rm(dir, { recursive: true, force: true })); |
| 21 | + const store = new Store(dir); |
| 22 | + await store.init(); |
| 23 | + return { dir, store, chat: store.chatIds()[0] }; |
| 24 | +} |
| 25 | + |
| 26 | +test('a failed state write leaves the chat and its messages untouched', async (t) => { |
| 27 | + const { dir, store, chat } = await fixture(t); |
| 28 | + await store.appendMessage(chat, { role: 'user', text: 'the greeting' }); |
| 29 | + await store.appendMessage(chat, { role: 'bot', text: 'the answer' }); |
| 30 | + store.state.sessions[chat] = { sessionId: 's_1', stamp: 1, lastSeen: 1 }; |
| 31 | + await store.save(); |
| 32 | + // The store seeds a greeting of its own, so the count is read, never assumed. |
| 33 | + const before = (await store.getMessages(chat)).length; |
| 34 | + assert.ok(before >= 2, 'the chat has a transcript to lose'); |
| 35 | + |
| 36 | + // The state file becomes a directory: every write to it fails. |
| 37 | + const statePath = join(dir, 'state.json'); |
| 38 | + const saved = await readFile(statePath, 'utf8'); |
| 39 | + await rm(statePath, { force: true }); |
| 40 | + await mkdir(statePath, { recursive: true }); |
| 41 | + |
| 42 | + await assert.rejects(() => store.deleteChat(chat)); |
| 43 | + |
| 44 | + // The transcript is still on disk, and the live state still knows the chat. |
| 45 | + const onDisk = JSON.parse(await readFile(store.messagePath(chat), 'utf8')); |
| 46 | + assert.equal(onDisk.length, before, 'the messages are still on disk'); |
| 47 | + assert.ok(store.state.sessions[chat], 'the live state still knows the chat'); |
| 48 | + |
| 49 | + // Put the state file back and restart: the chat is still there with its messages. |
| 50 | + await rm(statePath, { recursive: true, force: true }); |
| 51 | + await writeFile(statePath, saved, 'utf8'); |
| 52 | + const restarted = new Store(dir); |
| 53 | + await restarted.init(); |
| 54 | + const messages = await restarted.getMessages(chat); |
| 55 | + assert.equal(messages.length, before, 'the messages survive the restart'); |
| 56 | + assert.ok(messages.some((message) => message.text === 'the greeting'), 'the greeting is still there'); |
| 57 | + assert.ok(restarted.state.sessions[chat], 'the session survives the restart'); |
| 58 | +}); |
| 59 | + |
| 60 | +test('a delete that succeeds clears the transcript and forgets the chat', async (t) => { |
| 61 | + const { dir, store, chat } = await fixture(t); |
| 62 | + await store.appendMessage(chat, { role: 'user', text: 'the greeting' }); |
| 63 | + await store.appendMessage(chat, { role: 'bot', text: 'the answer' }); |
| 64 | + store.state.sessions[chat] = { sessionId: 's_1', stamp: 1, lastSeen: 1 }; |
| 65 | + await store.save(); |
| 66 | + |
| 67 | + await store.deleteChat(chat); |
| 68 | + |
| 69 | + assert.equal((await store.getMessages(chat)).length, 0, 'the transcript is empty'); |
| 70 | + assert.equal(store.state.sessions[chat], undefined, 'the chat is forgotten'); |
| 71 | + const restarted = new Store(dir); |
| 72 | + await restarted.init(); |
| 73 | + assert.equal((await restarted.getMessages(chat)).length, 0, 'the transcript stays empty'); |
| 74 | + assert.equal(restarted.state.sessions[chat], undefined, 'the chat stays forgotten'); |
| 75 | +}); |
0 commit comments