Make subreddit list editable from the dashboard

Adds an inline subreddit editor on the dashboard that lets you add,
remove, and edit (tone, type, weight) the subreddit list, with changes
persisted to config.yaml on disk so they survive restarts.

Backend
- src/config/persistence.ts: round-trips config.yaml via
  YAML.parseDocument, mutating existing row nodes in place so their
  trailing inline `# ...` comments survive an edit; new rows are
  appended as flow-style maps to match the on-disk idiom; removed rows
  are dropped. Default values ('default' tone, 'image' type) are
  omitted on write so the file stays clean. Atomic via tmp+rename.
- src/web/routes/config.ts: GET /api/config now also returns the
  current subreddit list and the available tone keys. PUT /api/config
  accepts a `subreddits` field, validates with the existing
  SubredditConfigSchema, rejects duplicates, persists, then mutates
  ctx.config.reddit.subreddits in place so the processor picks up the
  new tone immediately without a restart.

Frontend
- public/index.html: replaces the read-only subreddit chip list in the
  Configuration snapshot section with a grid of editable rows (name,
  type, tone dropdown, weight, remove). + Add / Save buttons with
  inline status. Tones are sourced from /api/config so the catalog
  drives the dropdown.

Tests
- tests/config-persistence.test.ts: covers in-place mutation comment
  preservation, removal, appends, reorder, default omission, and
  atomic write cleanup.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
jjbruntonandClaude Opus 4.7 committed 2026-05-22 22:43:05 +01:00
1 parent 46a213e3e0
commit d9b9a79bdd
4 files changed
+476 -5

No files matched your search

+128
View File
@@ -0,0 +1,128 @@
import fs from 'fs';
import os from 'os';
import path from 'path';
import YAML from 'yaml';
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { persistSubreddits } from '../src/config/persistence.js';
const SAMPLE = `# top-level note that must survive
reddit:
# weights bias picking
subreddits:
- { name: AverageNudes, weight: 4, tone: 'direct' } # validation seekers
- { name: gonewild40plus, weight: 4, tone: 'gentle' } # mature
- { name: normalnudes, weight: 3 } # real bodies
- { name: tightandtoned, weight: 1 }
pollIntervalMs: 30000
userAgent: "ua"
`;
describe('persistSubreddits', () => {
let tmpDir: string;
let configPath: string;
beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'rb-config-'));
configPath = path.join(tmpDir, 'config.yaml');
fs.writeFileSync(configPath, SAMPLE, 'utf-8');
});
afterEach(() => {
fs.rmSync(tmpDir, { recursive: true, force: true });
});
it('mutates an existing row in place and preserves its inline comment', () => {
persistSubreddits(
[
{ name: 'AverageNudes', type: 'image', weight: 4, tone: 'playful' },
{ name: 'gonewild40plus', type: 'image', weight: 4, tone: 'gentle' },
{ name: 'normalnudes', type: 'image', weight: 3, tone: 'default' },
{ name: 'tightandtoned', type: 'image', weight: 1, tone: 'default' },
],
configPath,
);
const out = fs.readFileSync(configPath, 'utf-8');
expect(out).toMatch(/tone:\s*['"]?playful['"]?/);
expect(out).toContain('# validation seekers');
expect(out).toContain('# top-level note that must survive');
expect(out).toContain('# weights bias picking');
expect(out).toContain('# mature');
expect(out).toContain('pollIntervalMs: 30000');
});
it('drops removed rows (and their attached comments)', () => {
persistSubreddits(
[
{ name: 'AverageNudes', type: 'image', weight: 4, tone: 'direct' },
{ name: 'tightandtoned', type: 'image', weight: 1, tone: 'default' },
],
configPath,
);
const out = fs.readFileSync(configPath, 'utf-8');
expect(out).not.toContain('gonewild40plus');
expect(out).not.toContain('normalnudes');
expect(out).not.toContain('# mature');
expect(out).toContain('AverageNudes');
expect(out).toContain('tightandtoned');
expect(out).toContain('# validation seekers');
});
it('appends new rows in caller order', () => {
persistSubreddits(
[
{ name: 'AverageNudes', type: 'image', weight: 4, tone: 'direct' },
{ name: 'gonewild40plus', type: 'image', weight: 4, tone: 'gentle' },
{ name: 'normalnudes', type: 'image', weight: 3, tone: 'default' },
{ name: 'tightandtoned', type: 'image', weight: 1, tone: 'default' },
{ name: 'NewSub', type: 'chat', weight: 2, tone: 'playful' },
],
configPath,
);
const parsed = YAML.parse(fs.readFileSync(configPath, 'utf-8'));
const subs = parsed.reddit.subreddits;
expect(subs[subs.length - 1]).toMatchObject({
name: 'NewSub',
type: 'chat',
weight: 2,
tone: 'playful',
});
});
it("omits 'default' tone and 'image' type on write to match on-disk style", () => {
persistSubreddits(
[{ name: 'OnlyOne', type: 'image', weight: 1, tone: 'default' }],
configPath,
);
const out = fs.readFileSync(configPath, 'utf-8');
expect(out).not.toMatch(/tone:\s*default/);
expect(out).not.toMatch(/type:\s*image/);
});
it('atomic write — tmp file not left behind on success', () => {
persistSubreddits(
[{ name: 'OnlyOne', type: 'image', weight: 1, tone: 'direct' }],
configPath,
);
expect(fs.existsSync(configPath + '.tmp')).toBe(false);
});
it('reorders rows according to input order', () => {
persistSubreddits(
[
{ name: 'tightandtoned', type: 'image', weight: 1, tone: 'default' },
{ name: 'AverageNudes', type: 'image', weight: 4, tone: 'direct' },
{ name: 'gonewild40plus', type: 'image', weight: 4, tone: 'gentle' },
{ name: 'normalnudes', type: 'image', weight: 3, tone: 'default' },
],
configPath,
);
const parsed = YAML.parse(fs.readFileSync(configPath, 'utf-8'));
expect(parsed.reddit.subreddits.map((s: { name: string }) => s.name)).toEqual([
'tightandtoned',
'AverageNudes',
'gonewild40plus',
'normalnudes',
]);
});
});