@enspirit/opspicy 0.3.2 → 0.6.0
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/README.md +56 -34
- package/bin/help.js +8 -2
- package/dist/configuration.d.ts +8 -2
- package/dist/explorer/api.js +162 -12
- package/dist/explorer/asking.js +51 -0
- package/dist/explorer/dependencies.js +117 -0
- package/dist/explorer/depending.js +139 -0
- package/dist/explorer/diagrams.js +176 -0
- package/dist/explorer/document.js +1 -0
- package/dist/explorer/elo-refs.js +6 -3
- package/dist/explorer/elo.js +18 -0
- package/dist/explorer/index.js +1 -2
- package/dist/explorer/operation.js +2 -4
- package/dist/explorer/pages.js +14 -16
- package/dist/explorer/script/depending.js +41 -0
- package/dist/explorer/script/main.js +1 -0
- package/dist/explorer/script/ranking.js +2 -2
- package/dist/explorer/script/review.js +1 -1
- package/dist/explorer/script/run.js +4 -1
- package/dist/explorer/script/runner.js +420 -0
- package/dist/explorer/script/running.js +111 -0
- package/dist/explorer/script/sending.js +1 -1
- package/dist/explorer/script/served.js +4 -2
- package/dist/explorer/serve.js +1 -1
- package/dist/explorer/server.js +28 -6
- package/dist/explorer/site.js +1 -1
- package/dist/explorer/style.css +91 -1
- package/dist/explorer/telling.js +2 -0
- package/dist/explorer/tests.js +51 -18
- package/dist/explorer/wiki.js +5 -3
- package/dist/files.js +27 -16
- package/dist/judging.js +2 -34
- package/dist/main.js +1 -1
- package/dist/meaning.js +2 -2
- package/dist/modelling.js +99 -25
- package/dist/naming.js +44 -15
- package/dist/notation.js +18 -3
- package/dist/observing.js +112 -0
- package/dist/planning.js +3 -3
- package/dist/reach.js +244 -140
- package/dist/reading.js +14 -18
- package/dist/running.js +18 -8
- package/dist/starting.js +4 -3
- package/dist/suite.js +8 -1
- package/dist/upgrade.js +3 -9
- package/dist/upgrading-state.js +197 -0
- package/dist/upgrading.js +13 -2
- package/dist/variables.js +62 -0
- package/dist/world.js +8 -39
- package/package.json +3 -2
package/dist/judging.js
CHANGED
|
@@ -40,42 +40,10 @@ function answer(operation, kase, observed, meaning, refusal) {
|
|
|
40
40
|
const typed = misfit ? [{ says: `what is answered is not a ${type}`, saw: `${misfit} — ${excerpt(observed.body)}` }] : [];
|
|
41
41
|
return [...typed, ...(kase.then.code && code !== kase.then.code ? [{ says: `it is not refused as ${kase.then.code}`, saw: `refused as ${excerpt(code)}` }] : [])];
|
|
42
42
|
}
|
|
43
|
-
/**
|
|
44
|
-
* The names of what is not as it was: of the state, of the relations, of what others see. Each is
|
|
45
|
-
* named as a whole, `me` or `seenBy.admin.me`, never by a part of it: a state is what an operation
|
|
46
|
-
* controls, and what others see is a state.
|
|
47
|
-
*/
|
|
48
|
-
export function changed(before, after, path = '') {
|
|
49
|
-
if (JSON.stringify(before) === JSON.stringify(after))
|
|
50
|
-
return [];
|
|
51
|
-
const nested = (value) => Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
52
|
-
const deeper = path === '' || path === 'db' || path === 'seenBy' || /^seenBy\.[^.]+$/.test(path);
|
|
53
|
-
if (!deeper || !nested(before) || !nested(after))
|
|
54
|
-
return [path];
|
|
55
|
-
return [...new Set([...Object.keys(before), ...Object.keys(after)])].flatMap((name) => changed(before[name], after[name], path ? `${path}.${name}` : name));
|
|
56
|
-
}
|
|
57
|
-
function unchanged(observed, why) {
|
|
58
|
-
const names = changed(observed.before, observed.after);
|
|
59
|
-
return names.length ? [{ says: `${why}, yet this is not as it was: ${names.join(', ')}` }] : [];
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* The states that changed and that the operation does not control, as it says or as it is derived:
|
|
63
|
-
* a state is named whoever sees it change, `seenBy.admin.boards` as `boards`; a relation is behind
|
|
64
|
-
* the front door, and is no state; one that was refused before or after the call did not change
|
|
65
|
-
* for who looks, who now sees it or no longer does (a sign-in, a sign-out).
|
|
66
|
-
*/
|
|
67
|
-
function uncontrolled(operation, observed) {
|
|
68
|
-
// A state somebody is refused reads as nothing: from or to nothing, what changed is whether they see it, not the state.
|
|
69
|
-
const at = (state, path) => path.split('.').reduce((value, key) => (value && typeof value === 'object' ? value[key] : undefined), state);
|
|
70
|
-
const seen = (path) => at(observed.before, path) !== null && at(observed.after, path) !== null;
|
|
71
|
-
const states = changed(observed.before, observed.after).filter((name) => !name.startsWith('db') && seen(name)).map((name) => name.replace(/^seenBy\.[^.]+\./, ''));
|
|
72
|
-
return [...new Set(states)].filter((name) => !operation.reach.controls.includes(name)).map((name) => ({ says: `"${name}" changed, which ${operation.name} does not control` }));
|
|
73
|
-
}
|
|
74
43
|
function postconditions(operation, kase, observed, meaning) {
|
|
75
44
|
const context = { ...observed, output: observed.body };
|
|
76
45
|
const clauses = [...operation.clauses.filter((clause) => POST.includes(clause.kind)), ...kase.and];
|
|
77
|
-
|
|
78
|
-
return [...clauses.flatMap((clause) => about(meaning, clause, context, true, 'does not hold after the call')), ...(reads ? unchanged(observed, operation.observes ? 'an observation changes nothing' : 'a route that reads changes nothing') : uncontrolled(operation, observed))];
|
|
46
|
+
return clauses.flatMap((clause) => about(meaning, clause, context, true, 'does not hold after the call'));
|
|
79
47
|
}
|
|
80
48
|
/** What a case shows that the specification does not say: nothing, when it passes. */
|
|
81
49
|
export function judge(operation, kase, observed, meaning, refusal) {
|
|
@@ -83,7 +51,7 @@ export function judge(operation, kase, observed, meaning, refusal) {
|
|
|
83
51
|
const answered = answer(operation, kase, observed, meaning, refusal);
|
|
84
52
|
if (answered.length && observed.status !== kase.then.status)
|
|
85
53
|
return [...before, ...answered];
|
|
86
|
-
const after = kase.kind === 'example' ? postconditions(operation, kase, observed, meaning) :
|
|
54
|
+
const after = kase.kind === 'example' ? postconditions(operation, kase, observed, meaning) : [];
|
|
87
55
|
return [...before, ...answered, ...after];
|
|
88
56
|
}
|
|
89
57
|
/**
|
package/dist/main.js
CHANGED
|
@@ -103,7 +103,7 @@ function tell(ran, stopped) {
|
|
|
103
103
|
for (const line of summaryLines(ran))
|
|
104
104
|
console.log(line);
|
|
105
105
|
if (asked.fast)
|
|
106
|
-
console.log('The fast run: nothing was read behind the front door, and each case observed
|
|
106
|
+
console.log('The fast run: nothing was read behind the front door, and each case observed what its clauses read.');
|
|
107
107
|
if (narrowed(asked).length)
|
|
108
108
|
console.log(`Asked: ${narrowed(asked).join(' ')}. The rest of the suite was not run.`);
|
|
109
109
|
if (stopped)
|
package/dist/meaning.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// It can do nothing else: Elo has no input or output of its own.
|
|
4
4
|
import { compileToJavaScript, parse, typecheck, typecheckWithType } from '@enspirit/elo';
|
|
5
5
|
import { DateTime, Duration, Interval } from 'luxon';
|
|
6
|
-
import { NOTHING_NAMED, misnamed, relationsType, sourceOf, typedSourceOf } from './naming.js';
|
|
6
|
+
import { NOTHING_NAMED, explained, misnamed, relationsType, sourceOf, typedSourceOf } from './naming.js';
|
|
7
7
|
const firstLine = (error) => String(error instanceof Error ? error.message : error).split('\n')[0] ?? '';
|
|
8
8
|
function compiled(source) {
|
|
9
9
|
const javascript = compileToJavaScript(parse(source));
|
|
@@ -33,7 +33,7 @@ export function meaningOf(types, names = NOTHING_NAMED) {
|
|
|
33
33
|
}
|
|
34
34
|
};
|
|
35
35
|
const prelude = `${types}\n${relationsType(names)}`;
|
|
36
|
-
const unreadable = (source, moment, exchanged) => moment ? misnamed(source, names) ?? unread(() => at(source, moment)) ?? mistyped(`${prelude}\n${typedSourceOf(source, moment, names, exchanged)}`) : unread(() => expression(source));
|
|
36
|
+
const unreadable = (source, moment, exchanged) => moment ? misnamed(source, names) ?? explained(unread(() => at(source, moment)), names) ?? mistyped(`${prelude}\n${typedSourceOf(source, moment, names, exchanged)}`) : unread(() => expression(source));
|
|
37
37
|
const decides = (name) => truthOf(`${prelude}\n${typedSourceOf(name, 'post', names)}`);
|
|
38
38
|
return { value, misfit: (type, given) => misfit(expression, types, type, given), unreadable, decides };
|
|
39
39
|
}
|
package/dist/modelling.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
import { citizenOf, list, record, text } from './citizens.js';
|
|
5
5
|
import { placeOf } from './lines.js';
|
|
6
6
|
import { RESERVED } from './naming.js';
|
|
7
|
-
import { observedOf } from './notation.js';
|
|
7
|
+
import { itemObservedOf, observedOf, signatureOf } from './notation.js';
|
|
8
8
|
import { named } from './suite.js';
|
|
9
9
|
import { conditionsOf, definitionsOf, typesOf } from './words.js';
|
|
10
10
|
const under = (where, problems) => problems.map((problem) => `${where}: ${problem}`);
|
|
@@ -36,39 +36,113 @@ function agentsOf(written, problems, place) {
|
|
|
36
36
|
}
|
|
37
37
|
return agents;
|
|
38
38
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
39
|
+
const PLAIN = /^[a-z_][a-z0-9_]*$/;
|
|
40
|
+
const EARLIER = 'the form of opspicy 0.3, which `opspicy upgrade` rewrites';
|
|
41
|
+
/** What a state says of its item: `board(id)`, the name one is read by and what it is known by. */
|
|
42
|
+
function itemOf(name, written, problems) {
|
|
43
|
+
if (written === undefined || written === null)
|
|
44
|
+
return undefined;
|
|
45
|
+
try {
|
|
46
|
+
const { name: item, parameters } = signatureOf(written);
|
|
47
|
+
if (!parameters.length)
|
|
48
|
+
throw new Error(`an item is known by something, as in "board(id)", got ${JSON.stringify(written)}`);
|
|
49
|
+
if (RESERVED.includes(item))
|
|
50
|
+
throw new Error(`the name "${item}" is taken by the suite`);
|
|
51
|
+
return { name: item, key: parameters };
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
problems.push(`state "${name}": "item": ${error instanceof Error ? error.message : error}`);
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/** How the front door tells a state: the route that tells it whole, or that route and the one that tells one item, each under the name of what it tells. */
|
|
59
|
+
function webOf(name, item, written, problems) {
|
|
60
|
+
if (written === undefined || written === null)
|
|
61
|
+
return {};
|
|
62
|
+
const routes = typeof written === 'string' ? { [name]: written } : record(written);
|
|
63
|
+
const one = item ? `${item.name}(${item.key.join(', ')})` : '';
|
|
64
|
+
const mine = [];
|
|
65
|
+
for (const key of Object.keys(routes))
|
|
66
|
+
if (key !== name && (!item || signed(key) !== signed(one)))
|
|
67
|
+
mine.push(`"${key}" is not what a route tells: "${name}"${item ? `, or one item, "${one}"` : ''}`);
|
|
68
|
+
const whole = routes[name];
|
|
69
|
+
const single = Object.entries(routes).find(([key]) => item && signed(key) === signed(one))?.[1];
|
|
70
|
+
if (typeof written !== 'string' && !Object.keys(routes).length)
|
|
71
|
+
mine.push('it says the route that tells it, as in "GET /api/boards -> Boards"');
|
|
43
72
|
try {
|
|
44
|
-
|
|
73
|
+
if (whole === undefined)
|
|
74
|
+
throw new Error(`it says the route that tells "${name}" whole${typeof written === 'string' ? '' : `, under "${name}"`}`);
|
|
75
|
+
const web = { ...observedOf(whole), ...(single !== undefined && item ? { one: itemObservedOf(single, item.key) } : {}) };
|
|
76
|
+
problems.push(...mine.map((problem) => `state "${name}": "web": ${problem}`));
|
|
77
|
+
return { web, formaldef: text(whole) };
|
|
45
78
|
}
|
|
46
79
|
catch (error) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
return { name, description, source, formaldef, path: '' };
|
|
80
|
+
problems.push(...[...mine, String(error instanceof Error ? error.message : error)].map((problem) => `state "${name}": "web": ${problem}`));
|
|
81
|
+
return {};
|
|
50
82
|
}
|
|
51
83
|
}
|
|
52
|
-
|
|
84
|
+
/** A signature whatever its spacing: `board( id )` is `board(id)`. */
|
|
85
|
+
const signed = (written) => written.replace(/\s+/g, '');
|
|
86
|
+
/** How the database shows a state: a table, the columns its rows are known by (the item's key, unless it says others), and those read of it. */
|
|
87
|
+
function dbOf(name, item, written, problems) {
|
|
88
|
+
if (written === undefined || written === null)
|
|
89
|
+
return undefined;
|
|
90
|
+
const raw = record(written);
|
|
53
91
|
const names = (given) => list(given).map(text).filter(Boolean);
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
92
|
+
const mine = Object.keys(raw).filter((key) => !['table', 'key', 'attributes'].includes(key)).map((key) => `"${key}" is not something it says (table, key, attributes)`);
|
|
93
|
+
// A column is said by its name, or with the attribute it is: `filter_json: filter`.
|
|
94
|
+
const columns = list(raw.attributes).map((one) => (typeof one === 'string' ? [one.trim()] : (Object.entries(record(one)).map(([column, attribute]) => [column, text(attribute)])[0] ?? [''])));
|
|
95
|
+
const named = Object.fromEntries(columns.filter(([, attribute]) => attribute !== undefined));
|
|
96
|
+
const shown = { table: text(raw.table), key: raw.key === undefined ? item?.key ?? [] : names(raw.key), attributes: columns.map(([column]) => column), ...(Object.keys(named).length ? { named } : {}) };
|
|
97
|
+
if (columns.some(([column, attribute]) => !column || (attribute !== undefined && !/^[A-Za-z_]\w*$/.test(attribute))) || columns.length !== list(raw.attributes).length)
|
|
98
|
+
mine.push('an attribute is a column by its plain name, or a column and the attribute it is, as in "filter_json: filter"');
|
|
99
|
+
if (!item)
|
|
100
|
+
mine.push('a table holds items: the state says its "item", as in "board(id)"');
|
|
101
|
+
else if (shown.key.length !== item.key.length)
|
|
102
|
+
mine.push(`its key is that of the item, ${item.key.join(', ')}: as many columns, in that order`);
|
|
103
|
+
if (!shown.table || ![shown.table, ...shown.key, ...shown.attributes].every((identifier) => PLAIN.test(identifier)))
|
|
104
|
+
mine.push('a table, its key and its attributes, each by its plain name');
|
|
105
|
+
problems.push(...mine.map((problem) => `state "${name}": "db": ${problem}`));
|
|
106
|
+
return mine.length ? undefined : shown;
|
|
59
107
|
}
|
|
60
|
-
|
|
108
|
+
function stateOf(item, problems) {
|
|
109
|
+
const { name, description, source, raw } = item;
|
|
110
|
+
if (RESERVED.includes(name))
|
|
111
|
+
problems.push(`state "${name}": the name is taken by the suite`);
|
|
112
|
+
if ('formaldef' in raw)
|
|
113
|
+
problems.push(`state "${name}": its route is said as "web", no longer as "formaldef": ${EARLIER}`);
|
|
114
|
+
const its = itemOf(name, raw.item, problems);
|
|
115
|
+
const { web, formaldef } = webOf(name, its, raw.web, problems);
|
|
116
|
+
const db = dbOf(name, its, raw.db, problems);
|
|
117
|
+
if (raw.web === undefined && raw.db === undefined && !('formaldef' in raw))
|
|
118
|
+
problems.push(`state "${name}" says what observes it: "web", the route that tells it, or "db", the table that shows it`);
|
|
119
|
+
return { name, description, source, ...(formaldef ? { formaldef } : {}), ...(its ? { item: its } : {}), ...(web ? { web } : {}), ...(db ? { db } : {}) };
|
|
120
|
+
}
|
|
121
|
+
/** The names the state gives: each variable and each item, once. */
|
|
122
|
+
function twiceNamed(state) {
|
|
123
|
+
const names = state.map((one) => one.name);
|
|
124
|
+
return state.flatMap((one) => {
|
|
125
|
+
const item = one.item?.name;
|
|
126
|
+
if (!item)
|
|
127
|
+
return [];
|
|
128
|
+
if (names.includes(item))
|
|
129
|
+
return [`state "${one.name}": its item is called "${item}", which is a state`];
|
|
130
|
+
return state.find((other) => other.item?.name === item) !== one ? [`state "${one.name}": its item is called "${item}", as the item of another state is`] : [];
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
/** What is observed, from `state.yml`: the state variables, each with what observes it. */
|
|
61
134
|
function observedIn(written, problems, place) {
|
|
62
135
|
const raw = record(written);
|
|
63
|
-
for (const key of Object.keys(raw))
|
|
64
|
-
if (
|
|
65
|
-
problems.push(`"
|
|
136
|
+
for (const key of Object.keys(raw)) {
|
|
137
|
+
if (key === 'relations')
|
|
138
|
+
problems.push(`"relations" is no longer a list of its own: what the database shows is a state, which says its table as "db": ${EARLIER}`);
|
|
139
|
+
else if (!['description', 'state'].includes(key))
|
|
140
|
+
problems.push(`"${key}" is not something it says (description, state)`);
|
|
141
|
+
}
|
|
66
142
|
const description = describedBy(raw.description, problems);
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
relations: listed('relation', raw.relations, problems, (at) => place('relations', at), { also: ['table', 'key', 'attributes'] }).map((item) => relationOf(item, problems)),
|
|
71
|
-
};
|
|
143
|
+
const state = listed('state', raw.state, problems, (at) => place('state', at), { also: ['item', 'web', 'db', 'formaldef'] }).map((item) => stateOf(item, problems));
|
|
144
|
+
problems.push(...twiceNamed(state));
|
|
145
|
+
return { ...(description ? { description } : {}), state };
|
|
72
146
|
}
|
|
73
147
|
function actorOf({ name, description, source, raw }, agents, problems) {
|
|
74
148
|
const held = raw.with === undefined || raw.with === null ? null : record(raw.with);
|
|
@@ -161,7 +235,7 @@ export function modelOf(said) {
|
|
|
161
235
|
const { description: ofState, ...observed } = observedIn(said.state, inState, at('state'));
|
|
162
236
|
const { description: ofWorld, ...known } = knownIn(said.world, agents, inWorld, at('world'));
|
|
163
237
|
const conditions = conditionsOf(words, inWords);
|
|
164
|
-
const definitions = definitionsOf(words, observed.state.
|
|
238
|
+
const definitions = definitionsOf(words, observed.state.flatMap((one) => [one.name, ...(one.item ? [one.item.name] : [])]), inWords);
|
|
165
239
|
const types = words.flatMap((one) => typesOf(one.types ?? '', one.area, inWords));
|
|
166
240
|
for (const condition of conditions)
|
|
167
241
|
if (condition.violated && !named(known.actors, condition.violated.actor))
|
package/dist/naming.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
export const RELATIONS = 'TheRelations';
|
|
3
3
|
export const NOTHING_NAMED = { states: [], relations: [], actors: [], definitions: [] };
|
|
4
4
|
/** What neither a state, a predicate nor a function may be called: the suite's own names, and those of the `let`. */
|
|
5
|
-
export const RESERVED = ['input', 'output', 'world', 'old', 'seenBy', 'db', 'nothingChanged', 'the_state', 'the_view', 'the_moment', 'the_now'];
|
|
5
|
+
export const RESERVED = ['input', 'output', 'world', 'old', 'seenBy', 'db', 'nothingChanged', 'the_state', 'the_view', 'the_moment', 'the_now', 'the_db', 'the_rows', 'the_item'];
|
|
6
6
|
/** An expression without what its texts hold: a name in a text names nothing. */
|
|
7
7
|
export const bare = (source) => source.replace(/'(?:[^'\\]|\\.)*'/g, "''");
|
|
8
8
|
const wordsOf = (source) => new Set(bare(source).match(/[A-Za-z_]\w*/g) ?? []);
|
|
@@ -27,24 +27,38 @@ export function definitionsUsed(source, definitions) {
|
|
|
27
27
|
export const witnessesNamed = (source) => [...new Set([...bare(source).matchAll(/\bseenBy\.(\w+)/g)].map((match) => match[1] ?? ''))];
|
|
28
28
|
// The closing parenthesis is on a line of its own: a formaldef may end with a comment.
|
|
29
29
|
const bound = (one) => (one.parameters.length ? `fn(${one.parameters.join(', ')} ~> ${one.formaldef}\n)` : `(${one.formaldef}\n)`);
|
|
30
|
-
/**
|
|
30
|
+
/** The item of a collection, by its key: among the items of `from`, the one whose columns are the key. */
|
|
31
|
+
const found = (key, columns, from) => `fn(${key.join(', ')} ~> find(${from}, the_item ~> ${columns.map((column, at) => `the_item.${column} == ${key[at]}`).join(' && ')}))`;
|
|
32
|
+
const webItems = (names) => (names.items ?? []).filter((one) => one.web);
|
|
33
|
+
const dbItems = (names) => (names.items ?? []).filter((one) => Boolean(one.db));
|
|
34
|
+
/** What the database shows, as a formaldef reads it: each table, and each item by its key. */
|
|
35
|
+
function shown(names) {
|
|
36
|
+
const tables = names.relations.map((name) => `${name}: the_rows.${name}`);
|
|
37
|
+
const items = dbItems(names).map((one) => `${one.name}: ${found(one.key, one.db, `the_rows.${one.of} | []`)}`);
|
|
38
|
+
return `fn(the_rows ~> { ${[...tables, ...items].join(', ')} })`;
|
|
39
|
+
}
|
|
40
|
+
/** A moment as somebody sees it: the state, its items, and the predicates and functions said of it. */
|
|
31
41
|
function view(names, used) {
|
|
32
|
-
const
|
|
33
|
-
const
|
|
42
|
+
const items = webItems(names);
|
|
43
|
+
const members = [...names.states, 'db', ...items.map((one) => one.name), ...used.map((one) => one.name)];
|
|
44
|
+
const state = [...names.states.map((name) => `${name} = the_state.${name}`), 'db = the_db(the_state.db)', ...items.map((one) => `${one.name} = ${found(one.key, one.key, `${one.of} | []`)}`)].join(', ');
|
|
34
45
|
return [`fn(the_state ~> let ${state}`, ...used.map((one) => `, ${one.name} = ${bound(one)}`), `in { ${members.map((name) => `${name}: ${name}`).join(', ')} })`].join('\n');
|
|
35
46
|
}
|
|
36
47
|
/** The expression as Elo reads it: under the names of its moment. */
|
|
37
|
-
export function sourceOf(expression, moment,
|
|
38
|
-
const used = definitionsUsed(expression,
|
|
48
|
+
export function sourceOf(expression, moment, all) {
|
|
49
|
+
const used = definitionsUsed(expression, all.definitions);
|
|
50
|
+
// An item is bound where it is named, and there only: a view that names none is data, which `==` compares.
|
|
51
|
+
const said = new Set([expression, ...used.map((one) => one.formaldef)].flatMap((text) => [...wordsOf(text)]));
|
|
52
|
+
const names = { ...all, items: (all.items ?? []).filter((one) => said.has(one.name)) };
|
|
39
53
|
const seen = witnessesNamed(expression).map((actor) => `${actor}: the_view(the_state.seenBy.${actor})`).join(', ');
|
|
40
54
|
const at = moment === 'post' ? 'old = the_moment(_.before), the_now = the_moment(_.after), input = _.input, output = _.output, nothingChanged = _.after == _.before' : `the_now = the_moment(_.before)${moment === 'pre' ? ', input = _.input' : ''}`;
|
|
41
|
-
const read = [...names.states, 'db', 'seenBy', ...used.map((one) => one.name)].map((name) => `${name} = the_now.${name}`).join(', ');
|
|
42
|
-
return `let world = _.world, the_view = ${view(names, used)}, the_moment = fn(the_state ~> merge(the_view(the_state), { seenBy: { ${seen} } })), ${at}, ${read} in\n${expression}`;
|
|
55
|
+
const read = [...names.states, 'db', 'seenBy', ...webItems(names).map((one) => one.name), ...used.map((one) => one.name)].map((name) => `${name} = the_now.${name}`).join(', ');
|
|
56
|
+
return `let world = _.world, the_db = ${shown(names)}, the_view = ${view(names, used)}, the_moment = fn(the_state ~> merge(the_view(the_state), { seenBy: { ${seen} } })), ${at}, ${read} in\n${expression}`;
|
|
43
57
|
}
|
|
44
58
|
/** An expression changed outside what its texts hold. */
|
|
45
59
|
const rewritten = (source, change) => source.split(/('(?:[^'\\]|\\.)*')/).map((part, at) => (at % 2 ? part : change(part))).join('');
|
|
46
|
-
/** Without `old.` and `seenBy.<actor>.`: the same names, which have the same types. */
|
|
47
|
-
const here = (source) => rewritten(source, (code) => code.replace(/(?<![\w.])old\./g, '').replace(/(?<![\w.])seenBy\.\w+\./g, ''));
|
|
60
|
+
/** Without `old.` and `seenBy.<actor>.`: the same names, which have the same types. An item of the database is read under a name of its own, `the_db_board`. */
|
|
61
|
+
const here = (source) => rewritten(source, (code) => code.replace(/(?<![\w.])old\./g, '').replace(/(?<![\w.])seenBy\.\w+\./g, '').replace(/(?<![\w.])db\.(\w+)(?=\s*\()/g, 'the_db_$1'));
|
|
48
62
|
/** The relations as a type: each a list of rows, of the columns the suite reads. */
|
|
49
63
|
export const relationsType = (names) => `${RELATIONS} = { ${Object.entries(names.columns ?? {}).map(([name, columns]) => `${name}: [{ ${columns.map((column) => `${column}: .`).join(', ')} }]`).join(', ')} }`;
|
|
50
64
|
/**
|
|
@@ -57,9 +71,13 @@ export function typedSourceOf(expression, moment, names, exchanged = {}) {
|
|
|
57
71
|
const of = (value, type) => (type ? `${value} |> ${type}` : value);
|
|
58
72
|
const given = moment === 'given' ? [] : [`input = ${of('_.input', exchanged.input)}`];
|
|
59
73
|
const answered = moment === 'post' ? [`output = ${of('_.output', exchanged.output)}`, 'nothingChanged = _.after == _.before'] : [];
|
|
60
|
-
|
|
74
|
+
// What a route tells is of its type, and the state is that, or the member of it the suite names.
|
|
75
|
+
const told = (name) => (names.types?.[name] ? `(${of(`_.after.${name}`, names.types[name])})${(names.members?.[name] ?? []).map((member) => `.${member}`).join('')}` : `_.after.${name}`);
|
|
76
|
+
const states = names.states.map((name) => `${name} = ${told(name)}`);
|
|
77
|
+
const items = webItems(names).map((one) => `${one.name} = ${found(one.key, one.key, one.of)}`);
|
|
78
|
+
const rows = dbItems(names).map((one) => `the_db_${one.name} = ${found(one.key, one.db, `db.${one.of}`)}`);
|
|
61
79
|
const used = definitionsUsed(expression, names.definitions).map((one) => `${one.name} = ${here(bound(one))}`);
|
|
62
|
-
return `let ${['world = _.world', ...given, ...answered, ...states, `db = ${of('_.after.db', names.columns ? RELATIONS : undefined)}`, ...used].join(',\n')} in\n${here(expression)}`;
|
|
80
|
+
return `let ${['world = _.world', ...given, ...answered, ...states, `db = ${of('_.after.db', names.columns ? RELATIONS : undefined)}`, ...items, ...rows, ...used].join(',\n')} in\n${here(expression)}`;
|
|
63
81
|
}
|
|
64
82
|
/**
|
|
65
83
|
* A name that names nothing, where Elo would give `null` and say nothing: a member of `old`,
|
|
@@ -67,7 +85,8 @@ export function typedSourceOf(expression, moment, names, exchanged = {}) {
|
|
|
67
85
|
*/
|
|
68
86
|
export function misnamed(expression, names) {
|
|
69
87
|
const text = bare(expression);
|
|
70
|
-
const told = [...names.states, ...names.definitions.map((one) => one.name)];
|
|
88
|
+
const told = [...names.states, ...webItems(names).map((one) => one.name), ...names.definitions.map((one) => one.name)];
|
|
89
|
+
const kept = [...names.relations, ...dbItems(names).map((one) => one.name)];
|
|
71
90
|
for (const [, name = ''] of text.matchAll(/(?<![\w.])old\.(\w+)/g))
|
|
72
91
|
if (![...told, 'db', 'seenBy'].includes(name))
|
|
73
92
|
return `"old.${name}" is neither a state, a predicate nor a function`;
|
|
@@ -78,7 +97,17 @@ export function misnamed(expression, names) {
|
|
|
78
97
|
return `"seenBy.${actor}.${name}" is neither a state, a predicate nor a function`;
|
|
79
98
|
}
|
|
80
99
|
for (const [, name = ''] of text.matchAll(/(?<![\w.])(?:old\.)?db\.(\w+)/g))
|
|
81
|
-
if (!
|
|
82
|
-
return `"db.${name}" is not a
|
|
100
|
+
if (!kept.includes(name))
|
|
101
|
+
return told.includes(name) ? `"db.${name}": the database does not show "${name}": state.yml says no "db" of it` : `"db.${name}" is not a state the database shows`;
|
|
83
102
|
return null;
|
|
84
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* What Elo says of a name nobody bound, said as the suite would where the name is one of its
|
|
106
|
+
* own: a state the database alone shows, or the item of one, read as if the front door told it.
|
|
107
|
+
*/
|
|
108
|
+
export function explained(problem, names) {
|
|
109
|
+
const [, name = ''] = problem?.match(/^(?:Undefined variable: '(\w+)'|Unknown function (\w+))/)?.filter(Boolean) ?? [];
|
|
110
|
+
const told = [...names.states, ...webItems(names).map((one) => one.name)];
|
|
111
|
+
const kept = [...names.relations, ...dbItems(names).map((one) => one.name)];
|
|
112
|
+
return name && kept.includes(name) && !told.includes(name) ? `"${name}" is shown by the database alone, and is read as "db.${name}"` : problem;
|
|
113
|
+
}
|
package/dist/notation.js
CHANGED
|
@@ -63,12 +63,27 @@ export function signatureOf(text) {
|
|
|
63
63
|
throw new Error(`expected a name, and what it is given, as in "board(id)", got ${told(text)}`);
|
|
64
64
|
return { name, parameters: (parameters ?? '').split(',').map((parameter) => parameter.trim()).filter(Boolean) };
|
|
65
65
|
}
|
|
66
|
-
|
|
66
|
+
const ROUTE = /^\s*(GET\s+\/\S*?)(?:\s*->\s*([A-Z]\w*)?((?:\.[A-Za-z_]\w*)*))?\s*$/;
|
|
67
|
+
/**
|
|
68
|
+
* `GET /api/boards -> Boards`: how the front door tells a state whole. It is observed by reading,
|
|
69
|
+
* and by nothing else; the type of what is told may follow, and then the member of it that is the
|
|
70
|
+
* state, when the route tells more: `GET /api/emails -> EmailsOfBox.emails`.
|
|
71
|
+
*/
|
|
67
72
|
export function observedOf(text) {
|
|
68
|
-
const [, way, type] = String(text ?? '').match(
|
|
73
|
+
const [, way, type, members] = String(text ?? '').match(ROUTE) ?? [];
|
|
69
74
|
const path = way ? entryOf(way).path : '';
|
|
70
|
-
if (!path || placeholders(path).length)
|
|
75
|
+
if (!path || placeholders(path).length || (!type && members))
|
|
71
76
|
throw new Error(`observed by a GET route with nothing to fill in, then "-> Type" for the type of what it tells if any, got ${told(text)}`);
|
|
77
|
+
const member = (members ?? '').split('.').filter(Boolean);
|
|
78
|
+
return { path, ...(type ? { type } : {}), ...(member.length ? { member } : {}) };
|
|
79
|
+
}
|
|
80
|
+
/** `GET /api/boards/:id -> Board`: how the front door tells one item, which the route names by its key and by nothing else. */
|
|
81
|
+
export function itemObservedOf(text, key) {
|
|
82
|
+
const [, way, type, members] = String(text ?? '').match(ROUTE) ?? [];
|
|
83
|
+
const path = way ? entryOf(way).path : '';
|
|
84
|
+
const named = placeholders(path);
|
|
85
|
+
if (!path || members || named.length !== key.length || key.some((one) => !named.includes(one)))
|
|
86
|
+
throw new Error(`one item is observed by a GET route that names its key, ${key.map((one) => `:${one}`).join(' and ')}, then "-> Type" if any, got ${told(text)}`);
|
|
72
87
|
return type ? { path, type } : { path };
|
|
73
88
|
}
|
|
74
89
|
/**
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { Unrunnable } from './finding.js';
|
|
2
|
+
import { keyText, itemsNamed } from './variables.js';
|
|
3
|
+
const everything = (state, mechanism) => Object.fromEntries(state.filter((one) => one[mechanism]).map((one) => [one.name, 'whole']));
|
|
4
|
+
/**
|
|
5
|
+
* What a case observes at one moment: each variable its clauses read, through the eyes they read it
|
|
6
|
+
* with, as who calls is told it: whole, unless only items of it are named, by keys that can be
|
|
7
|
+
* decided now. An item whose key cannot (the answer names it, and there is none yet) is not looked
|
|
8
|
+
* at. Everything, whole, when a formaldef reads what was observed whole, which says no variable.
|
|
9
|
+
*/
|
|
10
|
+
export function planOf(read, model, witnesses, decide, mode) {
|
|
11
|
+
const web = () => (read.whole ? everything(model.state, 'web') : {});
|
|
12
|
+
const plan = { requester: web(), witnesses: Object.fromEntries(witnesses.map((name) => [name, web()])), db: mode.complete && read.whole ? everything(model.state, 'db') : {} };
|
|
13
|
+
const ask = (looks, mechanism, ref) => {
|
|
14
|
+
const variable = model.state.find((one) => one.name === ref.variable);
|
|
15
|
+
if (!variable?.[mechanism] || looks[ref.variable] === 'whole')
|
|
16
|
+
return;
|
|
17
|
+
const keys = ref.of === 'item' && variable.item ? keyOf(ref, decide) : 'whole';
|
|
18
|
+
if (keys === 'whole')
|
|
19
|
+
looks[ref.variable] = 'whole';
|
|
20
|
+
else if (keys)
|
|
21
|
+
looks[ref.variable] = [...(looks[ref.variable] ?? []), ...keys].filter((one, at, all) => all.findIndex((other) => keyText(other) === keyText(one)) === at);
|
|
22
|
+
};
|
|
23
|
+
for (const one of read.reads) {
|
|
24
|
+
if (one.via === 'db')
|
|
25
|
+
(mode.complete ? ask(plan.db, 'db', one.ref) : undefined);
|
|
26
|
+
else if (!one.by)
|
|
27
|
+
ask(plan.requester, 'web', one.ref);
|
|
28
|
+
else if (plan.witnesses[one.by])
|
|
29
|
+
ask(plan.witnesses[one.by], 'web', one.ref);
|
|
30
|
+
}
|
|
31
|
+
return plan;
|
|
32
|
+
}
|
|
33
|
+
/** The items a key names, decided: one, or each of those it lists; nothing when it is not known now; the variable whole when it cannot be decided at all. */
|
|
34
|
+
function keyOf(ref, decide) {
|
|
35
|
+
try {
|
|
36
|
+
return itemsNamed((ref.key ?? []).map(decide));
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return 'whole';
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* What a route of the state tells somebody: what it answers, and nothing where it refuses them.
|
|
44
|
+
* A route that fails (a 5xx) refused nobody and told nothing: read as nothing, a system that is
|
|
45
|
+
* broken would look like one that is empty. The state was not observed, and the case cannot be judged.
|
|
46
|
+
*/
|
|
47
|
+
export function toldBy({ name }, path, answer) {
|
|
48
|
+
if (answer.status >= 500)
|
|
49
|
+
throw new Unrunnable(`the state "${name}" could not be observed: GET ${path} answered ${answer.status}`);
|
|
50
|
+
return answer.status === 200 ? answer.body : null;
|
|
51
|
+
}
|
|
52
|
+
/** The member of what a route tells that is the state: `emails` of `{ emails, counts }`; nothing of nothing. */
|
|
53
|
+
const memberOf = (told, member = []) => member.reduce((value, name) => (value && typeof value === 'object' && Object.hasOwn(value, name) ? value[name] : null), told);
|
|
54
|
+
/** The route of one item: its key fills the parts the route names. */
|
|
55
|
+
const routeOf = (path, names, key) => path.replace(/:(\w+)/g, (_all, name) => encodeURIComponent(String(key[names.indexOf(name)])));
|
|
56
|
+
/**
|
|
57
|
+
* What the front door tells one browser, of what it is asked. A variable is told whole by its
|
|
58
|
+
* route, asked once however many variables it tells; its items alone by the route that tells one,
|
|
59
|
+
* where state.yml says such a route, each asked once. An item nobody is told is not there for them.
|
|
60
|
+
*/
|
|
61
|
+
export async function throughTheFrontDoor(theirs, state, looks) {
|
|
62
|
+
const asked = new Map();
|
|
63
|
+
const get = (path) => asked.get(path) ?? asked.set(path, theirs.call('GET', path)).get(path);
|
|
64
|
+
const seen = {};
|
|
65
|
+
for (const one of state) {
|
|
66
|
+
const ask = looks[one.name];
|
|
67
|
+
if (!one.web || !ask)
|
|
68
|
+
continue;
|
|
69
|
+
if (ask === 'whole' || !one.web.one || !one.item) {
|
|
70
|
+
seen[one.name] = memberOf(toldBy(one, one.web.path, await get(one.web.path)), one.web.member);
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
const items = [];
|
|
74
|
+
for (const key of ask) {
|
|
75
|
+
const path = routeOf(one.web.one.path, one.item.key, key);
|
|
76
|
+
const told = toldBy(one, path, await get(path));
|
|
77
|
+
if (told !== null)
|
|
78
|
+
items.push(told);
|
|
79
|
+
}
|
|
80
|
+
seen[one.name] = items;
|
|
81
|
+
}
|
|
82
|
+
return seen;
|
|
83
|
+
}
|
|
84
|
+
/** The statement that reads a table: its key and the attributes read of it, in the order of its key; of the rows of these keys alone, when some are given. */
|
|
85
|
+
export function selecting({ table, key, attributes }, keys) {
|
|
86
|
+
const columns = [...key, ...attributes].join(', ');
|
|
87
|
+
if (!keys)
|
|
88
|
+
return { sql: `SELECT ${columns} FROM ${table} ORDER BY ${key.join(', ')}`, values: [] };
|
|
89
|
+
const one = (at) => `(${key.map((column, n) => `${column} = $${at * key.length + n + 1}`).join(' AND ')})`;
|
|
90
|
+
return { sql: `SELECT ${columns} FROM ${table} WHERE ${keys.map((_key, at) => one(at)).join(' OR ') || 'false'} ORDER BY ${key.join(', ')}`, values: keys.flat() };
|
|
91
|
+
}
|
|
92
|
+
/** What the database shows, of what it is asked: a table whole, or its rows of the keys asked, selected by them. Read, and never written. */
|
|
93
|
+
export async function inTheDatabase(query, state, looks) {
|
|
94
|
+
const seen = {};
|
|
95
|
+
for (const one of state) {
|
|
96
|
+
const ask = looks[one.name];
|
|
97
|
+
if (!one.db || !ask)
|
|
98
|
+
continue;
|
|
99
|
+
const { sql, values } = selecting(one.db, ask === 'whole' ? undefined : ask);
|
|
100
|
+
seen[one.name] = JSON.parse(JSON.stringify(await query(sql, values)));
|
|
101
|
+
}
|
|
102
|
+
return seen;
|
|
103
|
+
}
|
|
104
|
+
/** The state, now: as the requester is told it, as the witnesses are, and as the database shows it; of what the plan asks. */
|
|
105
|
+
export async function observe(lookers, model, plan) {
|
|
106
|
+
const mine = await throughTheFrontDoor(lookers.requester, model.state, plan.requester);
|
|
107
|
+
const others = {};
|
|
108
|
+
for (const [name, theirs] of Object.entries(lookers.witnesses))
|
|
109
|
+
others[name] = await throughTheFrontDoor(theirs, model.state, plan.witnesses[name] ?? {});
|
|
110
|
+
const kept = lookers.query ? await inTheDatabase(lookers.query, model.state, plan.db) : {};
|
|
111
|
+
return { ...mine, seenBy: others, db: kept };
|
|
112
|
+
}
|
package/dist/planning.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// What is run for an operation, and what its specification leaves unsaid. Pure.
|
|
2
2
|
import { witnessesNamed } from './naming.js';
|
|
3
|
-
import { isPre, sameSentence } from './suite.js';
|
|
3
|
+
import { TRIGGER, isPre, sameSentence } from './suite.js';
|
|
4
4
|
/** The counterexample a condition gives an operation that says it: its first example, called by who violates it. */
|
|
5
5
|
function generated(operation, clause, model) {
|
|
6
6
|
const condition = model.conditions.find((known) => sameSentence(known.description, clause.description));
|
|
@@ -17,9 +17,9 @@ export function mustOnly(operation) {
|
|
|
17
17
|
const told = (clause) => (clause.mark === 'complete' ? { ...clause, formaldef: undefined } : clause);
|
|
18
18
|
return { ...operation, clauses: operation.clauses.map(told), cases: operation.cases.map((kase) => ({ ...kase, and: kase.and.map(told) })) };
|
|
19
19
|
}
|
|
20
|
-
/** Who else must be signed in for a case: those through whose eyes a formaldef looks, `seenBy.admin.boards`. */
|
|
20
|
+
/** Who else must be signed in for a case: those through whose eyes a formaldef that is checked looks, `seenBy.admin.boards`. What triggers the operation is told, and has nobody look on. */
|
|
21
21
|
export function witnessesOf(operation, kase) {
|
|
22
|
-
const sources = [...operation.clauses, ...kase.and].map((clause) => clause.formaldef ?? '').concat(typeof kase.input === 'string' ? kase.input : '');
|
|
22
|
+
const sources = [...operation.clauses.filter((clause) => clause.kind !== TRIGGER), ...kase.and].map((clause) => clause.formaldef ?? '').concat(typeof kase.input === 'string' ? kase.input : '');
|
|
23
23
|
return [...new Set(sources.flatMap(witnessesNamed))];
|
|
24
24
|
}
|
|
25
25
|
/**
|