ShtukbaseStandard
package file→ .shtukbase/packages/automations/checks/t02_automation.ts

checks/t02_automation.ts

77 lines, 3.6 KB.

  1. // ACT-R02. An automation says when, what and how often: a trigger of a known kind with what that kind needs, a
  2. // reaction that leads to an existing action or agent page, a mode, and for `mode: auto` explicit limits.
  3. import { main } from '../../collections/checks/lib/runner.ts';
  4. import { type Project, type Rec, type Result, addr, count, filled, map, pageExists, recordsOf, target, text } from '../../agents/lib/factlib.ts';
  5. export const RULE = {
  6. id: 'ACT-R02', title: 'An automation says when, what and how many times',
  7. scope: 'all automations',
  8. };
  9. /** Trigger kind → the keys it needs (one of them when the list has `|`). */
  10. const NEEDS: Record<string, string[][]> = {
  11. clock: [['at', 'every', 'cron']], schedule: [['at', 'every', 'cron']],
  12. change: [['within']], phase: [['contour'], ['phase']], sensor: [['sensor', 'watch']],
  13. 'run-finished': [['automation']], run: [['automation']],
  14. manual: [], hand: [], answer: [],
  15. };
  16. function trigger(r: Rec, res: Result, at: string): void {
  17. const t = map(r.fm.trigger);
  18. if (!t) {
  19. res.hole(at, 'no event (trigger)', 'write trigger: {kind: clock, at: "03:00"} or another kind', 'no-trigger');
  20. return;
  21. }
  22. const kind = text(t.kind);
  23. if (!(kind in NEEDS)) {
  24. res.hole(at, `event kind "${kind || '—'}" is unknown`, `set kind: ${Object.keys(NEEDS).join(' | ')}`, 'bad-trigger');
  25. return;
  26. }
  27. for (const any of NEEDS[kind])
  28. if (!any.some((k) => filled(t[k])))
  29. res.hole(at, `event ${kind} without ${any.join(' or ')}`, `add ${any.join(' or ')} to trigger`, 'bad-trigger');
  30. }
  31. function reaction(p: Project, r: Rec, res: Result, at: string): void {
  32. const re = map(r.fm.reaction);
  33. const ref = re ? (filled(re.action) ? re.action : re.agent) : null;
  34. if (!filled(ref)) {
  35. res.hole(at, 'no response (reaction.action or reaction.agent)', 'write reaction: {action: /actions/<name>}', 'no-reaction');
  36. return;
  37. }
  38. const t = target(r, ref);
  39. if (!t || !pageExists(p, t)) res.hole(at, `the response points to ${t ?? text(ref)}, but there is no such page`, 'fix the link or create the action page', 'missing-action');
  40. }
  41. function limits(r: Rec, res: Result, at: string): void {
  42. const mode = text(r.fm.mode);
  43. if (mode !== 'manual' && mode !== 'auto') {
  44. res.hole(at, `mode "${mode || '—'}"`, 'set mode: manual or auto', 'bad-mode');
  45. return;
  46. }
  47. const l = map(r.fm.limits);
  48. const hour = l ? count(l.perHour) : null;
  49. const day = l ? count(l.perDay) : null;
  50. if (l) for (const k of ['perHour', 'perDay'])
  51. if (filled(l[k]) && count(l[k]) === null) res.hole(at, `limits.${k}: "${text(l[k])}" is not a whole number above zero`, 'set a whole number', 'bad-limit');
  52. if (mode === 'auto' && hour === null && day === null)
  53. res.hole(at, 'the automation runs by itself, but there is no limit', 'write limits: {perHour: N, perDay: M}', 'no-limit');
  54. if (hour !== null && day !== null && hour > day)
  55. res.hole(at, `per hour (${hour}) is more than per day (${day})`, 'fix the limits', 'bad-limit');
  56. }
  57. export function check(p: Project, res: Result): void {
  58. const recs = recordsOf(p, 'AUTOMATION');
  59. res.scope = recs.map((r) => addr(r.id));
  60. res.scopeText = `automations ${p.coll('AUTOMATION') || '—'} (${recs.length})`;
  61. if (!res.part('automations', p.coll('AUTOMATION') ? null : 'no automations collection')) return;
  62. for (const r of recs) {
  63. const at = addr(r.id);
  64. if (text(r.fm.type) !== 'Automation') res.hole(at, `type "${text(r.fm.type) || '—'}", not Automation`, 'set type: Automation', 'bad-type');
  65. trigger(r, res, at);
  66. reaction(p, r, res, at);
  67. limits(r, res, at);
  68. }
  69. }
  70. if (import.meta.main) main({ RULE, check });