@pikku/core 0.12.67 → 0.12.70

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/CHANGELOG.md +397 -0
  2. package/README.md +34 -2
  3. package/dist/function/functions.types.d.ts +27 -0
  4. package/dist/index.d.ts +1 -1
  5. package/dist/internal.d.ts +1 -1
  6. package/dist/internal.js +1 -1
  7. package/dist/pikku-state.js +1 -0
  8. package/dist/services/http-scenario-actors.d.ts +12 -4
  9. package/dist/services/http-scenario-actors.js +47 -45
  10. package/dist/services/in-memory-workflow-service.d.ts +2 -2
  11. package/dist/services/in-memory-workflow-service.js +2 -2
  12. package/dist/services/index.d.ts +2 -1
  13. package/dist/services/index.js +1 -0
  14. package/dist/services/meta-service.d.ts +5 -1
  15. package/dist/services/meta-service.js +44 -18
  16. package/dist/services/scenario-actors-service.d.ts +108 -2
  17. package/dist/services/scenario-actors-service.js +40 -1
  18. package/dist/types/core.types.d.ts +21 -3
  19. package/dist/types/state.types.d.ts +3 -1
  20. package/dist/wirings/actor-flow/actor-flow.types.d.ts +1 -1
  21. package/dist/wirings/actor-flow/index.d.ts +1 -1
  22. package/dist/wirings/actor-flow/run-conversation.d.ts +10 -10
  23. package/dist/wirings/actor-flow/run-conversation.js +27 -27
  24. package/dist/wirings/ai-agent/ai-agent-prepare.d.ts +18 -1
  25. package/dist/wirings/ai-agent/ai-agent-prepare.js +26 -4
  26. package/dist/wirings/cli/command-parser.js +11 -1
  27. package/dist/wirings/queue/index.d.ts +1 -1
  28. package/dist/wirings/queue/queue.types.d.ts +30 -0
  29. package/dist/wirings/rpc/rpc-runner.js +1 -1
  30. package/dist/wirings/workflow/dsl/workflow-dsl.types.d.ts +52 -3
  31. package/dist/wirings/workflow/feature.d.ts +28 -0
  32. package/dist/wirings/workflow/feature.js +57 -0
  33. package/dist/wirings/workflow/index.d.ts +13 -2
  34. package/dist/wirings/workflow/index.js +15 -0
  35. package/dist/wirings/workflow/pikku-scenario-service.d.ts +121 -0
  36. package/dist/wirings/workflow/pikku-scenario-service.js +419 -0
  37. package/dist/wirings/workflow/pikku-workflow-service.d.ts +118 -12
  38. package/dist/wirings/workflow/pikku-workflow-service.js +166 -153
  39. package/dist/wirings/workflow/scenario-cookie-jar.d.ts +29 -0
  40. package/dist/wirings/workflow/scenario-cookie-jar.js +51 -0
  41. package/dist/wirings/workflow/scenario-poll.d.ts +20 -0
  42. package/dist/wirings/workflow/scenario-poll.js +25 -0
  43. package/dist/wirings/workflow/scenario-prose.d.ts +38 -0
  44. package/dist/wirings/workflow/scenario-prose.js +45 -0
  45. package/dist/wirings/workflow/scenario-step-guards.d.ts +16 -0
  46. package/dist/wirings/workflow/scenario-step-guards.js +29 -0
  47. package/dist/wirings/workflow/scenario-step.types.d.ts +148 -0
  48. package/dist/wirings/workflow/scenario-step.types.js +1 -0
  49. package/dist/wirings/workflow/workflow.types.d.ts +119 -2
  50. package/package.json +3 -1
  51. package/src/function/functions.types.ts +32 -0
  52. package/src/index.ts +1 -0
  53. package/src/internal.ts +5 -1
  54. package/src/pikku-state.ts +1 -0
  55. package/src/services/http-scenario-actors.test.ts +85 -1
  56. package/src/services/http-scenario-actors.ts +65 -51
  57. package/src/services/in-memory-workflow-service.test.ts +50 -1
  58. package/src/services/in-memory-workflow-service.ts +3 -2
  59. package/src/services/index.ts +5 -0
  60. package/src/services/meta-service.test.ts +79 -0
  61. package/src/services/meta-service.ts +61 -26
  62. package/src/services/scenario-actors-service.ts +157 -2
  63. package/src/types/core.types.ts +27 -2
  64. package/src/types/state.types.ts +3 -0
  65. package/src/wirings/actor-flow/actor-flow.types.ts +1 -1
  66. package/src/wirings/actor-flow/index.ts +1 -1
  67. package/src/wirings/actor-flow/run-conversation.test.ts +12 -6
  68. package/src/wirings/actor-flow/run-conversation.ts +36 -41
  69. package/src/wirings/ai-agent/ai-agent-prepare.test.ts +29 -0
  70. package/src/wirings/ai-agent/ai-agent-prepare.ts +38 -4
  71. package/src/wirings/cli/command-parser.test.ts +60 -0
  72. package/src/wirings/cli/command-parser.ts +12 -1
  73. package/src/wirings/queue/index.ts +2 -0
  74. package/src/wirings/queue/queue.types.ts +32 -0
  75. package/src/wirings/rpc/rpc-runner.test.ts +28 -5
  76. package/src/wirings/rpc/rpc-runner.ts +1 -1
  77. package/src/wirings/workflow/dsl/workflow-dsl.types.ts +86 -2
  78. package/src/wirings/workflow/feature.test.ts +131 -0
  79. package/src/wirings/workflow/feature.ts +78 -0
  80. package/src/wirings/workflow/index.ts +74 -0
  81. package/src/wirings/workflow/pikku-scenario-service.ts +682 -0
  82. package/src/wirings/workflow/pikku-workflow-service.test.ts +126 -0
  83. package/src/wirings/workflow/pikku-workflow-service.ts +306 -228
  84. package/src/wirings/workflow/scenario-cookie-jar.test.ts +108 -0
  85. package/src/wirings/workflow/scenario-cookie-jar.ts +65 -0
  86. package/src/wirings/workflow/scenario-hooks.test.ts +212 -0
  87. package/src/wirings/workflow/scenario-poll.test.ts +66 -0
  88. package/src/wirings/workflow/scenario-poll.ts +36 -0
  89. package/src/wirings/workflow/scenario-prose.test.ts +152 -0
  90. package/src/wirings/workflow/scenario-prose.ts +79 -0
  91. package/src/wirings/workflow/scenario-service.test.ts +155 -0
  92. package/src/wirings/workflow/scenario-step-guards.ts +43 -0
  93. package/src/wirings/workflow/scenario-step.test.ts +441 -8
  94. package/src/wirings/workflow/scenario-step.types.ts +157 -0
  95. package/src/wirings/workflow/workflow.types.ts +137 -1
  96. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,38 @@
