mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-20 13:09:33 +01:00
test(discord): the review items of #832 that go on top
A test pins the symlink and non-file branch of the reply store (a planted link or a folder at the metadata path is never replaced, and replies are still accepted), the @fileoverview names the file, its bounds, where the bridge claims and the four rules a reader has to keep, and a comment left from the #793 journal approach says store file. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a9ed3f55c3
commit
d00a8d6cbf
2 files changed
+41
-3
No files matched your search
@@ -1,4 +1,24 @@
|
|||||||
/** @fileoverview Best-effort replay protection. A metadata-store failure must never block a person's reply. */
|
/**
|
||||||
|
* @fileoverview Best-effort replay protection for Discord replies.
|
||||||
|
*
|
||||||
|
* Owns `<data dir>/discord-reply-dedupe.json`: at most 2,048 `guild:channel:message`
|
||||||
|
* ids with the time each was claimed, kept for 7 days, written 0600 through a temp
|
||||||
|
* file and a rename. It holds ids only, never message content.
|
||||||
|
*
|
||||||
|
* `DiscordBridge.handleMessage()` calls `claim()` once per allowed message, after the
|
||||||
|
* guild, allowlist and thread gates and before the rate limit, the exited-agent check,
|
||||||
|
* any download and any typing, so a gateway replay of a message that was refused is
|
||||||
|
* refused again rather than typed.
|
||||||
|
*
|
||||||
|
* Rules a reader has to keep:
|
||||||
|
* - A store failure never costs a reply: the claim falls back to memory, warns once
|
||||||
|
* per outage and the reply is still typed.
|
||||||
|
* - A file that could not be read (an errno failure, a symlink, anything but a regular
|
||||||
|
* file) is never overwritten.
|
||||||
|
* - Readable but invalid content (bad JSON, bad rows, oversized) is replaced.
|
||||||
|
* - Rows read from disk go first, so claims made while the file was unreadable are
|
||||||
|
* the ones the 2,048 bound keeps.
|
||||||
|
*/
|
||||||
import fs from 'node:fs/promises';
|
import fs from 'node:fs/promises';
|
||||||
import { join } from 'node:path';
|
import { join } from 'node:path';
|
||||||
import { randomUUID } from 'node:crypto';
|
import { randomUUID } from 'node:crypto';
|
||||||
@@ -69,7 +89,7 @@ export class DiscordReplyDedupe {
|
|||||||
this.dirty = true;
|
this.dirty = true;
|
||||||
} // Before awaiting IO or typing, including during an outage.
|
} // Before awaiting IO or typing, including during an outage.
|
||||||
while (this.seen.size > LIMIT) this.seen.delete(this.seen.keys().next().value!);
|
while (this.seen.size > LIMIT) this.seen.delete(this.seen.keys().next().value!);
|
||||||
if (!this.loaded || !this.dirty) return fresh; // Do not overwrite a journal we could not read.
|
if (!this.loaded || !this.dirty) return fresh; // Never overwrite a store file we could not read.
|
||||||
const tmp = `${path}.${randomUUID()}.tmp`;
|
const tmp = `${path}.${randomUUID()}.tmp`;
|
||||||
try {
|
try {
|
||||||
await fs.mkdir(this.dir, { recursive: true });
|
await fs.mkdir(this.dir, { recursive: true });
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { mkdtemp, readFile, rm, writeFile, stat } from 'node:fs/promises';
|
import { lstat, mkdir, mkdtemp, readFile, rm, symlink, writeFile, stat } from 'node:fs/promises';
|
||||||
import { tmpdir } from 'node:os';
|
import { tmpdir } from 'node:os';
|
||||||
import { join } from 'node:path';
|
import { join } from 'node:path';
|
||||||
import { afterEach, beforeEach, expect, it, vi } from 'vitest';
|
import { afterEach, beforeEach, expect, it, vi } from 'vitest';
|
||||||
@@ -131,3 +131,21 @@ it('heals oversized regular metadata and persists replay protection across resta
|
|||||||
expect((await stat(path)).size).toBeLessThan(512 * 1024);
|
expect((await stat(path)).size).toBeLessThan(512 * 1024);
|
||||||
expect(warn).toHaveBeenCalledTimes(1);
|
expect(warn).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
it('never replaces a symlink or a folder at the metadata path, and still accepts replies', async () => {
|
||||||
|
const path = join(dir, 'discord-reply-dedupe.json');
|
||||||
|
const target = join(dir, 'target');
|
||||||
|
await writeFile(target, 'keep');
|
||||||
|
await symlink(target, path);
|
||||||
|
const warn = vi.fn(),
|
||||||
|
store = new DiscordReplyDedupe(dir, Date.now, warn);
|
||||||
|
expect(await store.claim(guild, channel, message)).toBe(true);
|
||||||
|
expect(await store.claim(guild, channel, message)).toBe(false);
|
||||||
|
expect(await store.claim(guild, channel, '444444444444444444')).toBe(true);
|
||||||
|
expect((await lstat(path)).isSymbolicLink()).toBe(true);
|
||||||
|
expect(await readFile(target, 'utf8')).toBe('keep');
|
||||||
|
expect(warn).toHaveBeenCalledTimes(1);
|
||||||
|
await rm(path);
|
||||||
|
await mkdir(path);
|
||||||
|
expect(await new DiscordReplyDedupe(dir, Date.now, warn).claim(guild, channel, message)).toBe(true);
|
||||||
|
expect((await lstat(path)).isDirectory()).toBe(true);
|
||||||
|
});
|
||||||
Reference in new issue
Block a user