@yaks/testing 0.0.0-stage → 0.2.36
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/LICENSE +202 -0
- package/README.md +427 -2
- package/_dist/assert.d.ts +40 -0
- package/_dist/assert.d.ts.map +1 -0
- package/_dist/choose.d.ts +39 -0
- package/_dist/choose.d.ts.map +1 -0
- package/_dist/clock.d.ts +8 -0
- package/_dist/clock.d.ts.map +1 -0
- package/_dist/deps.d.ts +113 -0
- package/_dist/deps.d.ts.map +1 -0
- package/_dist/examples.d.ts +69 -0
- package/_dist/examples.d.ts.map +1 -0
- package/_dist/find.d.ts +23 -0
- package/_dist/find.d.ts.map +1 -0
- package/_dist/kept.d.ts +27 -0
- package/_dist/kept.d.ts.map +1 -0
- package/_dist/lend.d.ts +15 -0
- package/_dist/lend.d.ts.map +1 -0
- package/_dist/main.d.ts +2 -0
- package/_dist/main.d.ts.map +1 -0
- package/_dist/mod.d.ts +45 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/names.d.ts +31 -0
- package/_dist/names.d.ts.map +1 -0
- package/_dist/own.d.ts +25 -0
- package/_dist/own.d.ts.map +1 -0
- package/_dist/reads.d.ts +8 -0
- package/_dist/reads.d.ts.map +1 -0
- package/_dist/suite.d.ts +82 -0
- package/_dist/suite.d.ts.map +1 -0
- package/_dist/typist.d.ts +24 -0
- package/_dist/typist.d.ts.map +1 -0
- package/_dist/wait.d.ts +21 -0
- package/_dist/wait.d.ts.map +1 -0
- package/assert.js +114 -0
- package/assert.js.map +1 -0
- package/assert.ts +143 -0
- package/choose.js +197 -0
- package/choose.js.map +1 -0
- package/choose.ts +327 -0
- package/clock.js +45 -0
- package/clock.js.map +1 -0
- package/clock.ts +60 -0
- package/deno.json +1 -0
- package/deps.js +868 -0
- package/deps.js.map +1 -0
- package/deps.ts +1004 -0
- package/drive.js +43 -0
- package/drive.js.map +1 -0
- package/drive.ts +41 -0
- package/examples.js +279 -0
- package/examples.js.map +1 -0
- package/examples.ts +380 -0
- package/find.js +86 -0
- package/find.js.map +1 -0
- package/find.ts +102 -0
- package/kept.js +128 -0
- package/kept.js.map +1 -0
- package/kept.ts +146 -0
- package/lend.js +30 -0
- package/lend.js.map +1 -0
- package/lend.ts +31 -0
- package/load.js +106 -0
- package/load.js.map +1 -0
- package/load.ts +139 -0
- package/main.js +214 -0
- package/main.js.map +1 -0
- package/main.ts +217 -0
- package/mod.js +45 -0
- package/mod.js.map +1 -0
- package/mod.ts +55 -0
- package/names.js +209 -0
- package/names.js.map +1 -0
- package/names.ts +204 -0
- package/own.js +109 -0
- package/own.js.map +1 -0
- package/own.ts +136 -0
- package/package.json +56 -4
- package/reads.js +88 -0
- package/reads.js.map +1 -0
- package/reads.ts +98 -0
- package/run.js +306 -0
- package/run.js.map +1 -0
- package/run.ts +405 -0
- package/suite.js +191 -0
- package/suite.js.map +1 -0
- package/suite.ts +224 -0
- package/typist.js +74 -0
- package/typist.js.map +1 -0
- package/typist.ts +79 -0
- package/wait.js +34 -0
- package/wait.js.map +1 -0
- package/wait.ts +60 -0
package/choose.ts
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
import * as io from '@yaks/file/io'
|
|
2
|
+
import { dirname } from 'node:path'
|
|
3
|
+
// Which of its files a run runs: each test module and example page is keyed
|
|
4
|
+
// by what it depends on (./deps.ts), and one whose pass is kept is left out,
|
|
5
|
+
// unless the run asks for all. A page is keyed by a module importing what its
|
|
6
|
+
// examples import, written beside it while the keys are made (./load.ts
|
|
7
|
+
// `plan`). The caller that chooses before it starts anything (bin/test.ts)
|
|
8
|
+
// hands each runtime the keys of the files it runs, so none are made twice.
|
|
9
|
+
import {
|
|
10
|
+
key,
|
|
11
|
+
passed,
|
|
12
|
+
proof,
|
|
13
|
+
READS,
|
|
14
|
+
recall,
|
|
15
|
+
shared,
|
|
16
|
+
type Survey,
|
|
17
|
+
survey,
|
|
18
|
+
versions,
|
|
19
|
+
} from './deps.js'
|
|
20
|
+
import type { Examples } from './examples.ts'
|
|
21
|
+
import { tested } from './find.js'
|
|
22
|
+
import { plan, sweep } from './load.js'
|
|
23
|
+
import { naming } from './names.js'
|
|
24
|
+
|
|
25
|
+
/** What a run chose: the key of each runtime consumer of the change, those it runs,
|
|
26
|
+
* whether those import any of `paths` (true when the graph could not say),
|
|
27
|
+
* and what a file read as this version of it last passed, where that is
|
|
28
|
+
* known: another version's reads are not this one's. */
|
|
29
|
+
export type Chosen = {
|
|
30
|
+
keys: Map<string, string>
|
|
31
|
+
run: string[]
|
|
32
|
+
affected: string[]
|
|
33
|
+
sources: string[]
|
|
34
|
+
beyond: number
|
|
35
|
+
reaches: (paths: string[]) => boolean
|
|
36
|
+
read: (file: string) => string[] | undefined
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/// let rank = (f: string) => ({ own: 0, near: 1, slow: 1, far: Infinity })[f]!
|
|
40
|
+
/// let cost = (f: string) => ({ own: 1, near: 1, slow: 5, far: 1 })[f]!
|
|
41
|
+
/// within(['far', 'slow', 'near', 'own'], rank, cost, 3) -> ['far', 'near', 'own']
|
|
42
|
+
/// within(['far', 'slow', 'near', 'own'], rank, cost, 0) -> []
|
|
43
|
+
/** Of `files`, what fits `budget`: the lowest rank first, and the cheapest
|
|
44
|
+
* first of a rank. A file that does not fit is passed over for a cheaper one
|
|
45
|
+
* behind it. Their order is kept. */
|
|
46
|
+
export let within = (
|
|
47
|
+
files: string[],
|
|
48
|
+
rank: (file: string) => number,
|
|
49
|
+
cost: (file: string) => number,
|
|
50
|
+
budget: number,
|
|
51
|
+
): string[] => {
|
|
52
|
+
let left = budget
|
|
53
|
+
let kept = new Set<string>()
|
|
54
|
+
for (let file of ranked(files, rank, cost)) {
|
|
55
|
+
if (file.cost > left) continue
|
|
56
|
+
left -= file.cost
|
|
57
|
+
kept.add(file.path)
|
|
58
|
+
}
|
|
59
|
+
return files.filter((f) => kept.has(f))
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
let ranked = (
|
|
63
|
+
files: string[],
|
|
64
|
+
rank: (file: string) => number,
|
|
65
|
+
cost: (file: string) => number,
|
|
66
|
+
) =>
|
|
67
|
+
files.map((path) => ({ path, rank: rank(path), cost: cost(path) }))
|
|
68
|
+
.sort((a, b) => a.rank == b.rank ? a.cost - b.cost : a.rank - b.rank)
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Chooses among `files`. A file runs in a runtime of some `kind`, whose files
|
|
72
|
+
* share the runtime's version, the kind's name, what `also` names and the
|
|
73
|
+
* modules of the `runner` running them, each with what loads with it
|
|
74
|
+
* (./deps.ts `shared`); when a run is `observed` its runtimes see what each
|
|
75
|
+
* file reads (./reads.ts), and a pass is kept by that too. `changed` first
|
|
76
|
+
* narrows the files to those reached by these paths, even with no cache.
|
|
77
|
+
* A narrowed run with a `budget` keeps changed files, files naming edited
|
|
78
|
+
* code directly or through re-exports, direct consumers of other runtime
|
|
79
|
+
* changes, and files that read the changed file or list its directory.
|
|
80
|
+
* Nearby and named consumers compete for the remaining time by expected cost.
|
|
81
|
+
* A change to shared runtime dependencies also keeps a sample of their files.
|
|
82
|
+
* Main's full run covers the farther consumers and files beyond the budget.
|
|
83
|
+
* Given each path's package
|
|
84
|
+
* (`owner`), the files of a changed package its distance reaches come first,
|
|
85
|
+
* followed by those importing it.
|
|
86
|
+
* When deno cannot say what a module imports, a narrowed run reports that
|
|
87
|
+
* selection is unavailable; an explicit run has no keys and runs its files.
|
|
88
|
+
*/
|
|
89
|
+
export let choose = async (
|
|
90
|
+
files: string[],
|
|
91
|
+
{
|
|
92
|
+
all = false,
|
|
93
|
+
observed = false,
|
|
94
|
+
reads: store = READS,
|
|
95
|
+
kind = () => '',
|
|
96
|
+
also = () => [],
|
|
97
|
+
runner = [],
|
|
98
|
+
sources = [],
|
|
99
|
+
changed,
|
|
100
|
+
budget,
|
|
101
|
+
cost = () => 0,
|
|
102
|
+
owner,
|
|
103
|
+
edits,
|
|
104
|
+
source = text,
|
|
105
|
+
inspect = survey,
|
|
106
|
+
parsed,
|
|
107
|
+
}: {
|
|
108
|
+
all?: boolean
|
|
109
|
+
observed?: boolean
|
|
110
|
+
/** Where what files read as they passed is kept (deps.ts `note`). */
|
|
111
|
+
reads?: string
|
|
112
|
+
kind?: (file: string) => string
|
|
113
|
+
also?: (kind: string) => string[]
|
|
114
|
+
runner?: string[]
|
|
115
|
+
sources?: string[]
|
|
116
|
+
changed?: string[]
|
|
117
|
+
budget?: number
|
|
118
|
+
cost?: (file: string) => number
|
|
119
|
+
owner?: (path: string) => string | undefined
|
|
120
|
+
edits?: Record<string, string[]>
|
|
121
|
+
source?: (path: string) => string | undefined
|
|
122
|
+
inspect?: (paths: string[]) => Promise<Survey | undefined>
|
|
123
|
+
parsed?: ReadonlyMap<string, Examples>
|
|
124
|
+
} = {},
|
|
125
|
+
): Promise<Chosen> => {
|
|
126
|
+
if (changed && !changed.length) {
|
|
127
|
+
return {
|
|
128
|
+
keys: new Map(),
|
|
129
|
+
run: [],
|
|
130
|
+
affected: [],
|
|
131
|
+
sources: [],
|
|
132
|
+
beyond: 0,
|
|
133
|
+
reaches: () => false,
|
|
134
|
+
read: () => undefined,
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
let pages = files.filter((f) => !tested(f))
|
|
138
|
+
await sweep(pages)
|
|
139
|
+
let plans = new Map(await plan(pages, parsed))
|
|
140
|
+
try {
|
|
141
|
+
let kinds = [...new Set(files.map(kind))]
|
|
142
|
+
// What each file read as it last passed, surveyed with the rest: the
|
|
143
|
+
// modules among them were imported by a computed specifier.
|
|
144
|
+
let read = observed ? recall(store) : {}
|
|
145
|
+
let known = observed ? versions(store) : {}
|
|
146
|
+
let reads = (f: string) => observed ? read[f] ?? [] : undefined
|
|
147
|
+
let s = await inspect([
|
|
148
|
+
...files.filter(tested),
|
|
149
|
+
...plans.values(),
|
|
150
|
+
...kinds.flatMap(also),
|
|
151
|
+
...runner,
|
|
152
|
+
...sources,
|
|
153
|
+
...new Set(files.flatMap((f) => reads(f) ?? [])),
|
|
154
|
+
])
|
|
155
|
+
if (!s) {
|
|
156
|
+
if (changed) {
|
|
157
|
+
throw new Error(
|
|
158
|
+
'Cannot select relevant tests: dependency graph unavailable',
|
|
159
|
+
)
|
|
160
|
+
}
|
|
161
|
+
return {
|
|
162
|
+
keys: new Map(),
|
|
163
|
+
run: files,
|
|
164
|
+
affected: files,
|
|
165
|
+
sources,
|
|
166
|
+
beyond: 0,
|
|
167
|
+
reaches: () => true,
|
|
168
|
+
read: () => undefined,
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
let keys = new Map<string, string>()
|
|
172
|
+
let common = new Map<string, Promise<string>>()
|
|
173
|
+
let basis = (k: string) => {
|
|
174
|
+
if (!common.has(k)) common.set(k, shared(s, also(k), k, runner))
|
|
175
|
+
return common.get(k)!
|
|
176
|
+
}
|
|
177
|
+
let before = all ? new Set<string>() : passed()
|
|
178
|
+
let candidates: string[] = []
|
|
179
|
+
let reached: string[] = []
|
|
180
|
+
for (let f of files) {
|
|
181
|
+
if (
|
|
182
|
+
changed &&
|
|
183
|
+
!affected(
|
|
184
|
+
s,
|
|
185
|
+
[f, plans.get(f) ?? f, ...also(kind(f))],
|
|
186
|
+
runner,
|
|
187
|
+
changed,
|
|
188
|
+
reads(f),
|
|
189
|
+
)
|
|
190
|
+
) continue
|
|
191
|
+
reached.push(f)
|
|
192
|
+
if (
|
|
193
|
+
changed && !affected(
|
|
194
|
+
s,
|
|
195
|
+
[f, plans.get(f) ?? f, ...also(kind(f))],
|
|
196
|
+
runner,
|
|
197
|
+
changed,
|
|
198
|
+
reads(f),
|
|
199
|
+
s,
|
|
200
|
+
true,
|
|
201
|
+
)
|
|
202
|
+
) continue
|
|
203
|
+
candidates.push(f)
|
|
204
|
+
}
|
|
205
|
+
let unpassed = async (f: string) => {
|
|
206
|
+
let digest = await key(
|
|
207
|
+
s,
|
|
208
|
+
await basis(kind(f)),
|
|
209
|
+
plans.get(f) ?? f,
|
|
210
|
+
f,
|
|
211
|
+
true,
|
|
212
|
+
)
|
|
213
|
+
keys.set(f, digest)
|
|
214
|
+
return all || !before.has(await proof(s, digest, reads(f), true))
|
|
215
|
+
}
|
|
216
|
+
let root = (f: string) => plans.get(f) ?? f
|
|
217
|
+
let kept = new Set<string>()
|
|
218
|
+
let beyond = 0
|
|
219
|
+
if (changed && budget != null) {
|
|
220
|
+
let info = edits ? await s.info(candidates.map(root)) : undefined
|
|
221
|
+
let named = info && edits
|
|
222
|
+
? naming(info, candidates, edits, source, root)
|
|
223
|
+
: new Map<string, number>()
|
|
224
|
+
// A module edited only inside its declarations reaches the tests
|
|
225
|
+
// naming them (`named`); any other change reaches every importer.
|
|
226
|
+
let direct = s.distance(
|
|
227
|
+
changed.filter((p) => !edits?.[p] || edits[p].includes('*')),
|
|
228
|
+
)
|
|
229
|
+
let dependency = s.distance(changed)
|
|
230
|
+
let far = s.distance(changed, owner)
|
|
231
|
+
let touched = new Set(changed)
|
|
232
|
+
let directories = new Set(changed.map((file) => dirname(file)))
|
|
233
|
+
let required = new Set(
|
|
234
|
+
candidates.filter((f) =>
|
|
235
|
+
touched.has(f) || direct(root(f)) <= 1 || named.get(f) == 0 ||
|
|
236
|
+
reads(f)?.some((p) => dependency(p) == 0 || directories.has(p))
|
|
237
|
+
),
|
|
238
|
+
)
|
|
239
|
+
let rank = (f: string) =>
|
|
240
|
+
named.has(f) ? -1 / (1 + named.get(f)!) : (reads(f) ?? []).reduce(
|
|
241
|
+
(nearest, path) => Math.min(nearest, dependency(path)),
|
|
242
|
+
Math.min(far(f), far(root(f))),
|
|
243
|
+
)
|
|
244
|
+
let left = budget
|
|
245
|
+
for (let f of required) {
|
|
246
|
+
if (!await unpassed(f)) continue
|
|
247
|
+
kept.add(f)
|
|
248
|
+
left -= cost(f)
|
|
249
|
+
}
|
|
250
|
+
let rest = candidates.filter((f) => !required.has(f))
|
|
251
|
+
let sharedChanges = new Set(
|
|
252
|
+
kinds.filter((k) =>
|
|
253
|
+
s.reaches(also(k), changed, false, true) ||
|
|
254
|
+
s.reaches(runner, changed, true)
|
|
255
|
+
),
|
|
256
|
+
)
|
|
257
|
+
for (let file of ranked(rest, rank, cost)) {
|
|
258
|
+
// A whole platform can load edited code without exercising it.
|
|
259
|
+
// Keep tests reaching that code by name or a nearby import.
|
|
260
|
+
if (
|
|
261
|
+
file.cost > Math.max(0, left) ||
|
|
262
|
+
file.rank == Infinity && !sharedChanges.has(kind(file.path))
|
|
263
|
+
) {
|
|
264
|
+
beyond++
|
|
265
|
+
continue
|
|
266
|
+
}
|
|
267
|
+
if (!await unpassed(file.path)) continue
|
|
268
|
+
kept.add(file.path)
|
|
269
|
+
left -= file.cost
|
|
270
|
+
}
|
|
271
|
+
} else {
|
|
272
|
+
for (let f of candidates) if (await unpassed(f)) kept.add(f)
|
|
273
|
+
}
|
|
274
|
+
let run = candidates.filter((f) => kept.has(f))
|
|
275
|
+
let typed = sources.filter((f) => !changed || s.reaches([f], changed))
|
|
276
|
+
let roots = [...run.map(root), ...typed]
|
|
277
|
+
return {
|
|
278
|
+
keys,
|
|
279
|
+
run,
|
|
280
|
+
affected: reached,
|
|
281
|
+
sources: typed,
|
|
282
|
+
beyond,
|
|
283
|
+
reaches: (paths) => s.reaches(roots, paths),
|
|
284
|
+
read: (f) => observed && known[f] == keys.get(f) ? read[f] : undefined,
|
|
285
|
+
}
|
|
286
|
+
} finally {
|
|
287
|
+
await Promise.all(
|
|
288
|
+
[...plans.values()].map((p) => io.remove(p).catch(() => {})),
|
|
289
|
+
)
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
let text = (path: string) => {
|
|
294
|
+
try {
|
|
295
|
+
return io.readTextFileSync(path)
|
|
296
|
+
} catch {
|
|
297
|
+
return undefined
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** Whether a change reaches a file, its shared dependencies or its observed
|
|
302
|
+
* reads. Runner dependencies count only when they load unconditionally. */
|
|
303
|
+
export let affected = (
|
|
304
|
+
s: Survey,
|
|
305
|
+
roots: string[],
|
|
306
|
+
runner: string[],
|
|
307
|
+
changed: string[],
|
|
308
|
+
reads: string[] = [],
|
|
309
|
+
observed: Survey = s,
|
|
310
|
+
runtime = false,
|
|
311
|
+
): boolean =>
|
|
312
|
+
s.reaches(roots, changed, false, runtime) ||
|
|
313
|
+
s.reaches(runner, changed, true) ||
|
|
314
|
+
readChange(observed, reads, changed, runtime)
|
|
315
|
+
|
|
316
|
+
let inDir = (dir: string, file: string) =>
|
|
317
|
+
dir == '.' ? !file.includes('/') : file.startsWith(`${dir}/`)
|
|
318
|
+
|
|
319
|
+
let readChange = (
|
|
320
|
+
s: Survey,
|
|
321
|
+
reads: string[],
|
|
322
|
+
changed: string[],
|
|
323
|
+
runtime: boolean,
|
|
324
|
+
): boolean =>
|
|
325
|
+
reads.length > 0 &&
|
|
326
|
+
(s.reaches(reads, changed, false, runtime) ||
|
|
327
|
+
reads.some((path) => changed.some((file) => inDir(path, file))))
|
package/clock.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// A virtual clock runs scheduled callbacks only when a test advances time.
|
|
2
|
+
// Consumers inject its operations at their timer boundary.
|
|
3
|
+
import { setImmediate } from 'node:timers/promises';
|
|
4
|
+
/** Create a virtual clock at a chosen time, in milliseconds. */ export let clock = (start = 0)=>{
|
|
5
|
+
let time = start;
|
|
6
|
+
let next = 0;
|
|
7
|
+
let pending = new Map();
|
|
8
|
+
return {
|
|
9
|
+
now: ()=>time,
|
|
10
|
+
after: (ms, callback)=>{
|
|
11
|
+
let id = next++;
|
|
12
|
+
pending.set(id, {
|
|
13
|
+
at: time + Math.max(0, ms),
|
|
14
|
+
callback
|
|
15
|
+
});
|
|
16
|
+
return ()=>{
|
|
17
|
+
pending.delete(id);
|
|
18
|
+
};
|
|
19
|
+
},
|
|
20
|
+
advance: async (ms)=>{
|
|
21
|
+
if (!Number.isFinite(ms) || ms < 0) {
|
|
22
|
+
throw new RangeError('clock: advance requires a finite, nonnegative span');
|
|
23
|
+
}
|
|
24
|
+
// What the caller queued runs first, as it would before the runtime's
|
|
25
|
+
// next timer: code that awaits on its way to setting a timer sets it.
|
|
26
|
+
// `setImmediate` turns the event loop without waiting on a clock.
|
|
27
|
+
await setImmediate();
|
|
28
|
+
let end = time + ms;
|
|
29
|
+
while(true){
|
|
30
|
+
let due;
|
|
31
|
+
for (let [id, timer] of pending){
|
|
32
|
+
if (timer.at <= end && (due === undefined || timer.at < pending.get(due).at)) due = id;
|
|
33
|
+
}
|
|
34
|
+
if (due === undefined) break;
|
|
35
|
+
let timer = pending.get(due);
|
|
36
|
+
pending.delete(due);
|
|
37
|
+
time = timer.at;
|
|
38
|
+
await timer.callback();
|
|
39
|
+
}
|
|
40
|
+
time = end;
|
|
41
|
+
},
|
|
42
|
+
pending: ()=>pending.size
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
//# sourceMappingURL=clock.js.map
|
package/clock.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clock.js","sources":["./clock.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,2DAA2D;AAC3D,SAAS,YAAY,QAAQ,uBAAsB;AAWnD,8DAA8D,GAC9D,OAAO,IAAI,QAAQ,CAAC,QAAQ,CAAC;EAC3B,IAAI,OAAO;EACX,IAAI,OAAO;EACX,IAAI,UAAU,IAAI;EAIlB,OAAO;IACL,KAAK,IAAM;IACX,OAAO,CAAC,IAAI;MACV,IAAI,KAAK;MACT,QAAQ,GAAG,CAAC,IAAI;QAAE,IAAI,OAAO,KAAK,GAAG,CAAC,GAAG;QAAK;MAAS;MACvD,OAAO;QACL,QAAQ,MAAM,CAAC;MACjB;IACF;IACA,SAAS,OAAO;MACd,IAAI,CAAC,OAAO,QAAQ,CAAC,OAAO,KAAK,GAAG;QAClC,MAAM,IAAI,WACR;MAEJ;MACA,sEAAsE;MACtE,sEAAsE;MACtE,kEAAkE;MAClE,MAAM;MACN,IAAI,MAAM,OAAO;MACjB,MAAO,KAAM;QACX,IAAI;QACJ,KAAK,IAAI,CAAC,IAAI,MAAM,IAAI,QAAS;UAC/B,IACE,MAAM,EAAE,IAAI,OACZ,CAAC,QAAQ,aAAa,MAAM,EAAE,GAAG,QAAQ,GAAG,CAAC,KAAM,EAAE,GACrD,MAAM;QACV;QACA,IAAI,QAAQ,WAAW;QACvB,IAAI,QAAQ,QAAQ,GAAG,CAAC;QACxB,QAAQ,MAAM,CAAC;QACf,OAAO,MAAM,EAAE;QACf,MAAM,MAAM,QAAQ;MACtB;MACA,OAAO;IACT;IACA,SAAS,IAAM,QAAQ,IAAI;EAC7B;AACF,EAAC"}
|
package/clock.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// A virtual clock runs scheduled callbacks only when a test advances time.
|
|
2
|
+
// Consumers inject its operations at their timer boundary.
|
|
3
|
+
import { setImmediate } from 'node:timers/promises'
|
|
4
|
+
|
|
5
|
+
/** A clock whose time and scheduled callbacks are controlled by the caller. */
|
|
6
|
+
export type Clock = {
|
|
7
|
+
now: () => number
|
|
8
|
+
after: (ms: number, callback: () => void | Promise<void>) => () => void
|
|
9
|
+
advance: (ms: number) => Promise<void>
|
|
10
|
+
/** How many callbacks are set and have neither run nor been cancelled. */
|
|
11
|
+
pending: () => number
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Create a virtual clock at a chosen time, in milliseconds. */
|
|
15
|
+
export let clock = (start = 0): Clock => {
|
|
16
|
+
let time = start
|
|
17
|
+
let next = 0
|
|
18
|
+
let pending = new Map<number, {
|
|
19
|
+
at: number
|
|
20
|
+
callback: () => void | Promise<void>
|
|
21
|
+
}>()
|
|
22
|
+
return {
|
|
23
|
+
now: () => time,
|
|
24
|
+
after: (ms, callback) => {
|
|
25
|
+
let id = next++
|
|
26
|
+
pending.set(id, { at: time + Math.max(0, ms), callback })
|
|
27
|
+
return () => {
|
|
28
|
+
pending.delete(id)
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
advance: async (ms) => {
|
|
32
|
+
if (!Number.isFinite(ms) || ms < 0) {
|
|
33
|
+
throw new RangeError(
|
|
34
|
+
'clock: advance requires a finite, nonnegative span',
|
|
35
|
+
)
|
|
36
|
+
}
|
|
37
|
+
// What the caller queued runs first, as it would before the runtime's
|
|
38
|
+
// next timer: code that awaits on its way to setting a timer sets it.
|
|
39
|
+
// `setImmediate` turns the event loop without waiting on a clock.
|
|
40
|
+
await setImmediate()
|
|
41
|
+
let end = time + ms
|
|
42
|
+
while (true) {
|
|
43
|
+
let due: number | undefined
|
|
44
|
+
for (let [id, timer] of pending) {
|
|
45
|
+
if (
|
|
46
|
+
timer.at <= end &&
|
|
47
|
+
(due === undefined || timer.at < pending.get(due)!.at)
|
|
48
|
+
) due = id
|
|
49
|
+
}
|
|
50
|
+
if (due === undefined) break
|
|
51
|
+
let timer = pending.get(due)!
|
|
52
|
+
pending.delete(due)
|
|
53
|
+
time = timer.at
|
|
54
|
+
await timer.callback()
|
|
55
|
+
}
|
|
56
|
+
time = end
|
|
57
|
+
},
|
|
58
|
+
pending: () => pending.size,
|
|
59
|
+
}
|
|
60
|
+
}
|
package/deno.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"publish":{"exclude":["**/*_test.ts"]},"name":"@yaks/testing","version":"0.2.36","license":"Apache-2.0","description":"Declare tests, check their results, and run test modules and examples in one runtime. The runner skips files whose dependencies have not changed since they passed.","exports":{".":"./mod.ts","./choose":"./choose.ts","./deps":"./deps.ts","./examples":"./examples.ts","./find":"./find.ts","./kept":"./kept.ts","./main":"./main.ts","./names":"./names.ts","./own":"./own.ts","./reads":"./reads.ts"},"imports":{"@yaks/testing":"./mod.ts","@yaks/testing/choose":"./choose.ts","@yaks/testing/deps":"./deps.ts","@yaks/testing/examples":"./examples.ts","@yaks/testing/find":"./find.ts","@yaks/testing/kept":"./kept.ts","@yaks/testing/main":"./main.ts","@yaks/testing/names":"./names.ts","@yaks/testing/own":"./own.ts","@yaks/testing/reads":"./reads.ts"}}
|