@antelopejs/dms-assistant 0.2.7-next.441.g5176154 → 0.2.7-next.442.gc5217b0
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.
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import type { JobKind, Scenario, Step } from "./connection-analysis-world";
|
|
2
|
+
|
|
3
|
+
const OPEN: Step = { kind: "open" };
|
|
4
|
+
const CLOSE: Step = { kind: "close" };
|
|
5
|
+
const RESET: Step = { kind: "reset" };
|
|
6
|
+
const CLOSE_TAB: Step = { kind: "closeTab" };
|
|
7
|
+
const CLOSE_TAB_QUIET: Step = { kind: "closeTab", quiet: true };
|
|
8
|
+
const RESET_QUIET: Step = { kind: "reset", quiet: true };
|
|
9
|
+
|
|
10
|
+
const connect = (database: string, quiet = false): Step => ({
|
|
11
|
+
kind: "connect",
|
|
12
|
+
database,
|
|
13
|
+
quiet,
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
const STARTS_TWICE = "2 analysis jobs started for shop, where one may";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Two tabs that both remember `shop`, as a tab duplicated while a connection loads does: the
|
|
20
|
+
* project declares it and each tab's storage holds it.
|
|
21
|
+
*/
|
|
22
|
+
function duplicated(
|
|
23
|
+
name: string,
|
|
24
|
+
job: JobKind,
|
|
25
|
+
first: Step[],
|
|
26
|
+
second: Step[],
|
|
27
|
+
defect?: string,
|
|
28
|
+
): Scenario {
|
|
29
|
+
return {
|
|
30
|
+
name,
|
|
31
|
+
job,
|
|
32
|
+
declared: ["shop"],
|
|
33
|
+
remembered: ["shop", "shop"],
|
|
34
|
+
programs: [first, second],
|
|
35
|
+
...(defect === undefined ? {} : { defect }),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** A tab with nothing remembered, which connects by hand. */
|
|
40
|
+
function connecting(
|
|
41
|
+
name: string,
|
|
42
|
+
job: JobKind,
|
|
43
|
+
first: Step[],
|
|
44
|
+
second: Step[] = [],
|
|
45
|
+
): Scenario {
|
|
46
|
+
return {
|
|
47
|
+
name,
|
|
48
|
+
job,
|
|
49
|
+
declared: [],
|
|
50
|
+
remembered: [null, null],
|
|
51
|
+
programs: [first, second],
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The tabs, popups and orders of steps the check covers: two tabs of one browser, each with one
|
|
57
|
+
* program of steps, and a job that does or does not ask which schemas to read.
|
|
58
|
+
*/
|
|
59
|
+
export const SCENARIOS: Scenario[] = [
|
|
60
|
+
duplicated("duplicate, both stay", "asks-schemas", [OPEN], [OPEN]),
|
|
61
|
+
duplicated("duplicate, both stay, no question", "direct", [OPEN], [OPEN]),
|
|
62
|
+
duplicated(
|
|
63
|
+
"duplicate, the first tab closes once every answer arrived",
|
|
64
|
+
"asks-schemas",
|
|
65
|
+
[OPEN, CLOSE_TAB_QUIET],
|
|
66
|
+
[OPEN],
|
|
67
|
+
),
|
|
68
|
+
duplicated(
|
|
69
|
+
"duplicate, the second tab closes once every answer arrived",
|
|
70
|
+
"direct",
|
|
71
|
+
[OPEN],
|
|
72
|
+
[OPEN, CLOSE_TAB_QUIET],
|
|
73
|
+
),
|
|
74
|
+
duplicated(
|
|
75
|
+
"duplicate, the first tab resets once every answer arrived",
|
|
76
|
+
"asks-schemas",
|
|
77
|
+
[OPEN, RESET_QUIET],
|
|
78
|
+
[OPEN],
|
|
79
|
+
),
|
|
80
|
+
duplicated(
|
|
81
|
+
"duplicate, the first tab closes while an answer is in flight",
|
|
82
|
+
"direct",
|
|
83
|
+
[OPEN, CLOSE_TAB],
|
|
84
|
+
[OPEN],
|
|
85
|
+
STARTS_TWICE,
|
|
86
|
+
),
|
|
87
|
+
duplicated(
|
|
88
|
+
"duplicate, the first tab resets while an answer is in flight",
|
|
89
|
+
"direct",
|
|
90
|
+
[OPEN, RESET],
|
|
91
|
+
[OPEN],
|
|
92
|
+
STARTS_TWICE,
|
|
93
|
+
),
|
|
94
|
+
duplicated(
|
|
95
|
+
"duplicate, the first tab connects another database once every answer arrived",
|
|
96
|
+
"asks-schemas",
|
|
97
|
+
[OPEN, connect("orders", true)],
|
|
98
|
+
[OPEN],
|
|
99
|
+
),
|
|
100
|
+
duplicated(
|
|
101
|
+
"duplicate, the first tab connects another database while an answer is in flight",
|
|
102
|
+
"direct",
|
|
103
|
+
[OPEN, connect("orders")],
|
|
104
|
+
[OPEN],
|
|
105
|
+
STARTS_TWICE,
|
|
106
|
+
),
|
|
107
|
+
connecting(
|
|
108
|
+
"connect, close the popup, open it, connect another",
|
|
109
|
+
"asks-schemas",
|
|
110
|
+
[OPEN, connect("shop"), CLOSE, OPEN, connect("orders")],
|
|
111
|
+
),
|
|
112
|
+
connecting("connect, close the popup, open it, reset", "direct", [
|
|
113
|
+
OPEN,
|
|
114
|
+
connect("shop"),
|
|
115
|
+
CLOSE,
|
|
116
|
+
OPEN,
|
|
117
|
+
RESET,
|
|
118
|
+
]),
|
|
119
|
+
connecting("connect, reset, connect another", "asks-schemas", [
|
|
120
|
+
OPEN,
|
|
121
|
+
connect("shop"),
|
|
122
|
+
RESET,
|
|
123
|
+
connect("orders"),
|
|
124
|
+
]),
|
|
125
|
+
connecting("connect twice", "direct", [
|
|
126
|
+
OPEN,
|
|
127
|
+
connect("shop"),
|
|
128
|
+
connect("shop"),
|
|
129
|
+
]),
|
|
130
|
+
];
|
|
@@ -0,0 +1,497 @@
|
|
|
1
|
+
import { mount, type VueWrapper } from "@vue/test-utils";
|
|
2
|
+
import { defineComponent } from "vue";
|
|
3
|
+
import {
|
|
4
|
+
type DatabaseConnect,
|
|
5
|
+
useDatabaseConnect,
|
|
6
|
+
} from "../../app/onboarding/connect-database";
|
|
7
|
+
import {
|
|
8
|
+
type ConnectMemory,
|
|
9
|
+
connectMemory,
|
|
10
|
+
CONNECT_MEMORY_KEY,
|
|
11
|
+
} from "../../app/onboarding/connect-memory";
|
|
12
|
+
import {
|
|
13
|
+
analysisWith,
|
|
14
|
+
LISTED,
|
|
15
|
+
PROJECT,
|
|
16
|
+
READER,
|
|
17
|
+
} from "../support/database-connect";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* What a tab does. A `quiet` step waits until no answer is in flight anywhere, so the tab leaves
|
|
21
|
+
* only after every request sent so far was answered.
|
|
22
|
+
*/
|
|
23
|
+
export type Step =
|
|
24
|
+
| { kind: "open"; quiet?: boolean }
|
|
25
|
+
| { kind: "close"; quiet?: boolean }
|
|
26
|
+
| { kind: "closeTab"; quiet?: boolean }
|
|
27
|
+
| { kind: "reset"; quiet?: boolean }
|
|
28
|
+
| { kind: "connect"; database: string; quiet?: boolean };
|
|
29
|
+
|
|
30
|
+
/** Whether the analysis the server starts asks which schemas to read before it finishes. */
|
|
31
|
+
export type JobKind = "asks-schemas" | "direct";
|
|
32
|
+
|
|
33
|
+
export interface Scenario {
|
|
34
|
+
name: string;
|
|
35
|
+
job: JobKind;
|
|
36
|
+
/** The databases the project already declares, which the tabs' remembered connection follows. */
|
|
37
|
+
declared: string[];
|
|
38
|
+
/** The database each tab remembers when it begins, or null for a tab that remembers none. */
|
|
39
|
+
remembered: (string | null)[];
|
|
40
|
+
programs: Step[][];
|
|
41
|
+
/** The rule the current code is known to break in this scenario, when it does. */
|
|
42
|
+
defect?: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface Pending {
|
|
46
|
+
id: number;
|
|
47
|
+
tab: Tab;
|
|
48
|
+
label: string;
|
|
49
|
+
deliver: () => void;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
interface Timer {
|
|
53
|
+
id: number;
|
|
54
|
+
tab: Tab;
|
|
55
|
+
run: () => void;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
interface Popup {
|
|
59
|
+
wrapper: VueWrapper;
|
|
60
|
+
connect: DatabaseConnect;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
interface Tab {
|
|
64
|
+
index: number;
|
|
65
|
+
storage: Map<string, string>;
|
|
66
|
+
memory: ConnectMemory;
|
|
67
|
+
program: Step[];
|
|
68
|
+
next: number;
|
|
69
|
+
popup?: Popup;
|
|
70
|
+
closed: boolean;
|
|
71
|
+
expected: string | null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
interface Analysis {
|
|
75
|
+
id: string;
|
|
76
|
+
open: unknown[];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const SCHEMAS_QUESTION = {
|
|
80
|
+
id: "schemas",
|
|
81
|
+
kind: "schemas",
|
|
82
|
+
question: "which",
|
|
83
|
+
options: [{ value: "public", title: "public (4)" }],
|
|
84
|
+
proposed: null,
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const OWNER = "owner-1";
|
|
88
|
+
|
|
89
|
+
function urlOf(database: string): string {
|
|
90
|
+
return `postgres://app:secret@db.example.com/${database}`;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function databaseOf(url: string): string {
|
|
94
|
+
return new URL(url).pathname.slice(1);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function notFound(): Error {
|
|
98
|
+
return Object.assign(new Error("not found"), { data: { code: "not_found" } });
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const nextTask = (): Promise<void> => new Promise((done) => setImmediate(done));
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* One run of a scenario: the tabs of one browser over one `@antelopejs/dms`, every step they take
|
|
105
|
+
* and every answer they wait for chosen by the caller, one at a time. The code under test is the
|
|
106
|
+
* real `useDatabaseConnect` with its real `connectMemory`; the server, the Web Locks and the timers
|
|
107
|
+
* are stand-ins that never act by themselves.
|
|
108
|
+
*
|
|
109
|
+
* A request reaches the server the moment it is sent and changes it then; only its answer waits
|
|
110
|
+
* for a choice, and a read answers what the server held when it was sent. A poll timer fires only
|
|
111
|
+
* when no answer is in flight: every answer arrives within the one-second poll period. A Web Lock is decided when it is requested, and only its callback waits. A closed
|
|
112
|
+
* tab stops: its pending answers and timers vanish, its locks are released, and what it already
|
|
113
|
+
* sent stays.
|
|
114
|
+
*/
|
|
115
|
+
export class World {
|
|
116
|
+
readonly tabs: Tab[] = [];
|
|
117
|
+
readonly starts = new Map<string, number>();
|
|
118
|
+
readonly staleAnswers: string[] = [];
|
|
119
|
+
private readonly declared = new Set<string>();
|
|
120
|
+
private readonly analyses = new Map<string, Analysis>();
|
|
121
|
+
private readonly held = new Map<string, Tab>();
|
|
122
|
+
private pending: Pending[] = [];
|
|
123
|
+
private timers: Timer[] = [];
|
|
124
|
+
private counter = 0;
|
|
125
|
+
|
|
126
|
+
constructor(private readonly scenario: Scenario) {
|
|
127
|
+
for (const database of scenario.declared) this.declared.add(database);
|
|
128
|
+
scenario.programs.forEach((program, index) => {
|
|
129
|
+
const storage = new Map<string, string>();
|
|
130
|
+
const memory = connectMemory(
|
|
131
|
+
() => ({
|
|
132
|
+
getItem: (key) => storage.get(key) ?? null,
|
|
133
|
+
setItem: (key, value) => {
|
|
134
|
+
storage.set(key, value);
|
|
135
|
+
},
|
|
136
|
+
removeItem: (key) => {
|
|
137
|
+
storage.delete(key);
|
|
138
|
+
},
|
|
139
|
+
}),
|
|
140
|
+
() => OWNER,
|
|
141
|
+
);
|
|
142
|
+
const remembered = scenario.remembered[index] ?? null;
|
|
143
|
+
if (remembered !== null) {
|
|
144
|
+
memory.write(memory.begin(), {
|
|
145
|
+
engine: "postgres",
|
|
146
|
+
connection: remembered,
|
|
147
|
+
access: READER,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
this.tabs.push({
|
|
151
|
+
index,
|
|
152
|
+
storage,
|
|
153
|
+
memory,
|
|
154
|
+
program,
|
|
155
|
+
next: 0,
|
|
156
|
+
closed: false,
|
|
157
|
+
expected: remembered,
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** What the next choice can be: each tab's next step, each answer, each timer. */
|
|
163
|
+
choices(): string[] {
|
|
164
|
+
const found: string[] = [];
|
|
165
|
+
for (const tab of this.tabs) {
|
|
166
|
+
if (tab.closed) continue;
|
|
167
|
+
const step = tab.program[tab.next];
|
|
168
|
+
if (step !== undefined && this.allowed(step)) {
|
|
169
|
+
found.push(`tab ${tab.index}: ${step.kind}`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
for (const pending of this.pending) {
|
|
173
|
+
found.push(`tab ${pending.tab.index}: ${pending.label}`);
|
|
174
|
+
}
|
|
175
|
+
if (this.pending.length === 0) {
|
|
176
|
+
for (const timer of this.timers) {
|
|
177
|
+
found.push(`tab ${timer.tab.index}: poll timer`);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return found;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** Takes the `choice`th of {@link choices}. */
|
|
184
|
+
async take(choice: number): Promise<string> {
|
|
185
|
+
let index = choice;
|
|
186
|
+
for (const tab of this.tabs) {
|
|
187
|
+
if (tab.closed) continue;
|
|
188
|
+
const step = tab.program[tab.next];
|
|
189
|
+
if (step === undefined || !this.allowed(step)) continue;
|
|
190
|
+
if (index === 0) {
|
|
191
|
+
tab.next += 1;
|
|
192
|
+
await this.run(tab, () => this.perform(tab, step));
|
|
193
|
+
return `tab ${tab.index}: ${describe(step)}`;
|
|
194
|
+
}
|
|
195
|
+
index -= 1;
|
|
196
|
+
}
|
|
197
|
+
if (index < this.pending.length) {
|
|
198
|
+
const [pending] = this.pending.splice(index, 1);
|
|
199
|
+
if (pending === undefined) throw new Error("no pending answer");
|
|
200
|
+
await this.run(pending.tab, pending.deliver);
|
|
201
|
+
return `tab ${pending.tab.index}: ${pending.label}`;
|
|
202
|
+
}
|
|
203
|
+
index -= this.pending.length;
|
|
204
|
+
const [timer] = this.timers.splice(index, 1);
|
|
205
|
+
if (timer === undefined) throw new Error("no such choice");
|
|
206
|
+
await this.run(timer.tab, timer.run);
|
|
207
|
+
return `tab ${timer.tab.index}: poll timer`;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
private allowed(step: Step): boolean {
|
|
211
|
+
return step.quiet !== true || this.pending.length === 0;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** The rules that must hold after every step; each broken one is a sentence. */
|
|
215
|
+
violations(): string[] {
|
|
216
|
+
const broken: string[] = [];
|
|
217
|
+
for (const [connection, count] of this.starts) {
|
|
218
|
+
if (count > 1) {
|
|
219
|
+
broken.push(
|
|
220
|
+
`${count} analysis jobs started for ${connection}, where one may`,
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
for (const stale of this.staleAnswers) {
|
|
225
|
+
broken.push(stale);
|
|
226
|
+
}
|
|
227
|
+
if (this.pending.length === 0) {
|
|
228
|
+
for (const [name, holder] of this.held) {
|
|
229
|
+
if (holder.popup?.connect.phase.value.kind !== "analysing") {
|
|
230
|
+
broken.push(
|
|
231
|
+
`tab ${holder.index} still holds ${name} after it stopped following the connection`,
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
for (const tab of this.tabs) {
|
|
237
|
+
const kept = writtenBy(tab.storage);
|
|
238
|
+
if (kept !== undefined && kept !== tab.expected) {
|
|
239
|
+
broken.push(
|
|
240
|
+
`tab ${tab.index} remembers ${kept} after its latest attempt moved to ${tab.expected ?? "nothing"}`,
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return broken;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** Unmounts every popup left, as the end of the run does. */
|
|
248
|
+
async end(): Promise<void> {
|
|
249
|
+
for (const tab of this.tabs) {
|
|
250
|
+
await this.run(tab, () => tab.popup?.wrapper.unmount());
|
|
251
|
+
tab.popup = undefined;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
private async run(tab: Tab, body: () => void): Promise<void> {
|
|
256
|
+
const { setTimeout: realSet, clearTimeout: realClear } = globalThis;
|
|
257
|
+
globalThis.setTimeout = ((run: () => void) => {
|
|
258
|
+
this.counter += 1;
|
|
259
|
+
this.timers.push({ id: this.counter, tab, run });
|
|
260
|
+
return this.counter;
|
|
261
|
+
}) as never;
|
|
262
|
+
globalThis.clearTimeout = ((id: number) => {
|
|
263
|
+
this.timers = this.timers.filter((timer) => timer.id !== id);
|
|
264
|
+
}) as never;
|
|
265
|
+
try {
|
|
266
|
+
body();
|
|
267
|
+
await nextTask();
|
|
268
|
+
await nextTask();
|
|
269
|
+
} finally {
|
|
270
|
+
globalThis.setTimeout = realSet;
|
|
271
|
+
globalThis.clearTimeout = realClear;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
private perform(tab: Tab, step: Step): void {
|
|
276
|
+
switch (step.kind) {
|
|
277
|
+
case "open":
|
|
278
|
+
if (tab.popup === undefined) tab.popup = this.mountPopup(tab);
|
|
279
|
+
return;
|
|
280
|
+
case "close":
|
|
281
|
+
tab.popup?.wrapper.unmount();
|
|
282
|
+
tab.popup = undefined;
|
|
283
|
+
return;
|
|
284
|
+
case "closeTab":
|
|
285
|
+
this.closeTab(tab);
|
|
286
|
+
return;
|
|
287
|
+
case "reset":
|
|
288
|
+
tab.popup?.connect.reset();
|
|
289
|
+
tab.expected = null;
|
|
290
|
+
return;
|
|
291
|
+
case "connect":
|
|
292
|
+
if (tab.popup === undefined) return;
|
|
293
|
+
tab.expected = databaseOf(urlOf(step.database));
|
|
294
|
+
void tab.popup.connect.connect("postgres", urlOf(step.database));
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
private closeTab(tab: Tab): void {
|
|
300
|
+
tab.closed = true;
|
|
301
|
+
this.pending = this.pending.filter((pending) => pending.tab !== tab);
|
|
302
|
+
this.timers = this.timers.filter((timer) => timer.tab !== tab);
|
|
303
|
+
for (const [name, holder] of this.held) {
|
|
304
|
+
if (holder === tab) this.held.delete(name);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
private mountPopup(tab: Tab): Popup {
|
|
309
|
+
Object.defineProperty(window.navigator, "locks", {
|
|
310
|
+
value: this.locksOf(tab),
|
|
311
|
+
configurable: true,
|
|
312
|
+
});
|
|
313
|
+
const api = this.apiOf(tab);
|
|
314
|
+
let connect: DatabaseConnect | undefined;
|
|
315
|
+
const wrapper = mount(
|
|
316
|
+
defineComponent({
|
|
317
|
+
setup() {
|
|
318
|
+
connect = useDatabaseConnect(api as never, tab.memory);
|
|
319
|
+
return () => null;
|
|
320
|
+
},
|
|
321
|
+
}),
|
|
322
|
+
);
|
|
323
|
+
if (connect === undefined) throw new Error("the popup did not set up");
|
|
324
|
+
return { wrapper, connect };
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
private later<T>(tab: Tab, label: string, value: () => T): Promise<T> {
|
|
328
|
+
return new Promise((resolve) => {
|
|
329
|
+
this.counter += 1;
|
|
330
|
+
this.pending.push({
|
|
331
|
+
id: this.counter,
|
|
332
|
+
tab,
|
|
333
|
+
label,
|
|
334
|
+
deliver: () => resolve(value()),
|
|
335
|
+
});
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
private locksOf(tab: Tab) {
|
|
340
|
+
return {
|
|
341
|
+
request: (
|
|
342
|
+
name: string,
|
|
343
|
+
_options: { ifAvailable: boolean },
|
|
344
|
+
callback: (lock: { name: string } | null) => unknown,
|
|
345
|
+
): Promise<unknown> => {
|
|
346
|
+
const taken = this.held.has(name);
|
|
347
|
+
if (!taken) this.held.set(name, tab);
|
|
348
|
+
return this.later(tab, `lock ${name} answered`, () => {
|
|
349
|
+
if (taken) return callback(null);
|
|
350
|
+
const result = Promise.resolve(callback({ name }));
|
|
351
|
+
void result.finally(() => {
|
|
352
|
+
if (this.held.get(name) === tab) this.held.delete(name);
|
|
353
|
+
});
|
|
354
|
+
return result;
|
|
355
|
+
});
|
|
356
|
+
},
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
private apiOf(tab: Tab) {
|
|
361
|
+
const connection = (database: string): string => database;
|
|
362
|
+
return {
|
|
363
|
+
checkConnection: () =>
|
|
364
|
+
this.later(tab, "connection check answered", () => READER),
|
|
365
|
+
projectModules: async () => PROJECT,
|
|
366
|
+
configureProject: (
|
|
367
|
+
_steps: unknown,
|
|
368
|
+
_dryRun: boolean,
|
|
369
|
+
variables?: { value: string }[],
|
|
370
|
+
) => {
|
|
371
|
+
const url = variables?.[0]?.value;
|
|
372
|
+
if (url !== undefined) this.declared.add(databaseOf(url));
|
|
373
|
+
return this.later(tab, "project write answered", () => ({ ok: true }));
|
|
374
|
+
},
|
|
375
|
+
setConnectionUrl: (id: string) => {
|
|
376
|
+
this.declared.add(connection(id));
|
|
377
|
+
return this.later(tab, "project write answered", () => ({ ok: true }));
|
|
378
|
+
},
|
|
379
|
+
failure: (error: unknown) => ({
|
|
380
|
+
detail: error instanceof Error ? error.message : String(error),
|
|
381
|
+
}),
|
|
382
|
+
databaseConnection: async (id: string) => {
|
|
383
|
+
if (!this.declared.has(id)) throw notFound();
|
|
384
|
+
return { wiring: { variable: "SHOP_URL" } };
|
|
385
|
+
},
|
|
386
|
+
databaseConnections: async () =>
|
|
387
|
+
[...this.declared].map((id) => ({ ...LISTED, id })),
|
|
388
|
+
latestAnalysis: (id: string) => {
|
|
389
|
+
const sent = this.analyses.get(id);
|
|
390
|
+
return this.later(tab, "analysis read answered", () => {
|
|
391
|
+
return sent === undefined
|
|
392
|
+
? { ok: false }
|
|
393
|
+
: { ok: true, data: { ...analysisWith(sent.open), id: sent.id } };
|
|
394
|
+
});
|
|
395
|
+
},
|
|
396
|
+
analyseDatabase: (id: string, _module: string, schemas?: string[]) => {
|
|
397
|
+
this.analyse(id, schemas);
|
|
398
|
+
return this.later(tab, "analysis request answered", () => ({
|
|
399
|
+
ok: true,
|
|
400
|
+
data: {},
|
|
401
|
+
}));
|
|
402
|
+
},
|
|
403
|
+
reloadDue: async () => ({ due: false }),
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
private analyse(id: string, schemas: string[] | undefined): void {
|
|
408
|
+
const existing = this.analyses.get(id);
|
|
409
|
+
if (schemas === undefined) {
|
|
410
|
+
this.starts.set(id, (this.starts.get(id) ?? 0) + 1);
|
|
411
|
+
this.counter += 1;
|
|
412
|
+
this.analyses.set(id, {
|
|
413
|
+
id: `job-${this.counter}`,
|
|
414
|
+
open: this.scenario.job === "asks-schemas" ? [SCHEMAS_QUESTION] : [],
|
|
415
|
+
});
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
if (existing === undefined || existing.open.length === 0) {
|
|
419
|
+
this.staleAnswers.push(
|
|
420
|
+
`the schemas question of ${id} was answered with no question open, which replaces its finished job`,
|
|
421
|
+
);
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
this.counter += 1;
|
|
425
|
+
this.analyses.set(id, { id: `job-${this.counter}`, open: [] });
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
function writtenBy(storage: Map<string, string>): string | undefined {
|
|
430
|
+
const text = storage.get(CONNECT_MEMORY_KEY);
|
|
431
|
+
if (text === undefined) return undefined;
|
|
432
|
+
const stored: unknown = JSON.parse(text);
|
|
433
|
+
if (stored === null || typeof stored !== "object") return undefined;
|
|
434
|
+
const written: unknown = Reflect.get(stored, "written");
|
|
435
|
+
if (written === null || typeof written !== "object") return undefined;
|
|
436
|
+
const connection: unknown = Reflect.get(written, "connection");
|
|
437
|
+
return typeof connection === "string" ? connection : undefined;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
function describe(step: Step): string {
|
|
441
|
+
return step.kind === "connect" ? `connect ${step.database}` : step.kind;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/** The paths a bounded search tried, and the first path that broke a rule. */
|
|
445
|
+
export interface Exploration {
|
|
446
|
+
paths: number;
|
|
447
|
+
failure?: { path: string[]; broken: string[] };
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
const DRAIN_LIMIT = 80;
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* Tries every order of the first `depth` choices a scenario offers, then finishes each path by
|
|
454
|
+
* always taking the oldest choice, until nothing is left to choose or {@link DRAIN_LIMIT} steps
|
|
455
|
+
* ran. Each path runs again from the start since the code under test cannot be copied.
|
|
456
|
+
*/
|
|
457
|
+
export async function explore(
|
|
458
|
+
scenario: Scenario,
|
|
459
|
+
depth: number,
|
|
460
|
+
): Promise<Exploration> {
|
|
461
|
+
const result: Exploration = { paths: 0 };
|
|
462
|
+
const stack: number[][] = [[]];
|
|
463
|
+
for (let prefix = stack.pop(); prefix !== undefined; prefix = stack.pop()) {
|
|
464
|
+
const world = new World(scenario);
|
|
465
|
+
const path: string[] = [];
|
|
466
|
+
let broken: string[] = [];
|
|
467
|
+
for (const choice of prefix) {
|
|
468
|
+
path.push(await world.take(choice));
|
|
469
|
+
broken = world.violations();
|
|
470
|
+
if (broken.length > 0) break;
|
|
471
|
+
}
|
|
472
|
+
let open = world.choices().length;
|
|
473
|
+
if (broken.length === 0 && prefix.length >= depth) {
|
|
474
|
+
for (let step = 0; step < DRAIN_LIMIT && open > 0; step += 1) {
|
|
475
|
+
path.push(await world.take(0));
|
|
476
|
+
broken = world.violations();
|
|
477
|
+
open = broken.length > 0 ? 0 : world.choices().length;
|
|
478
|
+
if (broken.length > 0) break;
|
|
479
|
+
}
|
|
480
|
+
open = 0;
|
|
481
|
+
}
|
|
482
|
+
await world.end();
|
|
483
|
+
if (broken.length > 0) {
|
|
484
|
+
result.paths += 1;
|
|
485
|
+
result.failure = { path, broken };
|
|
486
|
+
return result;
|
|
487
|
+
}
|
|
488
|
+
if (open === 0) {
|
|
489
|
+
result.paths += 1;
|
|
490
|
+
continue;
|
|
491
|
+
}
|
|
492
|
+
for (let choice = open - 1; choice >= 0; choice -= 1) {
|
|
493
|
+
stack.push([...prefix, choice]);
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
return result;
|
|
497
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
3
|
+
import { explore } from "./connection-analysis-world";
|
|
4
|
+
import { SCENARIOS } from "./connection-analysis-scenarios";
|
|
5
|
+
|
|
6
|
+
vi.mock("../../builder/app/runtime/i18n", async (importOriginal) => ({
|
|
7
|
+
...(await importOriginal<Record<string, unknown>>()),
|
|
8
|
+
t: (key: string) => key,
|
|
9
|
+
}));
|
|
10
|
+
|
|
11
|
+
const DEPTH = Number(process.env.FORMAL_DEPTH ?? 16);
|
|
12
|
+
const TIMEOUT_MS = 600_000;
|
|
13
|
+
|
|
14
|
+
afterEach(() => {
|
|
15
|
+
Reflect.deleteProperty(window.navigator, "locks");
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
describe("a connection's analysis across tabs", () => {
|
|
19
|
+
for (const scenario of SCENARIOS) {
|
|
20
|
+
it(scenario.name, { timeout: TIMEOUT_MS }, async () => {
|
|
21
|
+
const result = await explore(scenario, DEPTH);
|
|
22
|
+
if (process.env.FORMAL_VERBOSE !== undefined) {
|
|
23
|
+
console.log(
|
|
24
|
+
scenario.name,
|
|
25
|
+
result.paths,
|
|
26
|
+
JSON.stringify(result.failure),
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
if (scenario.defect === undefined) {
|
|
30
|
+
expect(result.failure).toBeUndefined();
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
expect(result.failure?.broken).toContain(scenario.defect);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@antelopejs/dms-assistant",
|
|
3
|
-
"version": "0.2.7-next.
|
|
3
|
+
"version": "0.2.7-next.442.gc5217b0",
|
|
4
4
|
"description": "Development assistant for the AntelopeJS DMS: a chat panel and coding agent that edit the project in reviewable, undoable change sets, and an onboarding that starts a project from scratch or from a database you already run.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ai",
|
|
@@ -95,7 +95,7 @@
|
|
|
95
95
|
}
|
|
96
96
|
},
|
|
97
97
|
"dmsFrontend": "dist/frontend-module.js",
|
|
98
|
-
"gitHead": "
|
|
98
|
+
"gitHead": "c5217b00b5ea94675a8d5249ed354485a3cbc6c7",
|
|
99
99
|
"scripts": {
|
|
100
100
|
"build": "rimraf dist && tsc",
|
|
101
101
|
"build:sidecar": "pnpm --dir sidecar run build",
|