Files
ttrpg_manager/server/src/campaigns.test.ts
T
NilsBriggen ba5ae37111 V2 P5-backend + P7 + P8: conflict-safe sync, revocable invites, feats, multiclass, tests
P5 backend (the deferred items):
- conflict-aware cloud sync via optimistic concurrency: the server versions each
  backup blob (file mtime) and rejects a push whose base version is stale (409),
  so a second device can't silently overwrite a newer cloud copy. The client
  tracks its last-synced version; the SyncStatusIndicator surfaces a "Sync
  conflict" with one-click resolution (Use cloud / Keep this device), and the
  Settings push offers the same choice.
- revocable invites: owner can rotate a campaign's invite code (kills the old
  one) and remove a member (drops them + deletes their published characters).
  (Skipped editor/player roles — no enforcement target in the current model.)
- image storage: deliberately deferred (live images already stream de-duped on a
  separate channel; a backup blob-store is infra with no correctness gain).

P7 — subclass/feat/multiclass depth:
- feats are now first-class tracked records (name + effect text) in a Feats &
  Features sheet section, not buried in notes. Dexie v15 migration.
- 5e multiclass spell-slot calculator: a pure multiclassSlots() (full ×1, half
  ÷2, third ÷3 → full-caster table) + a sheet control to fill the slot table for
  multiclass casters. (Honest scope: tracking + slots, not per-subclass feature
  automation.)

P8 — test hardening: v2-surfaces e2e (feats, multiclass calc, signals bell,
World nav) + server tests (blob versioning, invite rotation, member removal) +
multiclass unit tests.

Gate green: 283 unit + 35 e2e + 2 realtime. App + server build clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-09 10:34:29 +02:00

80 lines
3.7 KiB
TypeScript

import { describe, it, expect, beforeEach } from 'vitest';
import { promises as fs } from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { CloudStore } from './campaigns';
describe('CloudStore', () => {
let dir: string;
let store: CloudStore;
beforeEach(async () => { dir = await fs.mkdtemp(path.join(os.tmpdir(), 'cloud-')); store = new CloudStore(dir); });
it('creates a campaign and lets a player join by invite', async () => {
const c = await store.createCampaign('gm1', 'Strahd', '5e');
expect(c.ownerUserId).toBe('gm1');
expect(c.inviteCode).toHaveLength(6);
expect(store.isMember(c.id, 'gm1')).toBe(true);
expect(await store.joinByInvite('p1', 'ZZZZZZ')).toBeNull(); // bad code
const joined = await store.joinByInvite('p1', c.inviteCode);
expect(joined?.id).toBe(c.id);
expect(store.isMember(c.id, 'p1')).toBe(true);
expect(store.isMember(c.id, 'stranger')).toBe(false);
const forP1 = await store.listForUser('p1');
expect(forP1[0]).toMatchObject({ id: c.id, role: 'member' });
expect((await store.listForUser('gm1'))[0]).toMatchObject({ role: 'owner' });
});
it('enforces character ownership on upsert', async () => {
const c = await store.createCampaign('gm1', 'C', '5e');
await store.joinByInvite('p1', c.inviteCode);
await store.joinByInvite('p2', c.inviteCode);
expect(await store.putCharacter('stranger', c.id, { id: 'ch1', name: 'X', data: '{}' })).toBeNull(); // not a member
const r = await store.putCharacter('p1', c.id, { id: 'ch1', name: 'Lia', data: '{"hp":1}' });
expect(r?.ownerUserId).toBe('p1');
expect(await store.putCharacter('p2', c.id, { id: 'ch1', name: 'hijack', data: '{}' })).toBeNull(); // p2 can't update p1's char
const all = await store.listCharacters(c.id);
expect(all).toHaveLength(1);
expect(all[0]!.name).toBe('Lia');
expect(await store.usageBytes('p1')).toBeGreaterThan(0);
});
it('lets the char owner or campaign owner delete; persists across instances', async () => {
const c = await store.createCampaign('gm1', 'C', '5e');
await store.joinByInvite('p1', c.inviteCode);
await store.putCharacter('p1', c.id, { id: 'ch1', name: 'Lia', data: '{}' });
expect(await store.removeCharacter('p2', 'ch1')).toBe(false); // unrelated user
expect(await store.removeCharacter('gm1', 'ch1')).toBe(true); // campaign owner may remove
const store2 = new CloudStore(dir);
await store2.load();
expect((await store2.listForUser('gm1'))[0]?.id).toBe(c.id);
expect(await store2.listCharacters(c.id)).toHaveLength(0);
});
it('rotates the invite code (owner only) so the old code stops working', async () => {
const c = await store.createCampaign('gm1', 'C', '5e');
const old = c.inviteCode;
expect(await store.rotateInvite('p1', c.id)).toBeNull(); // non-owner can't
const fresh = await store.rotateInvite('gm1', c.id);
expect(fresh).toBeTruthy();
expect(fresh).not.toBe(old);
expect(await store.joinByInvite('p1', old)).toBeNull(); // old code is dead
expect((await store.joinByInvite('p1', fresh!))?.id).toBe(c.id); // new code works
});
it('removes a member and their characters (owner only)', async () => {
const c = await store.createCampaign('gm1', 'C', '5e');
await store.joinByInvite('p1', c.inviteCode);
await store.putCharacter('p1', c.id, { id: 'ch1', name: 'Lia', data: '{}' });
expect(await store.removeMember('p1', c.id, 'p1')).toBe(false); // non-owner can't
expect(await store.removeMember('gm1', c.id, 'p1')).toBe(true);
expect(store.isMember(c.id, 'p1')).toBe(false);
expect(await store.listCharacters(c.id)).toHaveLength(0); // their char is gone
});
});