rastack 0.0.48 → 0.0.50
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.
- package/CHANGELOG.md +4 -0
- package/components/auto-form/AutoForm.tsx +13 -0
- package/components/auto-form/use-auto-form.ts +28 -0
- package/components/types.ts +8 -0
- package/dist/admin.js +13 -13
- package/dist/compile/analyze.d.ts +26 -0
- package/dist/compile/analyze.js +70 -3
- package/dist/compile/entities.d.ts +19 -0
- package/dist/compile/entities.js +87 -13
- package/dist/compile/index.d.ts +23 -4
- package/dist/compile/index.js +86 -7
- package/dist/compile/model.d.ts +38 -0
- package/dist/compile/openapi.d.ts +9 -0
- package/dist/compile/openapi.js +14 -0
- package/dist/define/index.d.ts +107 -13
- package/dist/define/index.js +1 -1
- package/dist/import/tabular.d.ts +8 -2
- package/dist/import/tabular.js +1 -1
- package/dist/rastack-admin.d.ts +14 -7
- package/dist/rastack-admin.js +85 -40
- package/dist/rastack-import.js +4 -1
- package/dist/validate/adapters.js +2 -0
- package/dist/validate/index.d.ts +1 -0
- package/dist/validate/index.js +1 -0
- package/dist/validate/machine.d.ts +23 -2
- package/dist/validate/machine.js +35 -2
- package/dist/validate/transitions.d.ts +86 -0
- package/dist/validate/transitions.js +199 -0
- package/dist/wasm/rastack_wasm.js +1 -1
- package/dist/wasm/rastack_wasm_bg.wasm +0 -0
- package/hooks/data.ts +209 -0
- package/hooks/entity.ts +222 -0
- package/hooks/form/entity-form.ts +354 -0
- package/hooks/form/form.ts +8 -1
- package/hooks/form/index.ts +7 -1
- package/hooks/index.ts +4 -0
- package/hooks/manifest.ts +41 -0
- package/hooks/registry.ts +39 -0
- package/package.json +1 -1
- package/provider/provider.tsx +8 -1
- package/provider/types.ts +7 -2
- package/src/compile/analyze.ts +83 -4
- package/src/compile/entities.ts +111 -11
- package/src/compile/index.ts +108 -11
- package/src/compile/model.ts +40 -0
- package/src/compile/openapi.ts +13 -1
- package/src/define/index.ts +148 -20
- package/src/import/tabular.ts +9 -2
- package/src/rastack-admin.ts +105 -46
- package/src/rastack-import.ts +4 -1
- package/src/validate/adapters.ts +1 -0
- package/src/validate/index.ts +1 -0
- package/src/validate/machine.ts +55 -3
- package/src/validate/transitions.ts +232 -0
- package/test/components.spec.ts +22 -0
- package/test/transitions.spec.ts +372 -0
- package/test/typed-hooks.spec.ts +407 -0
- package/wasm/rastack_wasm.js +1 -1
- package/wasm/rastack_wasm_bg.wasm +0 -0
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
import { mkdirSync, rmSync, writeFileSync } from "fs";
|
|
2
|
+
import { join } from "path";
|
|
3
|
+
import {
|
|
4
|
+
analyzeProject,
|
|
5
|
+
analyzeResources,
|
|
6
|
+
buildOpenApi,
|
|
7
|
+
writeSchema,
|
|
8
|
+
} from "../src/compile";
|
|
9
|
+
import {
|
|
10
|
+
allowedTransitions,
|
|
11
|
+
applyTransition,
|
|
12
|
+
compileRecordMachine,
|
|
13
|
+
describeTransitions,
|
|
14
|
+
findTransition,
|
|
15
|
+
normalizeTransitions,
|
|
16
|
+
runRecordMachine,
|
|
17
|
+
specsFromResource,
|
|
18
|
+
transitionGuard,
|
|
19
|
+
transitionProblems,
|
|
20
|
+
type TransitionsModel,
|
|
21
|
+
} from "../src/validate";
|
|
22
|
+
import { validateDataset } from "../src/import/tabular";
|
|
23
|
+
|
|
24
|
+
/** The flight lifecycle used throughout — mirrors `resources/airports.ts`. */
|
|
25
|
+
const FLIGHT: TransitionsModel = {
|
|
26
|
+
field: "status",
|
|
27
|
+
states: ["scheduled", "boarding", "departed", "cancelled"],
|
|
28
|
+
initial: "scheduled",
|
|
29
|
+
on: {
|
|
30
|
+
board: { from: ["scheduled"], to: "boarding" },
|
|
31
|
+
depart: { from: ["boarding"], to: "departed", set: { gate: null } },
|
|
32
|
+
cancel: { from: ["scheduled", "boarding"], to: "cancelled" },
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
describe("transitions — the pure core", () => {
|
|
37
|
+
it("normalises an authored block (scalar `from` becomes a list)", () => {
|
|
38
|
+
const model = normalizeTransitions({
|
|
39
|
+
field: "status",
|
|
40
|
+
states: ["a", "b"],
|
|
41
|
+
initial: "a",
|
|
42
|
+
on: { go: { from: "a", to: "b" } },
|
|
43
|
+
});
|
|
44
|
+
expect(model).toEqual({
|
|
45
|
+
field: "status",
|
|
46
|
+
states: ["a", "b"],
|
|
47
|
+
initial: "a",
|
|
48
|
+
on: { go: { from: ["a"], to: "b" } },
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("rejects structurally broken blocks and drops malformed edges", () => {
|
|
53
|
+
expect(normalizeTransitions(undefined)).toBeUndefined();
|
|
54
|
+
expect(normalizeTransitions("nope")).toBeUndefined();
|
|
55
|
+
expect(normalizeTransitions({ field: "s", states: [] })).toBeUndefined();
|
|
56
|
+
const model = normalizeTransitions({
|
|
57
|
+
field: "s",
|
|
58
|
+
states: ["a", "b"],
|
|
59
|
+
on: { ok: { from: "a", to: "b" }, broken: { from: "a" } },
|
|
60
|
+
});
|
|
61
|
+
expect(Object.keys(model!.on)).toEqual(["ok"]);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("reports authoring problems against the resource's fields", () => {
|
|
65
|
+
const fields = [{ name: "status" }, { name: "gate" }];
|
|
66
|
+
expect(transitionProblems(FLIGHT, fields)).toEqual([]);
|
|
67
|
+
|
|
68
|
+
const broken: TransitionsModel = {
|
|
69
|
+
field: "state", // not a field
|
|
70
|
+
states: ["a"],
|
|
71
|
+
initial: "b", // not a state
|
|
72
|
+
on: {
|
|
73
|
+
go: { from: ["a"], to: "z", set: { state: "x", ghost: 1 } }, // bad to/set
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
const problems = transitionProblems(broken, [{ name: "status" }]);
|
|
77
|
+
expect(problems).toEqual(
|
|
78
|
+
expect.arrayContaining([
|
|
79
|
+
expect.stringContaining('"state" is not a field'),
|
|
80
|
+
expect.stringContaining('initial state "b"'),
|
|
81
|
+
expect.stringContaining('to-state "z"'),
|
|
82
|
+
expect.stringContaining("must not patch the state field"),
|
|
83
|
+
expect.stringContaining('unknown field "ghost"'),
|
|
84
|
+
]),
|
|
85
|
+
);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("lists the transitions available from a state", () => {
|
|
89
|
+
expect(allowedTransitions(FLIGHT, "scheduled").map((t) => t.name)).toEqual([
|
|
90
|
+
"board",
|
|
91
|
+
"cancel",
|
|
92
|
+
]);
|
|
93
|
+
expect(allowedTransitions(FLIGHT, "departed")).toEqual([]);
|
|
94
|
+
// No state falls back to the initial state.
|
|
95
|
+
expect(allowedTransitions(FLIGHT, undefined).map((t) => t.name)).toEqual([
|
|
96
|
+
"board",
|
|
97
|
+
"cancel",
|
|
98
|
+
]);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("finds the declared edge covering a jump", () => {
|
|
102
|
+
expect(findTransition(FLIGHT, "boarding", "departed")?.name).toBe("depart");
|
|
103
|
+
expect(findTransition(FLIGHT, "scheduled", "departed")).toBeUndefined();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("guards creates: records start at the initial state", () => {
|
|
107
|
+
expect(transitionGuard(FLIGHT, undefined, {}).ok).toBe(true);
|
|
108
|
+
expect(transitionGuard(FLIGHT, undefined, { status: "scheduled" }).ok).toBe(true);
|
|
109
|
+
const rejected = transitionGuard(FLIGHT, undefined, { status: "departed" });
|
|
110
|
+
expect(rejected).toMatchObject({
|
|
111
|
+
ok: false,
|
|
112
|
+
message: expect.stringContaining('start at "scheduled"'),
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("guards updates: a state change must follow a declared edge", () => {
|
|
117
|
+
const previous = { status: "scheduled" };
|
|
118
|
+
expect(transitionGuard(FLIGHT, previous, { number: "BA1" }).ok).toBe(true);
|
|
119
|
+
expect(transitionGuard(FLIGHT, previous, { status: "scheduled" }).ok).toBe(true);
|
|
120
|
+
expect(transitionGuard(FLIGHT, previous, { status: "boarding" }).ok).toBe(true);
|
|
121
|
+
expect(transitionGuard(FLIGHT, previous, { status: "departed" })).toMatchObject({
|
|
122
|
+
ok: false,
|
|
123
|
+
message: 'status: no transition from "scheduled" to "departed"',
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("carries the matched edge's `set` patches through the guard", () => {
|
|
128
|
+
const result = transitionGuard(FLIGHT, { status: "boarding" }, { status: "departed" });
|
|
129
|
+
expect(result).toEqual({ ok: true, patch: { gate: null } });
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("fires a named transition into a full write patch", () => {
|
|
133
|
+
const applied = applyTransition(FLIGHT, "depart", { status: "boarding" });
|
|
134
|
+
expect(applied).toEqual({
|
|
135
|
+
ok: true,
|
|
136
|
+
patch: { status: "departed", gate: null },
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
expect(applyTransition(FLIGHT, "depart", { status: "scheduled" })).toMatchObject({
|
|
140
|
+
ok: false,
|
|
141
|
+
message: expect.stringContaining('not available from "scheduled"'),
|
|
142
|
+
});
|
|
143
|
+
expect(applyTransition(FLIGHT, "teleport", {})).toMatchObject({
|
|
144
|
+
ok: false,
|
|
145
|
+
message: expect.stringContaining("unknown transition"),
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("describes the machine for UI hints", () => {
|
|
150
|
+
expect(describeTransitions(FLIGHT)).toEqual([
|
|
151
|
+
"board: scheduled → boarding",
|
|
152
|
+
"depart: boarding → departed",
|
|
153
|
+
"cancel: scheduled | boarding → cancelled",
|
|
154
|
+
]);
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
describe("transitions — the record-level gate in the record machine", () => {
|
|
159
|
+
const resource = {
|
|
160
|
+
fields: [
|
|
161
|
+
{ name: "number", type: "string" as const, maxLength: 8 },
|
|
162
|
+
{
|
|
163
|
+
name: "status",
|
|
164
|
+
type: "string" as const,
|
|
165
|
+
options: FLIGHT.states,
|
|
166
|
+
default: "scheduled",
|
|
167
|
+
},
|
|
168
|
+
{ name: "gate", type: "string" as const, null: true },
|
|
169
|
+
],
|
|
170
|
+
};
|
|
171
|
+
const machine = compileRecordMachine(specsFromResource(resource), FLIGHT);
|
|
172
|
+
|
|
173
|
+
it("passes a legal edge with the persisted row in context", () => {
|
|
174
|
+
const run = runRecordMachine(
|
|
175
|
+
machine,
|
|
176
|
+
{ number: "BA1", status: "boarding" },
|
|
177
|
+
{ previous: { status: "scheduled" } },
|
|
178
|
+
);
|
|
179
|
+
expect(run.state).toBe("valid");
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("fails an undeclared jump at the `transition` gate", () => {
|
|
183
|
+
const run = runRecordMachine(
|
|
184
|
+
machine,
|
|
185
|
+
{ number: "BA1", status: "departed" },
|
|
186
|
+
{ previous: { status: "scheduled" } },
|
|
187
|
+
);
|
|
188
|
+
expect(run.state).toBe("invalid");
|
|
189
|
+
expect(run.errors).toEqual([
|
|
190
|
+
expect.objectContaining({
|
|
191
|
+
field: "status",
|
|
192
|
+
gate: "transition",
|
|
193
|
+
message: 'status: no transition from "scheduled" to "departed"',
|
|
194
|
+
}),
|
|
195
|
+
]);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it("treats a run without `previous` as a create", () => {
|
|
199
|
+
const born = runRecordMachine(machine, { number: "BA1", status: "departed" });
|
|
200
|
+
expect(born.state).toBe("invalid");
|
|
201
|
+
expect(born.errors[0].gate).toBe("transition");
|
|
202
|
+
|
|
203
|
+
const fresh = runRecordMachine(machine, { number: "BA1" });
|
|
204
|
+
expect(fresh.state).toBe("valid");
|
|
205
|
+
// The state field's default (the initial state) fills in.
|
|
206
|
+
expect(fresh.values.status).toBe("scheduled");
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it("lets the `member` gate explain an undeclared state first", () => {
|
|
210
|
+
const run = runRecordMachine(machine, { number: "BA1", status: "teleported" });
|
|
211
|
+
expect(run.state).toBe("invalid");
|
|
212
|
+
expect(run.errors).toHaveLength(1);
|
|
213
|
+
expect(run.errors[0].gate).toBe("member");
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it("rejects import rows born mid-machine (dataset surface)", () => {
|
|
217
|
+
const report = validateDataset(
|
|
218
|
+
specsFromResource(resource),
|
|
219
|
+
{
|
|
220
|
+
headers: ["number", "status"],
|
|
221
|
+
rows: [
|
|
222
|
+
["BA1", "scheduled"],
|
|
223
|
+
["BA2", "departed"],
|
|
224
|
+
["BA3", ""],
|
|
225
|
+
],
|
|
226
|
+
},
|
|
227
|
+
{ transitions: FLIGHT },
|
|
228
|
+
);
|
|
229
|
+
expect(report.valid).toBe(2);
|
|
230
|
+
expect(report.issues).toEqual([
|
|
231
|
+
expect.objectContaining({ row: 1, gate: "transition" }),
|
|
232
|
+
]);
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
describe("transitions — through the compiler", () => {
|
|
237
|
+
const workRoot = join(__dirname, "transitions-fixtures");
|
|
238
|
+
const resourcesDir = join(workRoot, "resources");
|
|
239
|
+
|
|
240
|
+
const FLIGHTS_TS = `
|
|
241
|
+
import { resource, s } from "rastack/define";
|
|
242
|
+
|
|
243
|
+
export const Flight = resource("airports", "flight", {
|
|
244
|
+
number: s.string({ maxLength: 8 }),
|
|
245
|
+
status: s.string({ maxLength: 20 }),
|
|
246
|
+
gate: s.string({ maxLength: 5, null: true }),
|
|
247
|
+
}, {
|
|
248
|
+
transitions: {
|
|
249
|
+
field: "status",
|
|
250
|
+
states: ["scheduled", "boarding", "departed", "cancelled"],
|
|
251
|
+
initial: "scheduled",
|
|
252
|
+
on: {
|
|
253
|
+
board: { from: "scheduled", to: "boarding" },
|
|
254
|
+
depart: { from: "boarding", to: "departed", set: { gate: null } },
|
|
255
|
+
cancel: { from: ["scheduled", "boarding"], to: "cancelled" },
|
|
256
|
+
},
|
|
257
|
+
},
|
|
258
|
+
});
|
|
259
|
+
`;
|
|
260
|
+
|
|
261
|
+
const ORDERS_TS = `
|
|
262
|
+
// A plain data class with a \`static transitions\` block — the class-authored
|
|
263
|
+
// twin of the DSL option. Statics are never columns.
|
|
264
|
+
export class Order {
|
|
265
|
+
reference!: string;
|
|
266
|
+
status = "draft";
|
|
267
|
+
|
|
268
|
+
static transitions = {
|
|
269
|
+
field: "status",
|
|
270
|
+
states: ["draft", "placed", "shipped"],
|
|
271
|
+
initial: "draft",
|
|
272
|
+
on: {
|
|
273
|
+
place: { from: "draft", to: "placed" },
|
|
274
|
+
ship: { from: "placed", to: "shipped" },
|
|
275
|
+
},
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
`;
|
|
279
|
+
|
|
280
|
+
beforeAll(() => {
|
|
281
|
+
mkdirSync(resourcesDir, { recursive: true });
|
|
282
|
+
writeFileSync(join(resourcesDir, "flights.ts"), FLIGHTS_TS);
|
|
283
|
+
writeFileSync(join(resourcesDir, "orders.ts"), ORDERS_TS);
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
afterAll(() => {
|
|
287
|
+
rmSync(workRoot, { recursive: true, force: true });
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it("compiles a DSL transitions block into the manifest, normalised", () => {
|
|
291
|
+
const resources = analyzeResources(resourcesDir);
|
|
292
|
+
const flight = resources.find((r) => r.model === "flight")!;
|
|
293
|
+
expect(flight.transitions).toEqual({
|
|
294
|
+
field: "status",
|
|
295
|
+
states: ["scheduled", "boarding", "departed", "cancelled"],
|
|
296
|
+
initial: "scheduled",
|
|
297
|
+
on: {
|
|
298
|
+
board: { from: ["scheduled"], to: "boarding" },
|
|
299
|
+
depart: { from: ["boarding"], to: "departed", set: { gate: null } },
|
|
300
|
+
cancel: { from: ["scheduled", "boarding"], to: "cancelled" },
|
|
301
|
+
},
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it("stamps the state field with the states and the initial default", () => {
|
|
306
|
+
const resources = analyzeResources(resourcesDir);
|
|
307
|
+
const flight = resources.find((r) => r.model === "flight")!;
|
|
308
|
+
const status = flight.fields.find((f) => f.name === "status");
|
|
309
|
+
expect(status).toMatchObject({
|
|
310
|
+
options: ["scheduled", "boarding", "departed", "cancelled"],
|
|
311
|
+
default: "scheduled",
|
|
312
|
+
});
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
it("reads a class's `static transitions` (and keeps it out of the columns)", () => {
|
|
316
|
+
const resources = analyzeResources(resourcesDir);
|
|
317
|
+
const order = resources.find((r) => r.model === "order")!;
|
|
318
|
+
expect(order.transitions).toMatchObject({
|
|
319
|
+
field: "status",
|
|
320
|
+
states: ["draft", "placed", "shipped"],
|
|
321
|
+
on: { place: { from: ["draft"], to: "placed" } },
|
|
322
|
+
});
|
|
323
|
+
expect(order.fields.map((f) => f.name)).toEqual(["reference", "status"]);
|
|
324
|
+
const status = order.fields.find((f) => f.name === "status");
|
|
325
|
+
expect(status?.options).toEqual(["draft", "placed", "shipped"]);
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
it("emits `enum` and `x-rastack-transitions` into the OpenAPI contract", () => {
|
|
329
|
+
const resources = analyzeResources(resourcesDir);
|
|
330
|
+
const flight = resources.find((r) => r.model === "flight")!;
|
|
331
|
+
const doc = buildOpenApi(resources);
|
|
332
|
+
|
|
333
|
+
const status = doc.components.schemas.Flight.properties.status;
|
|
334
|
+
expect(status.enum).toEqual(["scheduled", "boarding", "departed", "cancelled"]);
|
|
335
|
+
|
|
336
|
+
const write = doc.components.schemas.FlightRequest;
|
|
337
|
+
expect(write["x-rastack-transitions"]).toEqual(flight.transitions);
|
|
338
|
+
expect(doc.components.schemas.PatchedFlightRequest["x-rastack-transitions"])
|
|
339
|
+
.toEqual(flight.transitions);
|
|
340
|
+
|
|
341
|
+
// The exported `writeSchema` (what `useForm(Entity)` derives at runtime)
|
|
342
|
+
// is the same schema the document publishes.
|
|
343
|
+
expect(writeSchema(flight, false)).toEqual(write);
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
it("surfaces authoring mistakes as compile diagnostics", () => {
|
|
347
|
+
const brokenDir = join(workRoot, "broken");
|
|
348
|
+
mkdirSync(brokenDir, { recursive: true });
|
|
349
|
+
writeFileSync(
|
|
350
|
+
join(brokenDir, "broken.ts"),
|
|
351
|
+
`
|
|
352
|
+
import { resource, s } from "rastack/define";
|
|
353
|
+
export const Thing = resource("app", "thing", {
|
|
354
|
+
status: s.string({ maxLength: 10 }),
|
|
355
|
+
}, {
|
|
356
|
+
transitions: {
|
|
357
|
+
field: "status",
|
|
358
|
+
states: ["a", "b"],
|
|
359
|
+
on: { go: { from: "a", to: "missing" } },
|
|
360
|
+
},
|
|
361
|
+
});
|
|
362
|
+
`,
|
|
363
|
+
);
|
|
364
|
+
const { diagnostics } = analyzeProject(brokenDir);
|
|
365
|
+
expect(diagnostics).toEqual([
|
|
366
|
+
expect.objectContaining({
|
|
367
|
+
severity: "error",
|
|
368
|
+
message: expect.stringContaining('to-state "missing"'),
|
|
369
|
+
}),
|
|
370
|
+
]);
|
|
371
|
+
});
|
|
372
|
+
});
|