Files
TREK/client/src/components/Plugins/settingsForm.test.ts
T
jubnl 1e6ab069cd Fix/sdk default (#2199)
* feat(plugin-sdk): settings-field default + unknown-attribute warning

* feat(shared): default on the plugin settings-field contract

* feat(server): persist and serve plugin settings-field defaults

* feat(client): pre-fill plugin settings defaults in the admin and user forms

* feat(server): enforce required plugin settings on both save paths

* feat(client): block plugin-settings save on an empty required field

* feat(server): resolve plugin settings defaults at runtime

A settings-field `default` was stored and served to the form but never
reached the plugin: the child's init envelope, `ctx.settings.get()`, the
channel dispatch's readAll and the OAuth provider config all read the
stored blob only. Fold `stored ?? default` in on every path that hands
config to a plugin (settings-defaults.ts), and let a default satisfy
`required` on both save gates — refusing a save the runtime would have
accepted was a contradiction.

Also: one `isFilled` rule shared by the save gate and `hasRequired`
(checkbox exempt, whitespace empty); the install parser drops a
non-boolean checkbox default or a select default outside its options,
matching what the SDK already refuses; export SETTING_FIELD_KEYS so the
SDK's mirror can be parity-tested.

* feat(client): share the plugin settings form rules across all three forms

The mobile user settings screen was a third consumer of the fields/config
contract that never got `default` pre-fill or the required check, and
the seed/patch loops lived in five hand copies. Extract
seedSettingsValues / findMissingRequired / settingsPatch into one module
and use it from the admin instance form, the desktop user tab and the
mobile screen. The user forms now surface a 4xx `{ error }` from the
server (the required-field refusal) instead of the generic toast.

* feat(plugin-sdk): seed dev fixtures from manifest defaults

`trek-plugin dev` handed the plugin only the fixture values, so a plugin
relying on its manifest defaults read undefined in dev and only in dev.
Seed ctx.config / ctx.settings from the declared defaults (a fixture
value still wins) via a new exported settingDefaults(manifest, scope).
SETTING_FIELD_KEYS is now parity-tested against the host's list.

* docs(plugins): document settings defaults as runtime fallbacks

A `default` is the field's effective value wherever nobody set one, not
just a form pre-fill; `required` is enforced on both ends; unknown
attributes are dropped at install and warned on by validate. Add a
Settings section with the attribute table to the SDK README and update
the ctx type comments, the shared schema, and the wiki guide.

* add missing exports
2026-09-02 18:42:16 +02:00

59 lines
2.9 KiB
TypeScript

/**
* The one set of rules the three plugin-settings forms (admin instance form, desktop user
* tab, mobile user screen) share: how a form is seeded from stored values + manifest
* defaults, which required fields block Save, and what a save patch carries.
*/
import { describe, expect, it } from 'vitest';
import type { PluginUserSettingField } from '../../api/client';
import { findMissingRequired, SECRET_MASK, seedSettingsValues, settingsPatch } from './settingsForm';
const f = (over: Partial<PluginUserSettingField> & { key: string }): PluginUserSettingField =>
({ label: over.key, input_type: 'text', required: false, secret: false, ...over }) as PluginUserSettingField;
describe('seedSettingsValues', () => {
it('seeds a declared default when nothing is stored, and a stored value wins', () => {
const fields = [
f({ key: 'url', default: 'https://d.example' }),
f({ key: 'retries', input_type: 'number', default: 3 }),
];
expect(seedSettingsValues(fields, {})).toEqual({ url: 'https://d.example', retries: '3' });
expect(seedSettingsValues(fields, { url: 'https://mine.example' })).toEqual({
url: 'https://mine.example',
retries: '3',
});
});
it('coerces a checkbox to a boolean (default or stored) and everything else to a string', () => {
const fields = [
f({ key: 'on', input_type: 'checkbox', default: true }),
f({ key: 'off', input_type: 'checkbox' }),
f({ key: 'n' }),
];
expect(seedSettingsValues(fields, {})).toEqual({ on: true, off: false, n: '' });
expect(seedSettingsValues(fields, { on: false, n: 5 })).toEqual({ on: false, off: false, n: '5' });
});
});
describe('findMissingRequired', () => {
it('names the first required non-checkbox field that is blank (whitespace counts as blank)', () => {
const fields = [f({ key: 'a', required: true }), f({ key: 'b', label: 'Bee', required: true })];
expect(findMissingRequired(fields, { a: 'x', b: ' ' })?.key).toBe('b');
expect(findMissingRequired(fields, { a: 'x', b: 'y' })).toBeUndefined();
});
it('exempts a required checkbox and treats an untouched secret (the mask) as filled', () => {
const fields = [
f({ key: 'consent', input_type: 'checkbox', required: true }),
f({ key: 'tok', secret: true, required: true }),
];
expect(findMissingRequired(fields, { consent: false, tok: SECRET_MASK })).toBeUndefined();
expect(findMissingRequired(fields, { consent: false, tok: '' })?.key).toBe('tok');
});
});
describe('settingsPatch', () => {
it('sends every field except an untouched secret still showing the mask', () => {
const fields = [f({ key: 'tok', secret: true }), f({ key: 'url' }), f({ key: 'on', input_type: 'checkbox' })];
expect(settingsPatch(fields, { tok: SECRET_MASK, url: 'u', on: true })).toEqual({ url: 'u', on: true });
expect(settingsPatch(fields, { tok: 'new', url: 'u', on: false })).toEqual({ tok: 'new', url: 'u', on: false });
});
});