Files
TREK/plugin-sdk/src/cli/checks
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
..
2026-07-18 22:13:52 +02:00
2026-09-02 18:42:16 +02:00
2026-07-18 22:13:52 +02:00

/**
 * The README quality gate, as pure functions of the markdown.
 *
 * These are a LINE-FOR-LINE port of TREK-Plugins' scripts/check-readme.mjs. They live apart
 * from the checks that call them because the same gate runs twice, against two different
 * READMEs, and both must agree with CI:
 *
 *   - offline, against README.md in the WORKING TREE  (`status` / `validate`)
 *   - over the network, against README.md at the PINNED COMMIT  (`preflight`)
 *
 * The two differ more often than you would think — an author fills the README in and forgets
 * to commit it, and the tree is green while the tag CI grades is not. Keeping one implementation
 * means the offline pass can never be more lenient than the network one; only more current.
 *
 * If you change a rule here, change scripts/check-readme.mjs to match, or you have built a
 * false green. test/checks-parity.test.ts asserts the two agree.
 */

/** Every section CI demands, matched case-insensitively as a substring of a heading. */
export const REQUIRED_SECTIONS = ['What it does', 'Screenshots', 'Permissions', 'Setup'];

/** Template prose that means the author never edited the scaffold. */
export const PLACEHOLDER_PATTERNS = [
  /\{\{[^}]*\}\}/,
  /\bREPLACE_ME\b/i,
  /\bDescribe (what|the)\b/i,
  /\byour-name\/trek-plugin/i,
];

export const MIN_PROSE_CHARS = 400;

/**
 * The heading body, with the trailing HTML comment authors use to hide anchors cut off.
 *
 * Cut with indexOf rather than a regex on purpose. The obvious spellings — the
 * `(.+?)\s*(?:<!--.*)?$` this used to be, or a `replace(/\s*<!--.*$/, '')` in front of it —
 * put two quantifiers on the same run of spaces, and both then backtrack quadratically:
 * a heading line padded with 80k spaces takes ~12s, 200k takes ~30s. This gate runs over
 * READMEs anyone can submit to the registry, so that is a stall someone can post. Same
 * failure the wiki sidebar parser was rewritten for (server/src/nest/help/wiki.ts).
 */
function headingBody(line: string): string {
  const comment = line.indexOf('<!--');
  return comment < 0 ? line : line.slice(0, comment);
}

/** Which of REQUIRED_SECTIONS have no matching heading. */
export function missingSections(md: string): string[] {
  const headings = md
    .split('\n')
    // `\S.*\S|\S` instead of a lazy body: the edges are pinned to non-space, so nothing
    // competes with the trailing `\s*`.
    .map((line) => /^#{1,6}\s+(\S.*\S|\S)\s*$/.exec(headingBody(line))?.[1].toLowerCase())
    .filter((h): h is string => h !== undefined);
  return REQUIRED_SECTIONS.filter((want) => !headings.some((got) => got.includes(want.toLowerCase())));
}

/** The literal placeholder strings still present, e.g. ['Describe what']. */
export function placeholders(md: string): string[] {
  const hits: string[] = [];
  for (const re of PLACEHOLDER_PATTERNS) {
    const hit = md.match(re);
    if (hit) hits.push(hit[0]);
  }
  return hits;
}

/**
 * The length of the author's actual WRITING, with everything that isn't prose stripped:
 * comments, code fences, images, links, headings, table rows, then markdown punctuation.
 *
 * This is why a scaffold can't game the 400-char floor with a big permissions table — the
 * table is stripped. It has to be sentences.
 */
export function proseLength(md: string): number {
  return md
    .replace(/<!--[\s\S]*?-->/g, '')
    .replace(/```[\s\S]*?```/g, '')
    .replace(/!\[[^\]]*\]\([^)]*\)/g, '')
    .replace(/\[[^\]]*\]\([^)]*\)/g, '')
    .replace(/^#{1,6}\s+.*$/gm, '')
    .replace(/^\s*\|.*$/gm, '')
    .replace(/[#>*_`|-]/g, '')
    .replace(/\s+/g, ' ')
    .trim().length;
}

/** Manifest permissions that are never named anywhere in the README. CI rejects these. */
export function undocumentedPermissions(md: string, permissions: string[]): string[] {
  const lower = md.toLowerCase();
  return permissions.filter((p) => !lower.includes(p.toLowerCase()));
}