1
+ import type { ScenarioStepPhase } from './scenario-step.types.js';
2
+ /**
3
+ * Renders the English sentence a reporter shows for a scenario step.
4
+ *
5
+ * This is the inversion of cucumber: rather than parsing English into a call,
6
+ * we render English out of a typed call, so the readable report survives
7
+ * without a regex registry paying for it.
8
+ *
9
+ * Lives in core so the CLI reporter and the console render identically.
10
+ */
11
+ /**
12
+ * Fill a step's `template` from the input that step was actually called with,
13
+ * so the reported sentence names the values under test — "sees @pikku/addon-todos"
14
+ * rather than "sees an addon in the gallery" repeated three times.
15
+ *
16
+ * A placeholder with no recorded value renders as nothing and the surrounding
17
+ * whitespace collapses, so an optional input that was omitted reads as a shorter
18
+ * sentence rather than a literal `{state}` leaking into the report.
19
+ */
20
+ export declare const renderStepTemplate: (template: string, input: unknown) => string;
21
+ export declare const composeStepProse: ({ phase, description, template, input, actor, keywordWidth, }: {
22
+ phase: ScenarioStepPhase;
23
+ description: string;
24
+ /**
25
+ * The prose this step renders, with `{placeholders}` filled from `input`.
26
+ * Unlike `description`, which documents what the step does, this is what a
27
+ * reader of the report sees. Falls back to `description` when absent.
28
+ */
29
+ template?: string;
30
+ /** The input this step was called with, as recorded on the run. */
31
+ input?: unknown;
32
+ actor?: string;
33
+ /**
34
+ * Pad the keyword to this width so a ladder of steps lines its sentences up
35
+ * under each other. Omit for inline prose.
36
+ */
37
+ keywordWidth?: number;
38
+ }) => string;
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Renders the English sentence a reporter shows for a scenario step.
3
+ *
4
+ * This is the inversion of cucumber: rather than parsing English into a call,
5
+ * we render English out of a typed call, so the readable report survives
6
+ * without a regex registry paying for it.
7
+ *
8
+ * Lives in core so the CLI reporter and the console render identically.
9
+ */
10
+ /**
11
+ * Fill a step's `template` from the input that step was actually called with,
12
+ * so the reported sentence names the values under test — "sees @pikku/addon-todos"
13
+ * rather than "sees an addon in the gallery" repeated three times.
14
+ *
15
+ * A placeholder with no recorded value renders as nothing and the surrounding
16
+ * whitespace collapses, so an optional input that was omitted reads as a shorter
17
+ * sentence rather than a literal `{state}` leaking into the report.
18
+ */
19
+ export const renderStepTemplate = (template, input) => {
20
+ const values = input && typeof input === 'object' ? input : {};
21
+ return template
22
+ .replace(/\{(\w+)\}/g, (_match, key) => formatValue(values[key]))
23
+ .replace(/\s+/g, ' ')
24
+ .trim();
25
+ };
26
+ const formatValue = (value) => {
27
+ if (value === undefined || value === null) {
28
+ return '';
29
+ }
30
+ if (typeof value === 'object') {
31
+ return JSON.stringify(value);
32
+ }
33
+ return String(value);
34
+ };
35
+ export const composeStepProse = ({ phase, description, template, input, actor, keywordWidth, }) => {
36
+ const keyword = phase === 'step' ? '' : capitalise(phase);
37
+ const subject = actor ? `the ${actor}` : '';
38
+ const rendered = template ? renderStepTemplate(template, input) : description;
39
+ const sentence = [subject, rendered].filter(Boolean).join(' ');
40
+ if (keywordWidth === undefined) {
41
+ return [keyword, sentence].filter(Boolean).join(' ');
42
+ }
43
+ return `${keyword.padEnd(keywordWidth)} ${sentence}`;
44
+ };
45
+ const capitalise = (value) => value.charAt(0).toUpperCase() + value.slice(1);
@@ -0,0 +1,16 @@
1
+ import type { PikkuScenarioStepWire, ScenarioEnvironment } from './scenario-step.types.js';
2
+ /**
3
+ * The actor this step was called with, or a loud error naming the step.
4
+ *
5
+ * A step that talks to the target app needs an identity, but `actor` is
6
+ * optional on the wire because a pure assertion step needs none. This is the
7
+ * one place that narrowing happens, so every step file stops writing its own
8
+ * `actorOf(...)` guard.
9
+ */
10
+ export declare const requireActor: <TActor>(scenarioStep: PikkuScenarioStepWire<TActor> | undefined) => TActor;
11
+ /**
12
+ * The environment this run targets, or a loud error naming the step. A run
13
+ * started outside `pikku scenario run` only carries one when the server has
14
+ * `API_URL` configured.
15
+ */
16
+ export declare const requireScenarioEnv: (scenarioStep: PikkuScenarioStepWire<unknown> | undefined) => ScenarioEnvironment;
@@ -0,0 +1,29 @@
1
+ /**
2
+ * The actor this step was called with, or a loud error naming the step.
3
+ *
4
+ * A step that talks to the target app needs an identity, but `actor` is
5
+ * optional on the wire because a pure assertion step needs none. This is the
6
+ * one place that narrowing happens, so every step file stops writing its own
7
+ * `actorOf(...)` guard.
8
+ */
9
+ export const requireActor = (scenarioStep) => {
10
+ const actor = scenarioStep?.actor;
11
+ if (!actor) {
12
+ throw new Error(`[scenario] step '${scenarioStep?.name ?? 'unknown'}' was called without an actor. ` +
13
+ `Pass { actor: actors.<name> } so it runs as that persona.`);
14
+ }
15
+ return actor;
16
+ };
17
+ /**
18
+ * The environment this run targets, or a loud error naming the step. A run
19
+ * started outside `pikku scenario run` only carries one when the server has
20
+ * `API_URL` configured.
21
+ */
22
+ export const requireScenarioEnv = (scenarioStep) => {
23
+ const env = scenarioStep?.env;
24
+ if (!env) {
25
+ throw new Error(`[scenario] step '${scenarioStep?.name ?? 'unknown'}' needs the target environment, but this run carries none. ` +
26
+ `Run it through 'pikku scenario run <environment>' with scenarios.environments.<environment> declared in pikku.config.json.`);
27
+ }
28
+ return env;
29
+ };
@@ -0,0 +1,148 @@
1
+ import type { ScenarioActor } from '../../services/scenario-actors-service.js';
2
+ /**
3
+ * Scenario steps: named, typed units of scenario behaviour.
4
+ *
5
+ * A step's body is an ordinary pikku function, so it may drive a browser, call
6
+ * an RPC as its actor, or run a workflow. `given`/`when`/`then` are sugar over
7
+ * `step` — they only change the prose the reporter renders.
8
+ */
9
+ /**
10
+ * Which Gherkin-style keyword the reporter prefixes this step with. `step`
11
+ * renders no prefix at all.
12
+ */
13
+ export type ScenarioStepPhase = 'step' | 'given' | 'when' | 'then';
14
+ /**
15
+ * Options accepted by `scenario.step/given/when/then`.
16
+ *
17
+ * Note the retry default differs from an ordinary workflow step: retrying a
18
+ * failed assertion is the wrong behaviour for a test primitive, so steps
19
+ * default to no retries.
20
+ */
21
+ export interface ScenarioStepOptions {
22
+ /** The actor this step runs as. Required for steps declaring `browser: true`. */
23
+ actor?: unknown;
24
+ /** Overrides the step's own `description` for this call site only. */
25
+ description?: string;
26
+ /** Defaults to 0 for steps — a failed assertion must not be retried. */
27
+ retries?: number;
28
+ retryDelay?: number | string;
29
+ }
30
+ /**
31
+ * The environment a scenario run targets, as declared in pikku.config.json
32
+ * under `scenarios.environments`.
33
+ */
34
+ export interface ScenarioEnvironment {
35
+ /** Base API URL of the target app, INCLUDING the HTTP prefix. */
36
+ apiUrl: string;
37
+ /** Base URL of the app's UI, for environments with browser steps. */
38
+ appUrl?: string;
39
+ }
40
+ /**
41
+ * The `scenarioStep` wire, present on every scenario step invocation.
42
+ *
43
+ * `TActor` is the project's own actor type, so a step reaches only the RPCs its
44
+ * actors can actually call. It defaults to the open `ScenarioActor` for a
45
+ * project that declares no registry.
46
+ */
47
+ export interface PikkuScenarioStepWire<TActor = ScenarioActor> {
48
+ /** Registered step name (also its pikkuFuncId) */
49
+ name: string;
50
+ /** Durable key within the run; may carry an `#ordinal` suffix when repeated */
51
+ stepName: string;
52
+ runId: string;
53
+ phase: ScenarioStepPhase;
54
+ /**
55
+ * The actor this step runs as, when one was given. Call RPCs through it
56
+ * (`actor.invoke(...)`) so they run against the target environment as that
57
+ * persona.
58
+ */
59
+ actor?: TActor;
60
+ /**
61
+ * The environment this run targets. A step runs in the CLI process, where
62
+ * there is no `variables` service — this is how a raw-HTTP step learns the
63
+ * target's URL without reaching for `process.env`.
64
+ */
65
+ env?: ScenarioEnvironment;
66
+ }
67
+ /**
68
+ * How a browser step names an element.
69
+ *
70
+ * A `data-testid` on its own is rarely enough to name exactly one: `where`
71
+ * matches the element's own data attributes (so a step asserts a status
72
+ * without reading translated copy back to the app), `prefix` matches a family
73
+ * of ids, `containing` picks the match holding a piece of text, and `within`
74
+ * scopes the lookup to one row or section.
75
+ *
76
+ * Declared here so a step's input type is structural; the driver
77
+ * (`@pikku/playwright`) is what resolves it against a real page.
78
+ */
79
+ export interface TestIdSelector {
80
+ testId: string;
81
+ /** Match every test id beginning with `testId`, e.g. every `flow-card-*`. */
82
+ prefix?: boolean;
83
+ /** Data attributes the element must also carry, e.g. `{ 'data-open': 'true' }`. */
84
+ where?: Record<string, string>;
85
+ /** Narrow to the one match holding this text. */
86
+ containing?: string;
87
+ /** Scope the lookup to one enclosing element, e.g. the row for one user. */
88
+ within?: TestIdSelector;
89
+ }
90
+ /**
91
+ * Structural browser handle, present only when the runner provisioned a
92
+ * browser for this step (`browser: true` on the step config).
93
+ *
94
+ * `@pikku/core` deliberately never imports playwright — it must stay
95
+ * dependency-free for edge runtimes. `@pikku/playwright` augments this
96
+ * interface via `declare module`, so `wire.browser.page` is a fully typed
97
+ * Playwright `Page` in a project that installs it.
98
+ */
99
+ export interface PikkuBrowserWire {
100
+ /** The actor whose browser context this is */
101
+ readonly actor: string;
102
+ goto(url: string): Promise<void>;
103
+ screenshot(name?: string): Promise<Uint8Array>;
104
+ }
105
+ /**
106
+ * What one actor's window looked like at the moment a scenario failed.
107
+ *
108
+ * A browser step fails with a selector timeout that says nothing about *why*
109
+ * the page never rendered. The answer is almost always in the page's own
110
+ * errors, which the driver has been collecting all along.
111
+ */
112
+ export interface ScenarioBrowserFailure {
113
+ /** The actor whose window this is. */
114
+ actor: string;
115
+ /** Where the window was pointed, when the driver can still report it. */
116
+ url?: string;
117
+ /** Path the screenshot was written to; absent when none could be taken. */
118
+ screenshot?: string;
119
+ consoleErrors: string[];
120
+ pageErrors: string[];
121
+ failedRequests: string[];
122
+ apiErrors: string[];
123
+ }
124
+ /**
125
+ * Supplied by `@pikku/playwright` (or any other driver) and consumed by the
126
+ * scenario runner. Declared here so the CLI depends only on core.
127
+ *
128
+ * `reset` and `captureFailure` are optional so a driver written against an
129
+ * earlier version keeps compiling; the runner treats a driver without them as
130
+ * one that simply offers no isolation and no diagnostics.
131
+ */
132
+ export interface ScenarioBrowserProvider {
133
+ /** Resolve — creating on first use — the browser session for an actor. */
134
+ sessionFor(actorName: string): Promise<PikkuBrowserWire>;
135
+ /**
136
+ * Discard every actor's per-scenario state — cookies, storage, open pages —
137
+ * while keeping the browser itself. Called between scenarios, so one
138
+ * scenario cannot leave the next signed in as somebody else.
139
+ */
140
+ reset?(): Promise<void>;
141
+ /**
142
+ * Snapshot every open window for a failed scenario. `label` identifies the
143
+ * scenario in artifact filenames. Never throws: a failure to capture must
144
+ * not replace the failure being captured.
145
+ */
146
+ captureFailure?(label: string): Promise<ScenarioBrowserFailure[]>;
147
+ close(): Promise<void>;
148
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,7 +1,9 @@
1
1
  import type { SerializedError, CommonWireMeta } from '../../types/core.types.js';
2
- import type { CorePikkuFunctionConfig } from '../../function/functions.types.js';
2
+ import type { CorePikkuFunctionConfig, CorePikkuFunctionHook } from '../../function/functions.types.js';
3
+ import type { GroupConcurrencyConfig } from '../queue/queue.types.js';
3
4
  export type { WorkflowService } from '../../services/workflow-service.js';
4
- export type { WorkflowStepOptions, WorkflowExpectEventuallyOptions, WorkflowExpectErrorOptions, WorkflowExpectServiceOptions, WorkflowWireDoRPC, WorkflowWireDoInline, WorkflowWireSleep, WorkflowWireSuspend, WorkflowWireApproval, WorkflowApprovalOptions, ApprovalOutcome, InputSource, OutputBinding, RpcStepMeta, SimpleCondition, Condition, BranchCase, BranchStepMeta, ParallelGroupStepMeta, FanoutStepMeta, ReturnStepMeta, InlineStepMeta, SleepStepMeta, CancelStepMeta, SuspendStepMeta, ApprovalStepMeta, SetStepMeta, SwitchCaseMeta, SwitchStepMeta, FilterStepMeta, ArrayPredicateStepMeta, WorkflowStepMeta, WorkflowStepWire, PikkuWorkflowWire, PikkuScenarioWire, } from './dsl/workflow-dsl.types.js';
5
+ export type { WorkflowStepOptions, WorkflowExpectEventuallyOptions, WorkflowExpectErrorOptions, WorkflowExpectServiceOptions, WorkflowWireDoRPC, WorkflowWireDoInline, WorkflowWireSleep, WorkflowWireSuspend, WorkflowWireApproval, WorkflowApprovalOptions, ApprovalOutcome, InputSource, OutputBinding, RpcStepMeta, SimpleCondition, Condition, BranchCase, BranchStepMeta, ParallelGroupStepMeta, FanoutStepMeta, ReturnStepMeta, InlineStepMeta, SleepStepMeta, CancelStepMeta, SuspendStepMeta, ApprovalStepMeta, SetStepMeta, SwitchCaseMeta, SwitchStepMeta, FilterStepMeta, ArrayPredicateStepMeta, ScenarioStepInvocation, ScenarioStepMeta, WorkflowStepMeta, WorkflowStepWire, PikkuWorkflowWire, PikkuScenarioWire, } from './dsl/workflow-dsl.types.js';
6
+ export type { ScenarioStepPhase, ScenarioStepOptions, PikkuScenarioStepWire, PikkuBrowserWire, ScenarioBrowserProvider, } from './scenario-step.types.js';
5
7
  import type { WorkflowStepMeta } from './dsl/workflow-dsl.types.js';
6
8
  export interface WorkflowRunWire {
7
9
  type: string;
@@ -18,6 +20,43 @@ export interface WorkflowServiceConfig {
18
20
  stepWorkerQueueName: string;
19
21
  sleeperRPCName: string;
20
22
  }
23
+ /**
24
+ * How a workflow service spreads its jobs across queues.
25
+ *
26
+ * Passed to the service constructor rather than read from `config.workflow`:
27
+ * the queues are wired during construction, before singleton services (and so
28
+ * before `config`) exist.
29
+ */
30
+ export interface WorkflowQueueOptions {
31
+ /**
32
+ * - `'per-workflow'` (default) — each workflow gets its own
33
+ * `wf-orchestrator-*` / `wf-step-*` queue. Complete isolation, and it's
34
+ * what lets serverless providers deploy one unit per workflow. Costs one
35
+ * set of pollers per queue, which adds up on pull-based backends.
36
+ * - `'shared-groups'` — every workflow shares the orchestrator/step-worker
37
+ * queues and stays isolated via {@link queueGroupConcurrency}, so no
38
+ * workflow can occupy more than its share. One set of pollers total.
39
+ * Only for single-process (monolith) runtimes; a per-unit serverless
40
+ * deploy needs the per-workflow queues to route to its units.
41
+ */
42
+ queueStrategy?: 'per-workflow' | 'shared-groups';
43
+ /**
44
+ * Total concurrent workflow jobs per node under `'shared-groups'`.
45
+ * Defaults to 20.
46
+ */
47
+ queueConcurrency?: number;
48
+ /**
49
+ * How many jobs of one workflow may run at once under `'shared-groups'`.
50
+ * Defaults to 2. Tiers are keyed by workflow name, so a specific workflow
51
+ * can be given its own limit without any extra wiring.
52
+ *
53
+ * Note tiers can only lower a workflow's limit, not raise it above
54
+ * `default`: the backend's pre-fetch exclusion is applied per group using
55
+ * `default` alone. To give one workflow more room, raise `default` and
56
+ * restrict the others by tier.
57
+ */
58
+ queueGroupConcurrency?: number | GroupConcurrencyConfig;
59
+ }
21
60
  export interface WorkflowPlannedStep {
22
61
  /** Durable step key — matches the runtime step name stored in the DB */
23
62
  stepName: string;
@@ -197,6 +236,77 @@ export type CoreWorkflow<PikkuFunctionConfig extends CorePikkuFunctionConfig<any
197
236
  /** Tags for organization and filtering */
198
237
  tags?: string[];
199
238
  };
239
+ /**
240
+ * A scenario as a feature references it: either the scenario itself, or the
241
+ * scenario paired with the input to run it with. The paired form is gherkin's
242
+ * `Examples:` — the same scenario run once per row, written as an ordinary
243
+ * loop rather than a table.
244
+ */
245
+ export type CoreFeatureScenario = CorePikkuFunctionConfig<any, any, any> | {
246
+ scenario: CorePikkuFunctionConfig<any, any, any>;
247
+ data: unknown;
248
+ };
249
+ /**
250
+ * A feature: an ordered group of scenarios, mirroring gherkin's Feature ↔
251
+ * Scenario structure. Scenarios are referenced by imported identifier, so a
252
+ * renamed or deleted scenario is a compile error rather than a silent skip.
253
+ *
254
+ * Hooks run **once around the whole group** (`before → a → b → c → after`),
255
+ * not per scenario — per-scenario setup is the scenario's own `before`. That
256
+ * is the one thing a feature deliberately cannot express: gherkin's
257
+ * `Background:` runs per scenario.
258
+ */
259
+ export type CoreFeature = {
260
+ /** Human-readable name. The export identifier is the id. */
261
+ name: string;
262
+ description?: string;
263
+ tags?: string[];
264
+ /** Readonly because `pikkuFeature`'s `const` generic infers a readonly tuple. */
265
+ scenarios: readonly CoreFeatureScenario[];
266
+ before?: CorePikkuFunctionHook;
267
+ after?: CorePikkuFunctionHook;
268
+ };
269
+ /** One entry of a feature's scenario list, as extracted from the source. */
270
+ export type FeatureMetaEntry = {
271
+ /** The scenario's declared export name — its key in `WorkflowsMeta`. */
272
+ scenario: string;
273
+ /** The input this entry runs the scenario with — gherkin's `Examples:`. */
274
+ data?: unknown;
275
+ };
276
+ /**
277
+ * A feature as the console reads it: the document structure around a group of
278
+ * scenarios. Generated to `scenarios/features.gen.json` and read off disk, so
279
+ * nothing app-facing has to import the scenario bootstrap to describe one.
280
+ */
281
+ export type FeatureMeta = {
282
+ /** The export identifier. */
283
+ id: string;
284
+ name: string;
285
+ description?: string;
286
+ tags: string[];
287
+ /** In declared order — a feature's reading order is its declaration order. */
288
+ entries: FeatureMetaEntry[];
289
+ /**
290
+ * Entries that could not be read statically (a spread, a `.map()`). Their
291
+ * membership is only known once the scenario bootstrap has been evaluated,
292
+ * so a non-zero count means this listing is partial.
293
+ */
294
+ unresolvedEntries: number;
295
+ /** Hooks are runtime-only; only their presence is knowable from meta. */
296
+ hasBefore: boolean;
297
+ hasAfter: boolean;
298
+ };
299
+ export type FeaturesMeta = Record<string, FeatureMeta>;
300
+ /** One planned scenario run, resolved from a feature's scenario list. */
301
+ export type FeaturePlanEntry = {
302
+ featureId: string;
303
+ featureName: string;
304
+ scenarioName: string;
305
+ /** The input this entry runs the scenario with, if the feature supplied one. */
306
+ data?: unknown;
307
+ /** The scenario's own tags unioned with the containing feature's. */
308
+ tags: string[];
309
+ };
200
310
  /**
201
311
  * Workflow client interface
202
312
  */
@@ -236,6 +346,13 @@ export type WorkflowsMeta = Record<string, CommonWireMeta & {
236
346
  expose?: boolean;
237
347
  /** True for pikkuScenario workflows (complex + actor steps). */
238
348
  scenario?: boolean;
349
+ /**
350
+ * Why a scenario is held out of a default run. Stating the reason in code
351
+ * keeps the quarantine next to the scenario it applies to, rather than in
352
+ * a CI invocation nobody reads. Naming the scenario with `--flows` runs it
353
+ * regardless.
354
+ */
355
+ skip?: string;
239
356
  /** Actor names a scenario declares (personas it runs steps as). */
240
357
  actors?: string[];
241
358
  }>;
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@pikku/core",
3
- "version": "0.12.67",
3
+ "version": "0.12.70",
4
+ "description": "The Pikku runtime — functions, wirings, services, middleware and types",
4
5
  "author": "yasser.fadl@gmail.com",
5
6
  "license": "MIT",
6
7
  "module": "dist/index.js",
@@ -22,6 +23,7 @@
22
23
  "./function": "./dist/function/index.js",
23
24
  "./channel": "./dist/wirings/channel/index.js",
24
25
  "./workflow": "./dist/wirings/workflow/index.js",
26
+ "./scenario": "./dist/wirings/workflow/pikku-scenario-service.js",
25
27
  "./workflow/timeline": "./dist/wirings/workflow/run-timeline.js",
26
28
  "./workflow/types": "./dist/wirings/workflow/workflow.types.js",
27
29
  "./actor-flow": "./dist/wirings/actor-flow/index.js",
@@ -248,6 +248,17 @@ export type CorePermissionGroup<PikkuPermission = CorePikkuPermission<any>> =
248
248
  | Record<string, PikkuPermission | PikkuPermission[]>
249
249
  | undefined
250
250
 
251
+ /**
252
+ * A lifecycle hook: the same call signature as the function it hangs off, but
253
+ * its return value is discarded. A hook is setup/teardown, not a step — it has
254
+ * no id, no meta and no schema, so it is never recorded and never replayed.
255
+ */
256
+ export type CorePikkuFunctionHook<Services = any, Data = any, Wire = any> = (
257
+ services: Services,
258
+ data: Data,
259
+ wire: Wire
260
+ ) => Promise<void> | void
261
+
251
262
  export type CorePikkuFunctionConfig<
252
263
  PikkuFunction extends
253
264
  | CorePikkuFunction<any, any, any, any, any>
@@ -282,6 +293,8 @@ export type CorePikkuFunctionConfig<
282
293
  workflowRetries?: number
283
294
  /** Timeout for this function when used as a workflow step (e.g. '30s', '5m'). */
284
295
  workflowTimeout?: string
296
+ /** Scenario steps only: this step drives a browser, so the runner must provision one before calling it. */
297
+ browser?: boolean
285
298
  audit?:
286
299
  | boolean
287
300
  | {
@@ -289,6 +302,25 @@ export type CorePikkuFunctionConfig<
289
302
  }
290
303
  approvalDescription?: any
291
304
  func: PikkuFunction
305
+ /**
306
+ * Scenarios only: runs before the scenario body, with the scenario's own
307
+ * signature. Throwing skips the body and fails the run, but `after` still
308
+ * runs.
309
+ */
310
+ before?: CorePikkuFunctionHook
311
+ /**
312
+ * Scenarios only: always runs after the scenario body, in a `finally`.
313
+ * Throwing fails a run that would otherwise have passed; on an
314
+ * already-failed run it attaches as the `cause` and never replaces the
315
+ * original error.
316
+ */
317
+ after?: CorePikkuFunctionHook
318
+ /**
319
+ * Scenarios only: why this scenario is held out of a default run. It is
320
+ * reported as skipped rather than quietly omitted, and naming it directly
321
+ * with `--flows` runs it anyway.
322
+ */
323
+ skip?: string
292
324
  auth?: boolean
293
325
  /**
294
326
  * Scopes the session must hold to run this function. All of them are
package/src/index.ts CHANGED
@@ -55,6 +55,7 @@ export type {
55
55
  CorePikkuAuthConfig,
56
56
  CorePikkuFunction,
57
57
  CorePikkuFunctionConfig,
58
+ CorePikkuFunctionHook,
58
59
  CorePikkuPermission,
59
60
  CorePikkuPermissionConfig,
60
61
  CorePikkuPermissionFactory,
package/src/internal.ts CHANGED
@@ -1,4 +1,8 @@
1
- export { pikkuState, resetPikkuState } from './pikku-state.js'
1
+ export {
2
+ pikkuState,
3
+ resetPikkuState,
4
+ getAllPackageStates,
5
+ } from './pikku-state.js'
2
6
  export { httpRouter } from './wirings/http/routers/http-router.js'
3
7
  export type {
4
8
  CreateSingletonServices,
@@ -104,6 +104,7 @@ const createEmptyPackageState = (): PikkuPackageState => ({
104
104
  },
105
105
  workflows: {
106
106
  registrations: new Map(),
107
+ features: new Map(),
107
108
  meta: {},
108
109
  },
109
110
  trigger: {
@@ -38,11 +38,27 @@ const startTarget = async () => {
38
38
  res.writeHead(401).end()
39
39
  return
40
40
  }
41
+ const rpcName = req.url.slice('/api/rpc/'.length)
42
+ if (rpcName === 'html-error') {
43
+ res
44
+ .writeHead(500, { 'content-type': 'text/html' })
45
+ .end('<html><body>Gateway blew up</body></html>')
46
+ return
47
+ }
48
+ if (rpcName === 'forbidden') {
49
+ res
50
+ .writeHead(403, { 'content-type': 'application/json' })
51
+ .end(
52
+ JSON.stringify({ message: 'MissingScopeError', scope: 'admin' })
53
+ )
54
+ return
55
+ }
41
56
  res.writeHead(200, { 'content-type': 'application/json' }).end(
42
57
  JSON.stringify({
43
- rpcName: req.url.slice('/api/rpc/'.length),
58
+ rpcName,
44
59
  echoed: body.data,
45
60
  cookie,
61
+ userHeader: req.headers['x-user-id'] ?? null,
46
62
  })
47
63
  )
48
64
  return
@@ -115,6 +131,74 @@ describe('HttpScenarioActor', async () => {
115
131
  assert.match(result.cookie, new RegExp(`session=s${loginsBefore + 1}`))
116
132
  })
117
133
 
134
+ test('invokeRaw returns the status and body instead of throwing', async () => {
135
+ const actors = makeActors()
136
+
137
+ // A refusal is the expected outcome of a permissions scenario, so the
138
+ // status and the payload that names the missing scope both have to survive.
139
+ const res = await actors.customer!.invokeRaw('forbidden', {})
140
+
141
+ assert.equal(res.status, 403)
142
+ assert.equal(res.ok, false)
143
+ assert.deepEqual(res.body, {
144
+ message: 'MissingScopeError',
145
+ scope: 'admin',
146
+ })
147
+ })
148
+
149
+ test('invokeRaw reports a success the same way', async () => {
150
+ const actors = makeActors()
151
+ const res = await actors.manager!.invokeRaw('listTodos', { page: 2 })
152
+
153
+ assert.equal(res.status, 200)
154
+ assert.equal(res.ok, true)
155
+ assert.deepEqual((res.body as any).echoed, { page: 2 })
156
+ })
157
+
158
+ test('invokeRaw carries the response text so a step can search it', async () => {
159
+ const actors = makeActors()
160
+ const res = await actors.customer!.invokeRaw('forbidden', {})
161
+
162
+ assert.match(res.serialized, /MissingScopeError/)
163
+ assert.match(res.serialized, /admin/)
164
+ })
165
+
166
+ test('invokeRaw keeps a non-JSON error body instead of failing to parse it', async () => {
167
+ const actors = makeActors()
168
+ const res = await actors.manager!.invokeRaw('html-error', {})
169
+
170
+ assert.equal(res.status, 500)
171
+ assert.equal(res.ok, false)
172
+ assert.equal(res.body, '<html><body>Gateway blew up</body></html>')
173
+ assert.match(res.serialized, /Gateway blew up/)
174
+ })
175
+
176
+ test('invokeRaw reports an empty body as an empty string', async () => {
177
+ const actors = makeActors()
178
+ const res = await actors.manager!.invokeRaw('listTodos', {})
179
+
180
+ assert.equal(typeof res.serialized, 'string')
181
+ })
182
+
183
+ test('invokeRaw passes extra headers through', async () => {
184
+ const actors = makeActors()
185
+ const res = await actors.manager!.invokeRaw(
186
+ 'whoAmI',
187
+ {},
188
+ { headers: { 'x-user-id': 'impersonated-1' } }
189
+ )
190
+
191
+ assert.equal((res.body as any).userHeader, 'impersonated-1')
192
+ })
193
+
194
+ test('invoke still throws on a refusal, naming the status and body', async () => {
195
+ const actors = makeActors()
196
+ await assert.rejects(
197
+ actors.customer!.invoke('forbidden', {}),
198
+ /'forbidden' as 'customer' returned 403.*MissingScopeError/
199
+ )
200
+ })
201
+
118
202
  test('a wrong impersonation secret surfaces status and body', async () => {
119
203
  const actors = makeActors('wrong-secret')
120
204
  await assert.rejects(