[repo] Workspace: Scaffold Phase 0 with schema, engine, content, ui and the PWA shell
pnpm + turbo workspace with the commands from CLAUDE.md §3. The contracts from §6 as JSON Schemas with generated types; localDay as the one place a day is computed; the forbidden-word list shared by lint:copy and the library validator, with 13 negative fixtures; a Svelte 5 PWA shell built under /Rosea/ with a meta CSP. E2E and offline suites run against the built bundle; the offline one closes the server rather than emulating. Analysis: .agents/features/phase0-workspace.md
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"name": "@rosea/schema",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"description": "THE source of truth: JSON Schemas for hope sentences, care events and traces; generated types; the validator. Imports nothing from this repository.",
|
||||
"main": "./src/index.ts",
|
||||
"types": "./src/index.ts",
|
||||
"exports": {
|
||||
".": "./src/index.ts",
|
||||
"./validate": "./src/validate.ts",
|
||||
"./schemas/*": "./schemas/*"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "node scripts/generate-types.mjs && tsc -p tsconfig.json",
|
||||
"typecheck": "tsc -p tsconfig.json",
|
||||
"test": "vitest run --reporter=verbose"
|
||||
},
|
||||
"dependencies": {
|
||||
"ajv": "8.20.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"json-schema-to-typescript": "15.0.4",
|
||||
"typescript": "5.9.3",
|
||||
"vitest": "3.2.7"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"$id": "https://rosea.invalid/schema/care-event.json",
|
||||
"title": "CareEvent",
|
||||
"description": "One act of care, append-only and never edited (CLAUDE.md §6.2). The other event kinds arrive with docs/GAMEPLAY.md.",
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["kind", "ts", "localDay", "from", "to", "act", "trace", "messageId", "clientVersion"],
|
||||
"properties": {
|
||||
"kind": { "const": "care" },
|
||||
"ts": { "description": "Epoch milliseconds. Stays on the device; never part of a trace.", "type": "integer", "minimum": 0 },
|
||||
"localDay": {
|
||||
"description": "The player's local day, from engine/src/time/localDay.ts and nowhere else.",
|
||||
"type": "string",
|
||||
"pattern": "^\\d{4}-\\d{2}-\\d{2}$"
|
||||
},
|
||||
"from": { "$ref": "#/definitions/publicKey" },
|
||||
"to": { "$ref": "#/definitions/publicKey" },
|
||||
"act": { "type": "string", "enum": ["water", "shade", "weed", "petals"] },
|
||||
"trace": { "type": "string", "pattern": "^petal:[a-z]+(-[a-z]+)*$" },
|
||||
"messageId": {
|
||||
"description": "A hope-library id or null. Never text: the pattern admits no space, so no sentence fits (CLAUDE.md §5.6 rule 1).",
|
||||
"oneOf": [
|
||||
{ "type": "string", "pattern": "^[a-z0-9]+(-[a-z0-9]+)*$", "maxLength": 64 },
|
||||
{ "type": "null" }
|
||||
]
|
||||
},
|
||||
"clientVersion": { "type": "string", "pattern": "^\\d+\\.\\d+\\.\\d+$" }
|
||||
},
|
||||
"definitions": {
|
||||
"publicKey": {
|
||||
"description": "An opaque base64url public key. Its exact encoding is decided with packages/identity.",
|
||||
"type": "string",
|
||||
"pattern": "^[A-Za-z0-9_-]{32,128}$"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"$id": "https://rosea.invalid/schema/hope-sentence.json",
|
||||
"title": "HopeSentence",
|
||||
"description": "One entry of the curated hope library (CLAUDE.md §6.1). The only words one player can send another are ids of these.",
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["id", "text", "source", "use", "tone", "promisesOutcome"],
|
||||
"properties": {
|
||||
"id": {
|
||||
"description": "Stable, lowercase, hyphenated. A message between players carries only this.",
|
||||
"type": "string",
|
||||
"pattern": "^[a-z0-9]+(-[a-z0-9]+)*$",
|
||||
"maxLength": 64
|
||||
},
|
||||
"text": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["en"],
|
||||
"properties": {
|
||||
"en": { "type": "string", "minLength": 1 },
|
||||
"cs": { "type": "string", "minLength": 1 }
|
||||
}
|
||||
},
|
||||
"source": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["work", "author", "tradition", "translation", "publicDomain", "verifiedBy", "verifiedAt"],
|
||||
"properties": {
|
||||
"work": { "type": "string", "minLength": 1 },
|
||||
"author": { "type": ["string", "null"] },
|
||||
"tradition": {
|
||||
"description": "A lowercase tag such as `biblical` or `stoic`. `rosea` marks a line the author wrote for this game.",
|
||||
"type": "string",
|
||||
"pattern": "^[a-z]+(-[a-z]+)*$"
|
||||
},
|
||||
"translation": { "type": ["string", "null"] },
|
||||
"publicDomain": { "type": "boolean" },
|
||||
"verifiedBy": { "type": "string", "minLength": 1 },
|
||||
"verifiedAt": { "type": "string", "pattern": "^\\d{4}-\\d{2}-\\d{2}$" }
|
||||
}
|
||||
},
|
||||
"use": {
|
||||
"type": "array",
|
||||
"minItems": 1,
|
||||
"uniqueItems": true,
|
||||
"items": { "type": "string", "enum": ["sunset", "comfort", "thanks"] }
|
||||
},
|
||||
"tone": { "type": "string", "pattern": "^[a-z]+(-[a-z]+)*$" },
|
||||
"promisesOutcome": { "type": "boolean" }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"$id": "https://rosea.invalid/schema/trace.json",
|
||||
"title": "Trace",
|
||||
"description": "An anonymous shared-sky trace, Phase 4, opt-in only (CLAUDE.md §6.3). No timestamp finer than a day, no precise location, no free text, no link to the friend graph.",
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["traceId", "geocell", "dayBucket", "gesture", "sig"],
|
||||
"properties": {
|
||||
"traceId": { "description": "Random and rotating.", "type": "string", "pattern": "^[A-Za-z0-9_-]{16,64}$" },
|
||||
"geocell": {
|
||||
"description": "A coarse cell id. The cell system and its density-adaptive size are decided in the Phase 4 feature doc.",
|
||||
"type": "string",
|
||||
"pattern": "^[a-z0-9]{4,16}$"
|
||||
},
|
||||
"dayBucket": { "type": "string", "pattern": "^\\d{4}-\\d{2}-\\d{2}$" },
|
||||
"gesture": { "type": "string", "enum": ["petal", "light"] },
|
||||
"sig": { "type": "string", "pattern": "^[A-Za-z0-9_-]{16,256}$" }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
// Generates TypeScript types from the JSON Schemas (CLAUDE.md §7: contract types
|
||||
// are generated, never hand-written). Output is git-ignored; `build` runs first
|
||||
// in every turbo task that needs it.
|
||||
import { mkdirSync, readFileSync, readdirSync, writeFileSync } from 'node:fs';
|
||||
import { basename, join } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { compile } from 'json-schema-to-typescript';
|
||||
|
||||
const root = fileURLToPath(new URL('..', import.meta.url));
|
||||
const schemaDir = join(root, 'schemas');
|
||||
const outDir = join(root, 'src', 'generated');
|
||||
mkdirSync(outDir, { recursive: true });
|
||||
|
||||
const banner = '/* Generated by packages/schema/scripts/generate-types.mjs. Do not edit. */';
|
||||
const names = [];
|
||||
|
||||
for (const file of readdirSync(schemaDir).filter((f) => f.endsWith('.schema.json')).sort()) {
|
||||
const schema = JSON.parse(readFileSync(join(schemaDir, file), 'utf8'));
|
||||
const name = basename(file, '.schema.json');
|
||||
const ts = await compile(schema, schema.title, { bannerComment: banner, additionalProperties: false });
|
||||
writeFileSync(join(outDir, `${name}.ts`), ts);
|
||||
names.push(name);
|
||||
}
|
||||
|
||||
writeFileSync(
|
||||
join(outDir, 'index.ts'),
|
||||
`${banner}\n${names.map((n) => `export type * from './${n}.js';`).join('\n')}\n`
|
||||
);
|
||||
|
||||
console.log(`[schema] generated types for ${names.length} schemas: ${names.join(', ')}`);
|
||||
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* The product's name, in one of the three places it may appear in source
|
||||
* (CLAUDE.md preamble). The other two are `apps/rosea/index.html` and the web
|
||||
* manifest, which `apps/rosea/vite.config.ts` builds from these constants.
|
||||
*
|
||||
* `Rosea` is a working name. Renaming must stay a one-commit operation: change
|
||||
* these, `index.html`, the `@rosea/*` scope and nothing else.
|
||||
*/
|
||||
|
||||
/** What the player reads. *Domi* never appears alone. */
|
||||
export const PRODUCT_NAME = 'Rosea Domi';
|
||||
|
||||
/** The short form, for the home-screen label. */
|
||||
export const PRODUCT_SHORT_NAME = 'Rosea';
|
||||
|
||||
/** The IndexedDB database name (CLAUDE.md §4). Changes together with the name. */
|
||||
export const DATABASE_NAME = 'rosea';
|
||||
@@ -0,0 +1,2 @@
|
||||
export * from './constants.js';
|
||||
export type * from './generated/index.js';
|
||||
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* Schema validation, for Node only: scripts, tests and the library validator.
|
||||
*
|
||||
* It is a separate entry point (`@rosea/schema/validate`) on purpose. Ajv
|
||||
* compiles validators with `new Function`, which the app's CSP forbids
|
||||
* (`script-src 'self'`, no `'unsafe-eval'`). Importing it from the browser bundle
|
||||
* would fail at runtime, not at build time. When the app needs to validate, it
|
||||
* gets standalone pre-compiled validators (GAP_ANALYSIS G0.12).
|
||||
*/
|
||||
import { Ajv, type ErrorObject, type ValidateFunction } from 'ajv';
|
||||
import careEvent from '../schemas/care-event.schema.json' with { type: 'json' };
|
||||
import hopeSentence from '../schemas/hope-sentence.schema.json' with { type: 'json' };
|
||||
import trace from '../schemas/trace.schema.json' with { type: 'json' };
|
||||
import type { CareEvent, HopeSentence, Trace } from './generated/index.js';
|
||||
|
||||
export type SchemaResult<T> =
|
||||
| { readonly ok: true; readonly value: T }
|
||||
| { readonly ok: false; readonly errors: readonly string[] };
|
||||
|
||||
const ajv = new Ajv({ allErrors: true, strict: true });
|
||||
|
||||
function wrap<T>(validate: ValidateFunction): (input: unknown) => SchemaResult<T> {
|
||||
return (input) =>
|
||||
validate(input)
|
||||
? { ok: true, value: input as T }
|
||||
: { ok: false, errors: (validate.errors ?? []).map(describe) };
|
||||
}
|
||||
|
||||
function describe(error: ErrorObject): string {
|
||||
const where = error.instancePath === '' ? '(root)' : error.instancePath;
|
||||
// Ajv already names a missing property in its message; an extra one it does not.
|
||||
const extra =
|
||||
error.keyword === 'additionalProperties'
|
||||
? ` '${String((error.params as { additionalProperty?: unknown }).additionalProperty)}'`
|
||||
: '';
|
||||
return `${where} ${error.message ?? error.keyword}${extra}`;
|
||||
}
|
||||
|
||||
export const validateHopeSentence = wrap<HopeSentence>(ajv.compile(hopeSentence));
|
||||
export const validateCareEvent = wrap<CareEvent>(ajv.compile(careEvent));
|
||||
export const validateTrace = wrap<Trace>(ajv.compile(trace));
|
||||
@@ -0,0 +1,112 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { validateCareEvent, validateHopeSentence, validateTrace } from '../src/validate.js';
|
||||
|
||||
const KEY_A = 'A'.repeat(43);
|
||||
const KEY_B = 'b_-'.repeat(15);
|
||||
|
||||
const careEvent = {
|
||||
kind: 'care',
|
||||
ts: 1761500000000,
|
||||
localDay: '2026-10-24',
|
||||
from: KEY_A,
|
||||
to: KEY_B,
|
||||
act: 'water',
|
||||
trace: 'petal:pale-pink',
|
||||
messageId: 'ps-23-4',
|
||||
clientVersion: '0.1.0'
|
||||
};
|
||||
|
||||
const sentence = {
|
||||
id: 'ps-23-4',
|
||||
text: { en: 'Placeholder text for a schema test.' },
|
||||
source: {
|
||||
work: 'Psalms 23:4',
|
||||
author: null,
|
||||
tradition: 'biblical',
|
||||
translation: 'KJV',
|
||||
publicDomain: true,
|
||||
verifiedBy: 'Test',
|
||||
verifiedAt: '2026-11-01'
|
||||
},
|
||||
use: ['sunset'],
|
||||
tone: 'gentle',
|
||||
promisesOutcome: false
|
||||
};
|
||||
|
||||
const trace = {
|
||||
traceId: 'r'.repeat(22),
|
||||
geocell: 'u2fkb',
|
||||
dayBucket: '2026-10-24',
|
||||
gesture: 'petal',
|
||||
sig: 's'.repeat(64)
|
||||
};
|
||||
|
||||
describe('hope sentence', () => {
|
||||
it('accepts a complete entry', () => {
|
||||
expect(validateHopeSentence(sentence)).toMatchObject({ ok: true });
|
||||
});
|
||||
|
||||
it('rejects an entry with no source', () => {
|
||||
const { source: _source, ...rest } = sentence;
|
||||
const result = validateHopeSentence(rest);
|
||||
expect(result.ok).toBe(false);
|
||||
expect(!result.ok && result.errors.join('\n')).toContain("'source'");
|
||||
});
|
||||
|
||||
it('rejects an id with a space, because an id is what travels between players', () => {
|
||||
expect(validateHopeSentence({ ...sentence, id: 'you are seen' }).ok).toBe(false);
|
||||
});
|
||||
|
||||
it('rejects an unknown use', () => {
|
||||
expect(validateHopeSentence({ ...sentence, use: ['push-notification'] }).ok).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('care event', () => {
|
||||
it('accepts every act the rose has', () => {
|
||||
for (const act of ['water', 'shade', 'weed', 'petals']) {
|
||||
expect(validateCareEvent({ ...careEvent, act }), act).toMatchObject({ ok: true });
|
||||
}
|
||||
});
|
||||
|
||||
it('rejects the acts §6.2 used to list by mistake', () => {
|
||||
for (const act of ['wind', 'light']) {
|
||||
expect(validateCareEvent({ ...careEvent, act }).ok, act).toBe(false);
|
||||
}
|
||||
});
|
||||
|
||||
it('accepts a null messageId', () => {
|
||||
expect(validateCareEvent({ ...careEvent, messageId: null })).toMatchObject({ ok: true });
|
||||
});
|
||||
|
||||
it('rejects free text in messageId (CLAUDE.md §5.6 rule 1)', () => {
|
||||
expect(validateCareEvent({ ...careEvent, messageId: 'meet me at the station' }).ok).toBe(false);
|
||||
});
|
||||
|
||||
it('rejects a missing localDay', () => {
|
||||
const { localDay: _day, ...rest } = careEvent;
|
||||
expect(validateCareEvent(rest).ok).toBe(false);
|
||||
});
|
||||
|
||||
it('rejects any extra field, so nothing rides along unseen', () => {
|
||||
expect(validateCareEvent({ ...careEvent, note: 'hello' }).ok).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('trace', () => {
|
||||
it('accepts a day-bucketed trace', () => {
|
||||
expect(validateTrace(trace)).toMatchObject({ ok: true });
|
||||
});
|
||||
|
||||
it('rejects a day bucket finer than a day', () => {
|
||||
expect(validateTrace({ ...trace, dayBucket: '2026-10-24T18:42' }).ok).toBe(false);
|
||||
});
|
||||
|
||||
it('rejects a timestamp field', () => {
|
||||
expect(validateTrace({ ...trace, ts: 1761500000000 }).ok).toBe(false);
|
||||
});
|
||||
|
||||
it('rejects a precise coordinate in place of a cell', () => {
|
||||
expect(validateTrace({ ...trace, geocell: '50.0755,14.4378' }).ok).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"types": ["node"]
|
||||
},
|
||||
"include": ["src/**/*.ts", "test/**/*.ts"]
|
||||
}
|
||||
Reference in New Issue
Block a user