@link-foundation/ui-animation 0.0.0-stage → 0.1.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/LICENSE +24 -0
- package/README.md +454 -2
- package/package.json +77 -4
- package/src/backgrounds/README.md +131 -0
- package/src/backgrounds/beams.d.ts +21 -0
- package/src/backgrounds/beams.js +140 -0
- package/src/backgrounds/common.d.ts +23 -0
- package/src/backgrounds/common.js +322 -0
- package/src/backgrounds/grid.d.ts +16 -0
- package/src/backgrounds/grid.js +122 -0
- package/src/backgrounds/nebula.d.ts +9 -0
- package/src/backgrounds/nebula.js +57 -0
- package/src/backgrounds/rain-cleanup.js +77 -0
- package/src/backgrounds/rain.d.ts +33 -0
- package/src/backgrounds/rain.js +238 -0
- package/src/backgrounds/shader.d.ts +14 -0
- package/src/backgrounds/shader.js +151 -0
- package/src/backgrounds/sprites.js +72 -0
- package/src/backgrounds/starfield.d.ts +38 -0
- package/src/backgrounds/starfield.js +168 -0
- package/src/clock.js +98 -0
- package/src/compile-waapi.js +147 -0
- package/src/diagnostics.js +51 -0
- package/src/easing.js +327 -0
- package/src/effects/decipher.js +83 -0
- package/src/effects.js +44 -0
- package/src/index.d.ts +581 -0
- package/src/index.js +74 -0
- package/src/layers.js +88 -0
- package/src/model.js +636 -0
- package/src/player.js +267 -0
- package/src/random.js +62 -0
- package/src/recorder/buffer.js +40 -0
- package/src/recorder/clock.js +2 -0
- package/src/recorder/format.js +125 -0
- package/src/recorder/input.js +53 -0
- package/src/recorder/recorder.js +183 -0
- package/src/recorder/redact.js +43 -0
- package/src/recorder/replay.js +115 -0
- package/src/recorder/state-at.js +62 -0
- package/src/recorder/store.js +92 -0
- package/src/recorder/to-playwright.js +113 -0
- package/src/recorder/verify.js +49 -0
- package/src/run.js +283 -0
- package/src/sample.js +160 -0
- package/src/timeline.js +115 -0
- package/src/timing.js +172 -0
- package/src/tokens.js +32 -0
- package/src/types.js +130 -0
- package/src/validate.js +218 -0
- package/src/values.js +331 -0
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { systemClock } from '../clock.js';
|
|
2
|
+
import { serializeRecording } from './format.js';
|
|
3
|
+
import { INPUT_EVENTS, inputEvent } from './input.js';
|
|
4
|
+
import {
|
|
5
|
+
redactNode,
|
|
6
|
+
redactOperation,
|
|
7
|
+
sensitiveNode,
|
|
8
|
+
sensitiveTarget,
|
|
9
|
+
} from './redact.js';
|
|
10
|
+
import { operationEvent, snapshotStore } from './store.js';
|
|
11
|
+
import { createEventBuffer } from './buffer.js';
|
|
12
|
+
|
|
13
|
+
function initialHeader(store, root, options, snapshot) {
|
|
14
|
+
const view = root?.ownerDocument?.defaultView;
|
|
15
|
+
return {
|
|
16
|
+
id: 'recording-1',
|
|
17
|
+
started: new Date(options.started ?? Date.now()).toISOString(),
|
|
18
|
+
seed: options.seed ?? 0,
|
|
19
|
+
viewport: options.viewport ?? [
|
|
20
|
+
view?.innerWidth ?? 0,
|
|
21
|
+
view?.innerHeight ?? 0,
|
|
22
|
+
],
|
|
23
|
+
document: options.document ?? store.roots()[0] ?? 'app',
|
|
24
|
+
snapshot,
|
|
25
|
+
redacted: options.redact !== false,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function playerEvent(event, time) {
|
|
29
|
+
return {
|
|
30
|
+
time,
|
|
31
|
+
kind: event.kind,
|
|
32
|
+
args: [
|
|
33
|
+
event.id,
|
|
34
|
+
['run', String(event.run)],
|
|
35
|
+
...Object.entries(event.options)
|
|
36
|
+
.filter(([, value]) => value !== undefined)
|
|
37
|
+
.map(([key, value]) => [key, String(value)]),
|
|
38
|
+
],
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Capture a session. start() begins a fresh session; stop() detaches every listener. */
|
|
43
|
+
export function createRecorder(options) {
|
|
44
|
+
const {
|
|
45
|
+
store,
|
|
46
|
+
player,
|
|
47
|
+
root,
|
|
48
|
+
handle,
|
|
49
|
+
clock = systemClock(),
|
|
50
|
+
redact = true,
|
|
51
|
+
maxEvents = 100000,
|
|
52
|
+
} = options;
|
|
53
|
+
let buffer = createEventBuffer(maxEvents);
|
|
54
|
+
let recording;
|
|
55
|
+
let origin;
|
|
56
|
+
let active = false;
|
|
57
|
+
let cleanups = [];
|
|
58
|
+
let lastPointerBucket = -1;
|
|
59
|
+
let pointerTimes = [];
|
|
60
|
+
let sensitiveIds = new Set();
|
|
61
|
+
const isSensitive = (id) => {
|
|
62
|
+
for (
|
|
63
|
+
let current = id;
|
|
64
|
+
current !== null && current !== undefined && store.has(current);
|
|
65
|
+
current = store.parentOf(current)
|
|
66
|
+
) {
|
|
67
|
+
if (sensitiveIds.has(current) || sensitiveNode(store.get(current))) {
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return false;
|
|
72
|
+
};
|
|
73
|
+
const time = () => Math.max(0, Math.floor(clock.now() - origin));
|
|
74
|
+
const append = (event) => {
|
|
75
|
+
if (active) {
|
|
76
|
+
buffer.append(event);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
const capture = (event) => {
|
|
80
|
+
if (redact && sensitiveTarget(event.target)) {
|
|
81
|
+
sensitiveIds.add(handle?.idFor(event.target));
|
|
82
|
+
}
|
|
83
|
+
const recorded = inputEvent(
|
|
84
|
+
event,
|
|
85
|
+
{ handle, store, redact, isSensitive },
|
|
86
|
+
time()
|
|
87
|
+
);
|
|
88
|
+
if (!recorded) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if (recorded.args[0] === 'pointer' && recorded.args[1] === 'move') {
|
|
92
|
+
const bucket = Math.floor((recorded.time * 60) / 1000);
|
|
93
|
+
if (lastPointerBucket === bucket) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
while (pointerTimes.length && recorded.time - pointerTimes[0] >= 1000) {
|
|
97
|
+
pointerTimes.shift();
|
|
98
|
+
}
|
|
99
|
+
if (pointerTimes.length === 60) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
lastPointerBucket = bucket;
|
|
103
|
+
pointerTimes.push(recorded.time);
|
|
104
|
+
}
|
|
105
|
+
append(recorded);
|
|
106
|
+
};
|
|
107
|
+
const stopListeners = () => {
|
|
108
|
+
cleanups.forEach((fn) => fn());
|
|
109
|
+
cleanups = [];
|
|
110
|
+
active = false;
|
|
111
|
+
};
|
|
112
|
+
return {
|
|
113
|
+
start() {
|
|
114
|
+
stopListeners();
|
|
115
|
+
origin = clock.now();
|
|
116
|
+
buffer = createEventBuffer(maxEvents);
|
|
117
|
+
lastPointerBucket = -1;
|
|
118
|
+
pointerTimes = [];
|
|
119
|
+
sensitiveIds = new Set();
|
|
120
|
+
for (const node of root?.querySelectorAll?.(
|
|
121
|
+
'[type="password"], [sensitive="true"], [data-sensitive="true"]'
|
|
122
|
+
) ?? []) {
|
|
123
|
+
sensitiveIds.add(handle?.idFor(node));
|
|
124
|
+
}
|
|
125
|
+
const snapshot = snapshotStore(
|
|
126
|
+
store,
|
|
127
|
+
redact ? (node, id) => redactNode(node, isSensitive(id)) : undefined
|
|
128
|
+
);
|
|
129
|
+
recording = initialHeader(store, root, options, snapshot);
|
|
130
|
+
active = true;
|
|
131
|
+
cleanups.push(
|
|
132
|
+
store.subscribe((events, entry) => {
|
|
133
|
+
for (const event of events) {
|
|
134
|
+
if (sensitiveNode({ properties: event.props })) {
|
|
135
|
+
sensitiveIds.add(event.id);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
for (const op of entry.ops) {
|
|
139
|
+
append(
|
|
140
|
+
operationEvent(
|
|
141
|
+
redact
|
|
142
|
+
? redactOperation(op, isSensitive(op.id ?? op.parentId))
|
|
143
|
+
: op,
|
|
144
|
+
time()
|
|
145
|
+
)
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
})
|
|
149
|
+
);
|
|
150
|
+
if (player) {
|
|
151
|
+
cleanups.push(
|
|
152
|
+
player.subscribe((event) => append(playerEvent(event, time())))
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
if (root) {
|
|
156
|
+
for (const name of INPUT_EVENTS) {
|
|
157
|
+
root.addEventListener(name, capture, {
|
|
158
|
+
capture: true,
|
|
159
|
+
passive: true,
|
|
160
|
+
});
|
|
161
|
+
cleanups.push(() =>
|
|
162
|
+
root.removeEventListener(name, capture, { capture: true })
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
stop() {
|
|
168
|
+
if (!recording) {
|
|
169
|
+
throw new Error('Recorder has not started');
|
|
170
|
+
}
|
|
171
|
+
stopListeners();
|
|
172
|
+
recording.events = buffer.values();
|
|
173
|
+
if (buffer.truncated) {
|
|
174
|
+
recording.truncated = true;
|
|
175
|
+
}
|
|
176
|
+
return serializeRecording(recording);
|
|
177
|
+
},
|
|
178
|
+
mark(label) {
|
|
179
|
+
append({ time: time(), kind: 'mark', args: [String(label)] });
|
|
180
|
+
},
|
|
181
|
+
size: () => buffer.size,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** Redact values, while retaining field definitions and structural operations. */
|
|
2
|
+
export function sensitiveNode(node) {
|
|
3
|
+
return (
|
|
4
|
+
node?.properties?.some(
|
|
5
|
+
(p) =>
|
|
6
|
+
(p.name === 'type' && p.values[0] === 'password') ||
|
|
7
|
+
(p.name === 'sensitive' && p.values[0] === 'true')
|
|
8
|
+
) ?? false
|
|
9
|
+
);
|
|
10
|
+
}
|
|
11
|
+
export function sensitiveTarget(target) {
|
|
12
|
+
const boundary = target?.closest?.(
|
|
13
|
+
'[sensitive="true"], [data-sensitive="true"], [type="password"]'
|
|
14
|
+
);
|
|
15
|
+
return target?.type === 'password' || Boolean(boundary);
|
|
16
|
+
}
|
|
17
|
+
export function redactNode(node, inherited = false) {
|
|
18
|
+
const sensitive = inherited || sensitiveNode(node);
|
|
19
|
+
return {
|
|
20
|
+
...node,
|
|
21
|
+
positional: sensitive ? node.positional.map(() => '***') : node.positional,
|
|
22
|
+
properties: node.properties.map((p) =>
|
|
23
|
+
sensitive && !['type', 'sensitive'].includes(p.name)
|
|
24
|
+
? { ...p, values: ['***'] }
|
|
25
|
+
: p
|
|
26
|
+
),
|
|
27
|
+
unknown: sensitive ? [] : node.unknown,
|
|
28
|
+
children: node.children.map((child) => redactNode(child, sensitive)),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
export function redactOperation(op, sensitive) {
|
|
32
|
+
if (op.op === 'insert') {
|
|
33
|
+
return { ...op, node: redactNode(op.node, sensitive) };
|
|
34
|
+
}
|
|
35
|
+
if (
|
|
36
|
+
sensitive &&
|
|
37
|
+
['setProp', 'setPositional'].includes(op.op) &&
|
|
38
|
+
!['type', 'sensitive'].includes(op.name)
|
|
39
|
+
) {
|
|
40
|
+
return { ...op, values: ['***'] };
|
|
41
|
+
}
|
|
42
|
+
return op;
|
|
43
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { systemClock } from '../clock.js';
|
|
2
|
+
import { asRecording } from './format.js';
|
|
3
|
+
import { animationsAt, eventOptions, stateAt } from './state-at.js';
|
|
4
|
+
import { applyEvent, restoreStore } from './store.js';
|
|
5
|
+
|
|
6
|
+
/** Playback restores state from the snapshot; input handlers are never dispatched. */
|
|
7
|
+
export function replay(source, { store, player, clock = systemClock() }) {
|
|
8
|
+
const recording = asRecording(source);
|
|
9
|
+
if (recording.truncated) {
|
|
10
|
+
throw new Error(
|
|
11
|
+
'Cannot replay a truncated recording: initial events are missing'
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
const duration = recording.events.at(-1)?.time ?? 0;
|
|
15
|
+
let time = 0;
|
|
16
|
+
let started = 0;
|
|
17
|
+
let running = false;
|
|
18
|
+
let timers = [];
|
|
19
|
+
const controls = new Map();
|
|
20
|
+
let resolvePlay;
|
|
21
|
+
const addRun = (run) => {
|
|
22
|
+
if (!player) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const control = player.play(run.id, {
|
|
26
|
+
target: run.target,
|
|
27
|
+
input: run.input,
|
|
28
|
+
seed: run.seed,
|
|
29
|
+
});
|
|
30
|
+
control.pause();
|
|
31
|
+
control.seek(run.time);
|
|
32
|
+
if (running) {
|
|
33
|
+
control.play();
|
|
34
|
+
}
|
|
35
|
+
controls.set(run.run ?? run.id, control);
|
|
36
|
+
};
|
|
37
|
+
const render = (ms) => {
|
|
38
|
+
controls.forEach((c) => c.cancel());
|
|
39
|
+
controls.clear();
|
|
40
|
+
time = Math.max(0, Math.min(duration, ms));
|
|
41
|
+
restoreStore(store, stateAt(recording, time));
|
|
42
|
+
for (const run of animationsAt(recording, time)) {
|
|
43
|
+
addRun(run);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
const pause = () => {
|
|
47
|
+
if (running) {
|
|
48
|
+
time = Math.min(duration, started + clock.now());
|
|
49
|
+
}
|
|
50
|
+
running = false;
|
|
51
|
+
timers.forEach((timer) => clock.clearTimeout(timer));
|
|
52
|
+
timers = [];
|
|
53
|
+
controls.forEach((c) => c.pause());
|
|
54
|
+
resolvePlay?.();
|
|
55
|
+
resolvePlay = null;
|
|
56
|
+
};
|
|
57
|
+
const apply = (event) => {
|
|
58
|
+
time = event.time;
|
|
59
|
+
store.transaction('Replay event', () => applyEvent(store, event), {
|
|
60
|
+
origin: 'replay',
|
|
61
|
+
});
|
|
62
|
+
const options = eventOptions(event);
|
|
63
|
+
if (event.kind === 'play') {
|
|
64
|
+
addRun({
|
|
65
|
+
id: event.args[0],
|
|
66
|
+
time: 0,
|
|
67
|
+
...options,
|
|
68
|
+
seed: options.seed ?? recording.seed,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
if (event.kind === 'cancel') {
|
|
72
|
+
const key = options.run ?? event.args[0];
|
|
73
|
+
controls.get(key)?.cancel();
|
|
74
|
+
controls.delete(key);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
const api = {
|
|
78
|
+
get time() {
|
|
79
|
+
return running ? Math.min(duration, started + clock.now()) : time;
|
|
80
|
+
},
|
|
81
|
+
duration,
|
|
82
|
+
pause,
|
|
83
|
+
seek(ms) {
|
|
84
|
+
if (!Number.isFinite(ms)) {
|
|
85
|
+
throw new RangeError('Seek time must be finite');
|
|
86
|
+
}
|
|
87
|
+
pause();
|
|
88
|
+
render(ms);
|
|
89
|
+
},
|
|
90
|
+
play() {
|
|
91
|
+
pause();
|
|
92
|
+
render(time);
|
|
93
|
+
running = true;
|
|
94
|
+
started = time - clock.now();
|
|
95
|
+
controls.forEach((c) => c.play());
|
|
96
|
+
const done = new Promise((resolve) => {
|
|
97
|
+
resolvePlay = resolve;
|
|
98
|
+
});
|
|
99
|
+
for (const event of recording.events) {
|
|
100
|
+
if (event.time > time) {
|
|
101
|
+
timers.push(clock.setTimeout(() => apply(event), event.time - time));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
timers.push(
|
|
105
|
+
clock.setTimeout(() => {
|
|
106
|
+
time = duration;
|
|
107
|
+
pause();
|
|
108
|
+
}, duration - time)
|
|
109
|
+
);
|
|
110
|
+
return done;
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
render(0);
|
|
114
|
+
return api;
|
|
115
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { asRecording } from './format.js';
|
|
2
|
+
import { applyEvent, snapshotStore, storeFromSnapshot } from './store.js';
|
|
3
|
+
import { endTime } from '../timing.js';
|
|
4
|
+
import { flattenTimeline } from '../timeline.js';
|
|
5
|
+
|
|
6
|
+
/** Pure: returns a LiNo snapshot, without mutating the recording or any caller store. */
|
|
7
|
+
export function stateAt(source, tMs) {
|
|
8
|
+
const recording = asRecording(source);
|
|
9
|
+
const store = storeFromSnapshot(recording.snapshot);
|
|
10
|
+
for (const event of recording.events) {
|
|
11
|
+
if (event.time <= tMs) {
|
|
12
|
+
applyEvent(store, event);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return snapshotStore(store);
|
|
16
|
+
}
|
|
17
|
+
export const eventOptions = (event) =>
|
|
18
|
+
Object.fromEntries(
|
|
19
|
+
event.args
|
|
20
|
+
.slice(1)
|
|
21
|
+
.filter(Array.isArray)
|
|
22
|
+
.map(([key, value]) => [
|
|
23
|
+
key,
|
|
24
|
+
key === 'seed' && Number.isFinite(Number(value))
|
|
25
|
+
? Number(value)
|
|
26
|
+
: value,
|
|
27
|
+
])
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
/** Active play invocations, with local time; supply a model to exclude completed runs. */
|
|
31
|
+
export function animationsAt(source, tMs, { model, resolveToken } = {}) {
|
|
32
|
+
const recording = asRecording(source);
|
|
33
|
+
const runs = new Map();
|
|
34
|
+
for (const event of recording.events) {
|
|
35
|
+
if (event.time > tMs) {
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
const options = eventOptions(event);
|
|
39
|
+
const key = options.run ?? event.args[0];
|
|
40
|
+
if (event.kind === 'play') {
|
|
41
|
+
runs.set(key, {
|
|
42
|
+
id: event.args[0],
|
|
43
|
+
time: tMs - event.time,
|
|
44
|
+
started: event.time,
|
|
45
|
+
...options,
|
|
46
|
+
seed: options.seed ?? recording.seed,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
if (event.kind === 'cancel') {
|
|
50
|
+
runs.delete(key);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return [...runs.values()].filter(
|
|
54
|
+
(run) =>
|
|
55
|
+
!model ||
|
|
56
|
+
flattenTimeline(model, run.id, { target: run.target, resolveToken }).some(
|
|
57
|
+
(entry) =>
|
|
58
|
+
run.time <
|
|
59
|
+
entry.start + endTime(model.animations.get(entry.id), resolveToken)
|
|
60
|
+
)
|
|
61
|
+
);
|
|
62
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/** Adapt the merged store's committed operation log; never depend on event internals. */
|
|
2
|
+
import { createStore, logFromLino, logToLino } from '@link-foundation/ui-store';
|
|
3
|
+
import { formatLinks, parseLinks } from '@link-foundation/ui-core';
|
|
4
|
+
|
|
5
|
+
const idsOf = (store, id) => [
|
|
6
|
+
id,
|
|
7
|
+
...store.children(id).flatMap((child) => idsOf(store, child)),
|
|
8
|
+
];
|
|
9
|
+
export function snapshotStore(store, mapNode = (node) => node) {
|
|
10
|
+
const mapTree = (node, id) =>
|
|
11
|
+
mapNode(
|
|
12
|
+
{
|
|
13
|
+
...node,
|
|
14
|
+
children: node.children.map((child, index) =>
|
|
15
|
+
mapTree(child, store.children(id)[index])
|
|
16
|
+
),
|
|
17
|
+
},
|
|
18
|
+
id
|
|
19
|
+
);
|
|
20
|
+
const source = store.toDocument();
|
|
21
|
+
const document = {
|
|
22
|
+
...source,
|
|
23
|
+
roots: source.roots.map((node, index) =>
|
|
24
|
+
mapTree(node, store.roots()[index])
|
|
25
|
+
),
|
|
26
|
+
};
|
|
27
|
+
const ops = document.roots.map((node, index) => ({
|
|
28
|
+
op: 'insert',
|
|
29
|
+
parentId: null,
|
|
30
|
+
index,
|
|
31
|
+
node,
|
|
32
|
+
ids: idsOf(store, store.roots()[index]),
|
|
33
|
+
}));
|
|
34
|
+
return ops.length
|
|
35
|
+
? logToLino([{ tx: 1, origin: 'recording', label: null, ops }])
|
|
36
|
+
: '';
|
|
37
|
+
}
|
|
38
|
+
export function restoreStore(store, snapshot = '') {
|
|
39
|
+
store.transaction(
|
|
40
|
+
'Replay snapshot',
|
|
41
|
+
() => {
|
|
42
|
+
for (const id of store.roots()) {
|
|
43
|
+
store.remove(id);
|
|
44
|
+
}
|
|
45
|
+
for (const entry of logFromLino(snapshot)) {
|
|
46
|
+
for (const op of entry.ops) {
|
|
47
|
+
store.apply(op);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
{ origin: 'replay' }
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
export function storeFromSnapshot(snapshot) {
|
|
55
|
+
const store = createStore();
|
|
56
|
+
restoreStore(store, snapshot);
|
|
57
|
+
return store;
|
|
58
|
+
}
|
|
59
|
+
export function operationEvent(op, time) {
|
|
60
|
+
if (op.op === 'setProp' && op.index === undefined) {
|
|
61
|
+
return { time, kind: 'set', args: [op.id, op.name, ...op.values] };
|
|
62
|
+
}
|
|
63
|
+
const raw = parseLinks(
|
|
64
|
+
logToLino([{ tx: 1, origin: 'recording', label: null, ops: [op] }])
|
|
65
|
+
)[0];
|
|
66
|
+
const encoded = raw.find(
|
|
67
|
+
(part) => Array.isArray(part) && !['origin', 'label'].includes(part[0])
|
|
68
|
+
);
|
|
69
|
+
const kind = ['insert', 'remove', 'move'].includes(op.op) ? op.op : 'set';
|
|
70
|
+
return {
|
|
71
|
+
time,
|
|
72
|
+
kind,
|
|
73
|
+
args: kind === op.op ? encoded.slice(1) : [['operation', encoded]],
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
export const isDiff = (event) =>
|
|
77
|
+
['set', 'insert', 'remove', 'move'].includes(event.kind);
|
|
78
|
+
export function applyEvent(store, event) {
|
|
79
|
+
if (!isDiff(event)) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (event.kind === 'set' && event.args[0]?.[0] !== 'operation') {
|
|
83
|
+
store.setProp(event.args[0], event.args[1], event.args.slice(2));
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const op =
|
|
87
|
+
event.kind === 'set' ? event.args[0][1] : [event.kind, ...event.args];
|
|
88
|
+
const [entry] = logFromLino(
|
|
89
|
+
formatLinks([['tx', '1', ['origin', 'replay'], op]])
|
|
90
|
+
);
|
|
91
|
+
store.apply(entry.ops[0]);
|
|
92
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { asRecording } from './format.js';
|
|
2
|
+
// Keep literals safe if the exported spec is embedded in an HTML script too.
|
|
3
|
+
const quote = (value) =>
|
|
4
|
+
JSON.stringify(value).replace(
|
|
5
|
+
/[<>\u2028\u2029]/g,
|
|
6
|
+
(char) => `\\u${char.charCodeAt(0).toString(16).padStart(4, '0')}`
|
|
7
|
+
);
|
|
8
|
+
const selector = (id) => `[data-link-id=${JSON.stringify(String(id))}]`;
|
|
9
|
+
const locator = (id) => `page.locator(${quote(selector(id))})`;
|
|
10
|
+
const field = (args, name) =>
|
|
11
|
+
args.find((v) => Array.isArray(v) && v[0] === name)?.slice(1) ?? [];
|
|
12
|
+
|
|
13
|
+
function pointerLines(args) {
|
|
14
|
+
const [, action, id] = args;
|
|
15
|
+
const [x = 0, y = 0] = field(args, 'at');
|
|
16
|
+
const [button = 0] = field(args, 'button');
|
|
17
|
+
const lines = [
|
|
18
|
+
` { const box = await target(${quote(id)}).boundingBox(); await page.mouse.move(box.x + ${Number(x)}, box.y + ${Number(y)}); }`,
|
|
19
|
+
];
|
|
20
|
+
if (action === 'down' || action === 'up') {
|
|
21
|
+
lines.push(
|
|
22
|
+
` await page.mouse.${action}({ button: ${quote(['left', 'middle', 'right'][Number(button)] ?? 'left')} });`
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
if (action === 'cancel') {
|
|
26
|
+
lines.push(` await target(${quote(id)}).dispatchEvent('pointercancel');`);
|
|
27
|
+
}
|
|
28
|
+
return lines;
|
|
29
|
+
}
|
|
30
|
+
const INPUT = {
|
|
31
|
+
pointer: pointerLines,
|
|
32
|
+
text: ([, id, value]) => [
|
|
33
|
+
` if (await target(${quote(id)}).evaluate(node => String(node.value ?? node.textContent ?? '')) !== ${quote(value)}) { await target(${quote(id)}).fill(${quote(value)}); }`,
|
|
34
|
+
],
|
|
35
|
+
key: (args) => {
|
|
36
|
+
const id = field(args, 'target')[0];
|
|
37
|
+
if (args[2] === '***') {
|
|
38
|
+
// A redacted key has no native equivalent; never type the placeholder.
|
|
39
|
+
return [
|
|
40
|
+
` await target(${quote(id)}).dispatchEvent(${quote(`key${args[1]}`)}, { key: ${quote(args[2])} });`,
|
|
41
|
+
];
|
|
42
|
+
}
|
|
43
|
+
return [
|
|
44
|
+
` await target(${quote(id)}).focus();`,
|
|
45
|
+
` await page.keyboard.${args[1] === 'up' ? 'up' : 'down'}(${quote(args[2])});`,
|
|
46
|
+
];
|
|
47
|
+
},
|
|
48
|
+
wheel: ([, id, x, y, mode = 0]) => [
|
|
49
|
+
` await target(${quote(id)}).dispatchEvent('wheel', { deltaX: ${Number(x)}, deltaY: ${Number(y)}, deltaMode: ${Number(mode)} });`,
|
|
50
|
+
],
|
|
51
|
+
focus: ([, action, id]) => [
|
|
52
|
+
` await target(${quote(id)}).${action === 'in' ? 'focus' : 'blur'}();`,
|
|
53
|
+
],
|
|
54
|
+
};
|
|
55
|
+
function assertion(event) {
|
|
56
|
+
const [id, name, ...values] = event.args;
|
|
57
|
+
if (typeof id !== 'string') {
|
|
58
|
+
return [];
|
|
59
|
+
}
|
|
60
|
+
const value = values.join(' ');
|
|
61
|
+
if (['label', 'text'].includes(name)) {
|
|
62
|
+
return [` await expect(${locator(id)}).toHaveText(${quote(value)});`];
|
|
63
|
+
}
|
|
64
|
+
if (name === 'value') {
|
|
65
|
+
return [
|
|
66
|
+
` await expect(target(${quote(id)})).toHaveValue(${quote(value)});`,
|
|
67
|
+
];
|
|
68
|
+
}
|
|
69
|
+
// The application supplies the hook for properties with no DOM representation.
|
|
70
|
+
return [
|
|
71
|
+
` await expect.poll(() => page.evaluate(([id, name]) => globalThis.uiRecordingState(id, name), ${quote([id, name])})).toEqual(${quote(values)});`,
|
|
72
|
+
];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Export standard DOM interactions and observable property assertions. */
|
|
76
|
+
export function toPlaywright(source, { url = '/' } = {}) {
|
|
77
|
+
const recording = asRecording(source);
|
|
78
|
+
if (recording.truncated) {
|
|
79
|
+
throw new Error('Cannot export a truncated recording');
|
|
80
|
+
}
|
|
81
|
+
const lines = [
|
|
82
|
+
"import { test, expect } from '@playwright/test';",
|
|
83
|
+
'',
|
|
84
|
+
'// prettier-ignore',
|
|
85
|
+
"test('recorded session', async ({ page }) => {",
|
|
86
|
+
` await page.setViewportSize({ width: ${recording.viewport?.[0] || 1280}, height: ${recording.viewport?.[1] || 800} });`,
|
|
87
|
+
' await page.clock.install();',
|
|
88
|
+
` await page.goto(${quote(url)});`,
|
|
89
|
+
' const target = (id) => { const host = page.locator(`[data-link-id=${JSON.stringify(id)}]`); return host.locator("button, input, textarea, select").or(host).last(); };',
|
|
90
|
+
];
|
|
91
|
+
let previous = 0;
|
|
92
|
+
for (const event of recording.events) {
|
|
93
|
+
if (event.time > previous) {
|
|
94
|
+
lines.push(` await page.clock.runFor(${event.time - previous});`);
|
|
95
|
+
}
|
|
96
|
+
previous = event.time;
|
|
97
|
+
if (event.kind === 'mark') {
|
|
98
|
+
lines.push(` await test.step(${quote(event.args[0])}, async () => {});`);
|
|
99
|
+
}
|
|
100
|
+
if (event.kind === 'input') {
|
|
101
|
+
const encode = INPUT[event.args[0]];
|
|
102
|
+
if (!encode) {
|
|
103
|
+
throw new TypeError(`Unsupported input ${event.args[0]}`);
|
|
104
|
+
}
|
|
105
|
+
lines.push(...encode(event.args));
|
|
106
|
+
}
|
|
107
|
+
if (event.kind === 'set') {
|
|
108
|
+
lines.push(...assertion(event));
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
lines.push('});', '');
|
|
112
|
+
return lines.join('\n');
|
|
113
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { createManualClock } from '../clock.js';
|
|
2
|
+
import { asRecording, parseRecording } from './format.js';
|
|
3
|
+
import { createRecorder } from './recorder.js';
|
|
4
|
+
import { isDiff, restoreStore } from './store.js';
|
|
5
|
+
|
|
6
|
+
/** createApp(seed, clock) returns { store, dispatchInput(event), dispose?() }. */
|
|
7
|
+
export async function verifyReplay(source, { createApp }) {
|
|
8
|
+
const recording = asRecording(source);
|
|
9
|
+
if (recording.truncated) {
|
|
10
|
+
throw new Error('Cannot verify a truncated recording');
|
|
11
|
+
}
|
|
12
|
+
const clock = createManualClock();
|
|
13
|
+
const app = await createApp(recording.seed, clock);
|
|
14
|
+
let recorder;
|
|
15
|
+
try {
|
|
16
|
+
restoreStore(app.store, recording.snapshot);
|
|
17
|
+
recorder = createRecorder({
|
|
18
|
+
store: app.store,
|
|
19
|
+
clock,
|
|
20
|
+
seed: recording.seed,
|
|
21
|
+
started: recording.started ?? 0,
|
|
22
|
+
redact: recording.redacted !== false,
|
|
23
|
+
});
|
|
24
|
+
recorder.start();
|
|
25
|
+
for (const event of recording.events) {
|
|
26
|
+
clock.advance(event.time - clock.now());
|
|
27
|
+
if (event.kind === 'input') {
|
|
28
|
+
await app.dispatchInput(event);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
await Promise.resolve();
|
|
32
|
+
const actual = parseRecording(recorder.stop()).events.filter(isDiff);
|
|
33
|
+
const expected = recording.events.filter(isDiff);
|
|
34
|
+
const mismatches = [];
|
|
35
|
+
for (let i = 0; i < Math.max(expected.length, actual.length); i++) {
|
|
36
|
+
if (JSON.stringify(expected[i]) !== JSON.stringify(actual[i])) {
|
|
37
|
+
mismatches.push({
|
|
38
|
+
time: expected[i]?.time ?? actual[i].time,
|
|
39
|
+
expected: expected[i] ?? null,
|
|
40
|
+
actual: actual[i] ?? null,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return { ok: mismatches.length === 0, mismatches };
|
|
45
|
+
} finally {
|
|
46
|
+
recorder?.stop();
|
|
47
|
+
await app.dispose?.();
|
|
48
|
+
}
|
|
49
|
+
}
|