mirror of
https://github.com/Komediruzecki/token-circles.git
synced 2026-10-20 09:46:43 +02:00
Profiles, Settings and the Import page move onto the form kit from #602: a refused entry is said under the field that is wrong, in the same words in cloud and local-first. Each runtime now runs one set of rules from shared/ for a profile, a setting, a connected source, a learned category mapping, an uploaded file and an export. - A profile name that is blank, over 100 characters, or another profile's in other capitals is refused at name. Any profile but the last can be deleted, and the Danger Zone lists the page's profiles under their current names. - PUT /api/settings refuses a key another route owns. The base currency select is labelled and says why at the select. - Both runtimes read an uploaded file with shared/importUpload.ts: an upload is checked to be a complete file before it is read, every cell is text, and a file with no header row is refused at file. Local-first reads the file off the page's thread, with a time limit, and imports an uploaded file again. The email importer, /api/v1/import and MCP's upload use the same reader. - A backup carries each profile's connected sources and only its own settings. A restore files each retirement plan and badge record under its profile, from either runtime's file, and restores profile names that differ only in case, the later one numbered. - Clearing a profile deletes its connected sources on the Worker, and deleting an account runs the same deletes. - Applying category mappings updates the linked goals. The Worker's full backup is indented when Settings asks. The Worker's reseed-demo answers 410. - The contract suite settles 12 pins (43 to 31). Slice 4b of 6 in docs/plans/2026-10-07-form-errors.md.
94 lines
3.6 KiB
TypeScript
94 lines
3.6 KiB
TypeScript
import { SETTINGS_MESSAGES } from '../../settingsSchema';
|
|
import { expectOk, scenario } from '../types';
|
|
import type { ContractApi, Expect, Json } from '../types';
|
|
|
|
async function settingsOf(api: ContractApi, expect: Expect): Promise<Json> {
|
|
const reply = await api.get('/api/settings');
|
|
expectOk(expect, reply, 'GET /api/settings');
|
|
return reply.body;
|
|
}
|
|
|
|
/** The record the achievements store saves (core/achievements/records.ts, serializeRecords). */
|
|
const ACHIEVEMENTS = JSON.stringify({
|
|
v: 1,
|
|
unlocks: [{ id: 'first-budget', earnedOn: '2026-03-01', unlockedAt: '2026-03-01T09:30:00.000Z' }],
|
|
dismissedAdvice: [],
|
|
});
|
|
|
|
export const settings = [
|
|
scenario('settings are saved and read back', async (api, expect) => {
|
|
// What the app reads back: the base currency, the onboarding state and the badges earned.
|
|
const fresh = await settingsOf(api, expect);
|
|
expect(fresh.currency).toBe('EUR');
|
|
expect(fresh.onboarding).toBeUndefined();
|
|
|
|
// Each write sends only its own keys (Settings, onboardingStore, achievementsStore).
|
|
expectOk(expect, await api.put('/api/settings', { currency: 'USD' }), 'PUT the currency');
|
|
expectOk(
|
|
expect,
|
|
await api.put('/api/settings', { onboarding: 'completed' }),
|
|
'PUT the onboarding state'
|
|
);
|
|
expectOk(
|
|
expect,
|
|
await api.put('/api/settings', { achievements: ACHIEVEMENTS }),
|
|
'PUT the badges'
|
|
);
|
|
expect(await settingsOf(api, expect)).toMatchObject({
|
|
currency: 'USD',
|
|
onboarding: 'completed',
|
|
achievements: ACHIEVEMENTS,
|
|
});
|
|
|
|
// A later write changes what it sends, and nothing else.
|
|
expectOk(
|
|
expect,
|
|
await api.put('/api/settings', { onboarding: 'skipped' }),
|
|
'PUT the onboarding state again'
|
|
);
|
|
expect(await settingsOf(api, expect)).toMatchObject({
|
|
currency: 'USD',
|
|
onboarding: 'skipped',
|
|
achievements: ACHIEVEMENTS,
|
|
});
|
|
}),
|
|
|
|
scenario("one profile's settings, seen from another", async (api, expect) => {
|
|
expectOk(
|
|
expect,
|
|
await api.put('/api/settings', { currency: 'USD', onboarding: 'completed' }),
|
|
'PUT the settings'
|
|
);
|
|
const theirs = await settingsOf(api.other, expect);
|
|
// DIFFERENCE settings-scope
|
|
if (api.runtime === 'worker') {
|
|
expect(theirs.currency).toBe('EUR');
|
|
expect(theirs.onboarding).toBeUndefined();
|
|
} else {
|
|
expect(theirs).toMatchObject({ currency: 'USD', onboarding: 'completed' });
|
|
}
|
|
}),
|
|
|
|
scenario('the storage mode is read, and a switch acknowledged', async (api, expect) => {
|
|
const read = await api.get('/api/storage-mode');
|
|
expectOk(expect, read, 'GET /api/storage-mode');
|
|
// Each runtime names itself: the Worker is the cloud, local-first this browser.
|
|
expect(read.body).toEqual({ mode: api.runtime === 'worker' ? 'self-hosted' : 'serverless' });
|
|
// Settings' switch without moving data sends the mode it switches to, then sets it in the
|
|
// browser itself and reloads. The answer acknowledges the mode and switches nothing. Nothing in
|
|
// the app sends the Express server's name for it, which answers the same.
|
|
for (const path of ['/api/storage-mode', '/api/settings/set-storage']) {
|
|
const switched = await api.post(path, { mode: 'self-hosted' });
|
|
expectOk(expect, switched, `POST ${path}`);
|
|
expect(switched.body).toEqual({ ok: true, mode: 'self-hosted' });
|
|
const refused = await api.post(path, { mode: 'cloud' });
|
|
expect(refused.status).toBe(400);
|
|
expect(refused.body).toEqual({
|
|
error: SETTINGS_MESSAGES.mode,
|
|
fields: { mode: SETTINGS_MESSAGES.mode },
|
|
});
|
|
}
|
|
expect((await api.get('/api/storage-mode')).body).toEqual(read.body);
|
|
}),
|
|
];
|