[schema] PlanetGarden: The gardener's look, from fixed lists, kept on the device
This commit is contained in:
@@ -12,7 +12,7 @@
|
||||
import Dexie, { type DexieOptions, type Table } from 'dexie';
|
||||
import { newCare, newOpened, type NamedPlace } from '@rosea/engine';
|
||||
import { isIdentity, type Identity } from '@rosea/identity';
|
||||
import { DATABASE_NAME, type CareEvent, type SentenceOpened } from '@rosea/schema';
|
||||
import { DATABASE_NAME, GARDENER_FIELDS, GARDENER_OPTIONS, type CareEvent, type Gardener, type SentenceOpened } from '@rosea/schema';
|
||||
|
||||
export { DATABASE_NAME };
|
||||
|
||||
@@ -34,6 +34,17 @@ interface KeyRow {
|
||||
}
|
||||
|
||||
const SUNSET_PLACE = 'sunsetPlace';
|
||||
const GARDENER = 'gardener';
|
||||
|
||||
/** The shape check for a stored gardener: exactly the six fields, each from its list. */
|
||||
export function asGardener(value: unknown): Gardener | null {
|
||||
if (typeof value !== 'object' || value === null) return null;
|
||||
const v = value as Record<string, unknown>;
|
||||
const keys = GARDENER_FIELDS;
|
||||
if (Object.keys(v).length !== keys.length) return null;
|
||||
for (const k of keys) if (!(GARDENER_OPTIONS[k] as readonly unknown[]).includes(v[k])) return null;
|
||||
return Object.fromEntries(keys.map((k) => [k, v[k]])) as unknown as Gardener;
|
||||
}
|
||||
const SELF = 'self';
|
||||
const PAST_KEYS = 'pastKeys';
|
||||
|
||||
@@ -55,6 +66,9 @@ export interface RoseFile {
|
||||
export interface Storage {
|
||||
/** The saved sunset place, or null if the player has not answered yet. */
|
||||
loadSunsetPlace(): Promise<SunsetPlace | null>;
|
||||
/** The player's gardener, or null before they chose one (planet-garden.md G6). */
|
||||
loadGardener(): Promise<Gardener | null>;
|
||||
saveGardener(look: Gardener): Promise<void>;
|
||||
saveSunsetPlace(place: SunsetPlace): Promise<void>;
|
||||
/** Appends one act of care. The log is never edited or shortened. */
|
||||
appendCare(event: CareEvent): Promise<void>;
|
||||
@@ -194,7 +208,7 @@ class RoseaDatabase extends Dexie {
|
||||
}
|
||||
|
||||
/** What another tab changed (two-tabs.md W2): only which part, never the data. */
|
||||
export type Change = 'log' | 'place';
|
||||
export type Change = 'log' | 'place' | 'gardener';
|
||||
|
||||
/** The channel every tab of the garden announces its writes on (two-tabs.md W1). */
|
||||
export const CHANGES_CHANNEL = `${DATABASE_NAME}-changes`;
|
||||
@@ -213,7 +227,7 @@ export function openStorage(options?: DexieOptions, settings: { readonly channel
|
||||
if (channel) {
|
||||
channel.onmessage = (message: MessageEvent<unknown>) => {
|
||||
const what = message.data;
|
||||
if (what !== 'log' && what !== 'place') return;
|
||||
if (what !== 'log' && what !== 'place' && what !== 'gardener') return;
|
||||
for (const listener of listeners) {
|
||||
try {
|
||||
listener(what);
|
||||
@@ -250,6 +264,19 @@ export function openStorage(options?: DexieOptions, settings: { readonly channel
|
||||
await db.profile.put({ key: SUNSET_PLACE, value: plain });
|
||||
announce('place');
|
||||
},
|
||||
async loadGardener() {
|
||||
const row = await db.profile.get(GARDENER);
|
||||
if (row === undefined) return null;
|
||||
const look = asGardener(row.value);
|
||||
if (look === null) console.warn('storage: the saved gardener has an unknown shape; asking again', row.value);
|
||||
return look;
|
||||
},
|
||||
async saveGardener(look) {
|
||||
const plain = asGardener(look);
|
||||
if (plain === null) throw new TypeError('storage: refusing to save a gardener of an unknown shape');
|
||||
await db.profile.put({ key: GARDENER, value: plain });
|
||||
announce('gardener');
|
||||
},
|
||||
async appendCare(event) {
|
||||
const plain = asCareEvent(event);
|
||||
if (plain === null) throw new TypeError('storage: refusing to append a care event of an unknown shape');
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
import { GARDENER_FIELDS, GARDENER_OPTIONS, type Gardener } from '@rosea/schema';
|
||||
import { validateGardener } from '@rosea/schema/validate';
|
||||
import { IDBFactory, IDBKeyRange } from 'fake-indexeddb';
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { asGardener, openStorage, type Change, type Storage } from '../src/index.js';
|
||||
|
||||
const look: Gardener = { presentation: 'girl', skin: 'skin-3', hair: 'braids', hairColour: 'auburn', clothes: 'sage', hat: 'straw-hat' };
|
||||
const KEY = 'K'.repeat(87);
|
||||
|
||||
const opened: Storage[] = [];
|
||||
const open = (indexedDB: IDBFactory = new IDBFactory(), channel: string | null = null): Storage => {
|
||||
const s = openStorage({ indexedDB, IDBKeyRange }, { channel });
|
||||
opened.push(s);
|
||||
return s;
|
||||
};
|
||||
afterEach(() => {
|
||||
for (const s of opened.splice(0)) s.close();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe('the gardener (planet-garden.md G6)', () => {
|
||||
it('is null before the player chose, then kept across a reopen', async () => {
|
||||
const factory = new IDBFactory();
|
||||
const s = open(factory);
|
||||
expect(await s.loadGardener()).toBeNull();
|
||||
await s.saveGardener(new Proxy(look, {}));
|
||||
s.close();
|
||||
expect(await open(factory).loadGardener()).toEqual(look);
|
||||
});
|
||||
|
||||
it('refuses a value off its list, a missing field, an extra field, or free text', async () => {
|
||||
const s = open();
|
||||
for (const bad of [
|
||||
{ ...look, hat: 'crown' },
|
||||
{ ...look, clothes: 'green suit' },
|
||||
{ ...look, extra: 'a scarf' },
|
||||
(({ hat: _, ...rest }) => rest)(look),
|
||||
{ ...look, presentation: 'Marek' }
|
||||
]) {
|
||||
await expect(s.saveGardener(bad as unknown as Gardener)).rejects.toThrow(/unknown shape/);
|
||||
}
|
||||
expect(await s.loadGardener()).toBeNull();
|
||||
});
|
||||
|
||||
it('the hand check, the schema and the lists agree', () => {
|
||||
expect([...GARDENER_FIELDS].sort()).toEqual(Object.keys(GARDENER_OPTIONS).sort());
|
||||
for (const k of GARDENER_FIELDS) {
|
||||
for (const v of GARDENER_OPTIONS[k]) expect(validateGardener({ ...look, [k]: v }).ok).toBe(true);
|
||||
expect(asGardener({ ...look, [k]: 'other' })).toBeNull();
|
||||
expect(validateGardener({ ...look, [k]: 'other' }).ok).toBe(false);
|
||||
}
|
||||
expect(asGardener(look)).toEqual(look);
|
||||
});
|
||||
|
||||
it('never offers what would recreate the book\'s boy', () => {
|
||||
const everything = JSON.stringify(GARDENER_OPTIONS).toLowerCase();
|
||||
for (const word of ['scarf', 'coat', 'cape', 'suit', 'crown', 'sword', 'golden']) expect(everything).not.toContain(word);
|
||||
});
|
||||
|
||||
it('is announced to the other tabs, and never travels in the rose file', async () => {
|
||||
const factory = new IDBFactory();
|
||||
const a = open(factory, 'rosea-test-gardener');
|
||||
const b = open(factory, 'rosea-test-gardener');
|
||||
const heard: Change[] = [];
|
||||
b.onChange((w) => heard.push(w));
|
||||
await a.saveGardener(look);
|
||||
await new Promise((r) => setTimeout(r, 100));
|
||||
expect(heard).toEqual(['gardener']);
|
||||
expect(JSON.stringify(await a.exportRose(KEY))).not.toMatch(/braids|auburn|skin-3|gardener/);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user