Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
src/reports.ts keeps chat reports under FORGE_APP_DATA: one per person per line, at most 20 per reporter, newest 200 - module and test only #323
smith/arm:smith/316-src-reports-ts-keeps-chat-reports-under → main 1 commit fork
2 files
changed +136
−0
Software/so101/src/reports.test.ts added +66
| @@ -0,0 +1,66 @@ | ||
| 1 | import { afterEach, beforeEach, expect, spyOn, test } from "bun:test"; | |
| 2 | import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | import { Reports } from "./reports.ts"; | |
| 6 | ||
| 7 | let dir: string; | |
| 8 | let file: string; | |
| 9 | beforeEach(() => { | |
| 10 | dir = mkdtempSync(join(process.env.TMPDIR ?? tmpdir(), "reports-")); | |
| 11 | file = join(dir, "reports.json"); | |
| 12 | }); | |
| 13 | afterEach(() => rmSync(dir, { recursive: true, force: true })); | |
| 14 | ||
| 15 | const rep = (id: string, reporter: string, at = 1) => ({ id, text: `t${id}`, author: "a", reporter, at }); | |
| 16 | ||
| 17 | test("a repeat of the same reporter and line is ignored", () => { | |
| 18 | const r = new Reports(file); | |
| 19 | expect(r.add(rep("1", "u"))).toBe(true); | |
| 20 | expect(r.add(rep("1", "u"))).toBe(false); | |
| 21 | expect(r.add(rep("1", "v"))).toBe(true); | |
| 22 | expect(r.list()).toHaveLength(2); | |
| 23 | }); | |
| 24 | ||
| 25 | test("list is newest first", () => { | |
| 26 | const r = new Reports(file); | |
| 27 | r.add(rep("1", "u")); | |
| 28 | r.add(rep("2", "u")); | |
| 29 | expect(r.list().map((x) => x.id)).toEqual(["2", "1"]); | |
| 30 | }); | |
| 31 | ||
| 32 | test("a reporter keeps at most 20, dropping their oldest", () => { | |
| 33 | const r = new Reports(file); | |
| 34 | r.add(rep("other", "v")); | |
| 35 | for (let i = 0; i < 25; i++) r.add(rep(String(i), "u")); | |
| 36 | const mine = r.list().filter((x) => x.reporter === "u"); | |
| 37 | expect(mine).toHaveLength(20); | |
| 38 | expect(mine.at(-1)!.id).toBe("5"); | |
| 39 | expect(r.list().some((x) => x.reporter === "v")).toBe(true); | |
| 40 | }); | |
| 41 | ||
| 42 | test("at most 200 overall, dropping the oldest", () => { | |
| 43 | const r = new Reports(file); | |
| 44 | for (let i = 0; i < 210; i++) r.add(rep("l", `u${i}`)); | |
| 45 | expect(r.list()).toHaveLength(200); | |
| 46 | expect(r.list().at(-1)!.reporter).toBe("u10"); | |
| 47 | }); | |
| 48 | ||
| 49 | test("a restart restores the list", () => { | |
| 50 | const r = new Reports(file); | |
| 51 | r.add(rep("1", "u")); | |
| 52 | r.add(rep("2", "u")); | |
| 53 | const again = new Reports(file); | |
| 54 | expect(again.list()).toEqual(r.list()); | |
| 55 | expect(again.add(rep("2", "u"))).toBe(false); | |
| 56 | }); | |
| 57 | ||
| 58 | test("a corrupt file starts empty with one log line", () => { | |
| 59 | writeFileSync(file, "{not json"); | |
| 60 | const err = spyOn(console, "error").mockImplementation(() => {}); | |
| 61 | const r = new Reports(file); | |
| 62 | expect(r.list()).toEqual([]); | |
| 63 | expect(err).toHaveBeenCalledTimes(1); | |
| 64 | err.mockRestore(); | |
| 65 | expect(r.add(rep("1", "u"))).toBe(true); | |
| 66 | }); |
Software/so101/src/reports.ts added +70
| @@ -0,0 +1,70 @@ | ||
| 1 | /** | |
| 2 | * Chat reports: one per reporter per line, at most 20 per reporter and 200 overall, newest first. | |
| 3 | * Persisted as JSON at the path the caller gives; an unreadable file starts empty. | |
| 4 | */ | |
| 5 | import { mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs"; | |
| 6 | import { dirname } from "node:path"; | |
| 7 | ||
| 8 | export const MAX_TOTAL = 200; | |
| 9 | export const MAX_PER_REPORTER = 20; | |
| 10 | ||
| 11 | export interface Report { | |
| 12 | id: string; | |
| 13 | text: string; | |
| 14 | author: string; | |
| 15 | reporter: string; | |
| 16 | at: number; | |
| 17 | } | |
| 18 | ||
| 19 | function valid(r: unknown): r is Report { | |
| 20 | if (!r || typeof r !== "object") return false; | |
| 21 | const o = r as Record<string, unknown>; | |
| 22 | return ( | |
| 23 | typeof o.id === "string" && typeof o.text === "string" && typeof o.author === "string" && | |
| 24 | typeof o.reporter === "string" && typeof o.at === "number" && Number.isFinite(o.at) | |
| 25 | ); | |
| 26 | } | |
| 27 | ||
| 28 | export class Reports { | |
| 29 | /** Oldest first. */ | |
| 30 | private items: Report[] = []; | |
| 31 | ||
| 32 | constructor(private readonly file: string) { | |
| 33 | try { | |
| 34 | const raw = JSON.parse(readFileSync(file, "utf8")); | |
| 35 | if (!Array.isArray(raw)) throw new Error("not a list"); | |
| 36 | this.items = raw.filter(valid).slice(-MAX_TOTAL); | |
| 37 | } catch (err) { | |
| 38 | if ((err as NodeJS.ErrnoException).code !== "ENOENT") { | |
| 39 | console.error(`reports: could not read ${file}, starting empty: ${err instanceof Error ? err.message : String(err)}`); | |
| 40 | } | |
| 41 | } | |
| 42 | } | |
| 43 | ||
| 44 | /** False when this reporter already reported this line. */ | |
| 45 | add(r: Report): boolean { | |
| 46 | if (this.items.some((x) => x.id === r.id && x.reporter === r.reporter)) return false; | |
| 47 | this.items.push({ id: r.id, text: r.text, author: r.author, reporter: r.reporter, at: r.at }); | |
| 48 | const mine = this.items.filter((x) => x.reporter === r.reporter); | |
| 49 | if (mine.length > MAX_PER_REPORTER) this.items.splice(this.items.indexOf(mine[0]!), 1); | |
| 50 | if (this.items.length > MAX_TOTAL) this.items.shift(); | |
| 51 | this.save(); | |
| 52 | return true; | |
| 53 | } | |
| 54 | ||
| 55 | /** Newest first. */ | |
| 56 | list(): Report[] { | |
| 57 | return this.items.map((r) => ({ ...r })).reverse(); | |
| 58 | } | |
| 59 | ||
| 60 | private save(): void { | |
| 61 | try { | |
| 62 | mkdirSync(dirname(this.file), { recursive: true }); | |
| 63 | const tmp = `${this.file}.tmp`; | |
| 64 | writeFileSync(tmp, JSON.stringify(this.items)); | |
| 65 | renameSync(tmp, this.file); | |
| 66 | } catch (err) { | |
| 67 | console.error(`reports: could not write ${this.file}: ${err instanceof Error ? err.message : String(err)}`); | |
| 68 | } | |
| 69 | } | |
| 70 | } |
new above
offline