Skip to content

Commit 199d422

Browse files
committed
Update hyperclayjs
1 parent 33b1036 commit 199d422

23 files changed

Lines changed: 483 additions & 147 deletions

File tree

‎__tests__/All.test.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,4 +1090,19 @@ describe('All.js', () => {
10901090
});
10911091
});
10921092
});
1093+
1094+
test('an array argument reaches each element whole, not spread across calls', () => {
1095+
const calls = [];
1096+
Element.prototype.__probeArgs = function (...args) { calls.push(args); };
1097+
try {
1098+
All('.item').__probeArgs([{ k: 1 }, { k: 2 }], 300);
1099+
} finally {
1100+
delete Element.prototype.__probeArgs;
1101+
}
1102+
expect(calls).toEqual([
1103+
[[{ k: 1 }, { k: 2 }], 300],
1104+
[[{ k: 1 }, { k: 2 }], 300],
1105+
[[{ k: 1 }, { k: 2 }], 300],
1106+
]);
1107+
});
10931108
});
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* @jest-environment jsdom
3+
*
4+
* Two defects in one lane:
5+
* - a duplicated tab inherited the original's sender id through sessionStorage,
6+
* so each treated the other's edits as its own echo and they stopped syncing;
7+
* - concurrent sends raced, and a last-write-wins server could store the OLDER
8+
* snapshot for every peer.
9+
*/
10+
11+
jest.mock('../src/vendor/hyper-morph.vendor.js', () => ({
12+
HyperMorph: { morph: jest.fn(() => Promise.resolve()) },
13+
}));
14+
15+
jest.mock('../src/utilities/mutation.js', () => ({
16+
__esModule: true,
17+
default: { pause: jest.fn(), resume: jest.fn() },
18+
}));
19+
20+
class FakeEventSource {
21+
constructor(url) {
22+
this.url = url;
23+
this.readyState = 0;
24+
}
25+
close() {}
26+
}
27+
28+
import { LiveSync } from '../src/communication/live-sync.js';
29+
30+
describe('LiveSync sender identity + send ordering', () => {
31+
beforeEach(() => {
32+
global.EventSource = FakeEventSource;
33+
window.EventSource = FakeEventSource;
34+
});
35+
36+
afterEach(() => {
37+
delete global.EventSource;
38+
delete window.EventSource;
39+
delete global.fetch;
40+
});
41+
42+
test("a duplicated tab does not inherit the original's sender id", () => {
43+
// What tab duplication actually does: the copy starts with the original's
44+
// sessionStorage already populated.
45+
sessionStorage.setItem('livesync-client-id', 'inherited-id');
46+
47+
const original = new LiveSync();
48+
const duplicate = new LiveSync();
49+
50+
expect(original.clientId).not.toBe(duplicate.clientId);
51+
expect(original.clientId).not.toBe('inherited-id');
52+
expect(duplicate.clientId).not.toBe('inherited-id');
53+
});
54+
55+
test('one POST in flight at a time, and the newest queued payload wins', async () => {
56+
const settle = [];
57+
global.fetch = jest.fn(
58+
() => new Promise((resolve) => settle.push(() => resolve({ ok: true })))
59+
);
60+
61+
const sync = new LiveSync();
62+
63+
sync._enqueueSend('<html>A</html>', null);
64+
expect(global.fetch).toHaveBeenCalledTimes(1);
65+
66+
// Both land while A is still on the wire; C must replace B in the one slot.
67+
sync._enqueueSend('<html>B</html>', null);
68+
sync._enqueueSend('<html>C</html>', null);
69+
expect(global.fetch).toHaveBeenCalledTimes(1);
70+
71+
settle[0]();
72+
await new Promise((resolve) => setTimeout(resolve, 0));
73+
74+
expect(global.fetch).toHaveBeenCalledTimes(2);
75+
expect(JSON.parse(global.fetch.mock.calls[1][1].body).html).toBe('<html>C</html>');
76+
});
77+
});

‎__tests__/live-sync.test.js‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -359,19 +359,23 @@ describe('LiveSync applyUpdate (rAF-paced queue)', () => {
359359
document.body.innerHTML = '';
360360
});
361361

362-
test('_mintId persists counter to sessionStorage', () => {
363-
sessionStorage.setItem('livesync-id-counter', '0');
362+
test('_mintId numbers per instance; the clientId prefix is what keeps ids unique', () => {
364363
const fresh = new LiveSync();
365-
fresh._mintId();
366-
fresh._mintId();
367-
expect(sessionStorage.getItem('livesync-id-counter')).toBe('2');
364+
const firstA = fresh._mintId();
365+
expect(firstA.endsWith(':1')).toBe(true);
366+
expect(fresh._mintId().endsWith(':2')).toBe(true);
368367
fresh.stop();
369368

370-
// A new instance reloads the counter and resumes from there.
369+
// The counter used to persist to sessionStorage so a later instance resumed
370+
// it. sessionStorage is COPIED into a duplicated tab, which is what made two
371+
// tabs share one sender id and stop syncing, so nothing about identity lives
372+
// in storage now: the counter restarts and the differing clientId prefix is
373+
// what keeps minted ids from colliding.
371374
const reborn = new LiveSync();
372-
expect(reborn.idCounter).toBe(2);
373-
const id = reborn._mintId();
374-
expect(id.endsWith(':3')).toBe(true);
375+
expect(reborn.idCounter).toBe(0);
376+
const firstB = reborn._mintId();
377+
expect(firstB.endsWith(':1')).toBe(true);
378+
expect(firstB).not.toBe(firstA);
375379
reborn.stop();
376380
});
377381

‎__tests__/nearest.test.js‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @jest-environment jsdom
3+
*/
4+
5+
import nearest from '../src/utilities/nearest.js';
6+
7+
describe('nearest', () => {
8+
test('an empty-string transform result is a hit, not a miss', () => {
9+
document.body.innerHTML = `
10+
<section>
11+
<div id="start"></div>
12+
<input class="field" value="">
13+
</section>
14+
<input class="field" id="far" value="later">
15+
`;
16+
const start = document.getElementById('start');
17+
expect(nearest(start, '.field', (el) => el.value)).toBe('');
18+
});
19+
20+
test('a false transform result is a hit, not a miss', () => {
21+
document.body.innerHTML = `
22+
<section>
23+
<div id="start"></div>
24+
<input type="checkbox" class="flag">
25+
</section>
26+
<input type="checkbox" class="flag" id="far" checked>
27+
`;
28+
const start = document.getElementById('start');
29+
expect(nearest(start, '.flag', (el) => el.checked)).toBe(false);
30+
});
31+
});

‎conformance/config.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export const FIXTURES_DIR = 'fixtures';
1818
// here is skipped, not failed (fixtures.md: a client may skip a feature it lacks
1919
// and still claim conformance on the rest).
2020
export const CAPABILITY_MODULES = {
21+
'freeze': '/src/custom-attributes/saveFreeze.js',
2122
'form-persist': '/src/core/enablePersistentFormInputValues.js',
2223
};
2324

‎conformance/conformance.test.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,11 @@ for (const fx of manifest.fixtures) {
4343
await goldenAssert(`${FIXTURES_DIR}/${fx.name}/document.html`, out.document);
4444
});
4545
}
46+
47+
if (fx.syncGolden) {
48+
it('sync payload bytes match golden', async () => {
49+
await goldenAssert(`${FIXTURES_DIR}/${fx.name}/sync.html`, out.sync);
50+
});
51+
}
4652
});
4753
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html><html lang="en"><head>
2+
<meta charset="utf-8">
3+
<title>Freeze nested in no-snapshot</title>
4+
<script>window.__hyperclayEditMode = true;</script>
5+
</head>
6+
<body>
7+
<p>A [freeze] region is saved as authored, whatever the runtime does to it.</p>
8+
<div freeze="" id="before">AUTHORED-BEFORE</div>
9+
10+
<div freeze="" id="after">AUTHORED-AFTER</div>
11+
12+
13+
</body></html>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Freeze nested in no-snapshot</title>
6+
<script>window.__hyperclayEditMode = true;</script>
7+
</head>
8+
<body>
9+
<p>A [freeze] region is saved as authored, whatever the runtime does to it.</p>
10+
<div freeze id="before">AUTHORED-BEFORE</div>
11+
<div no-snapshot>
12+
<div freeze id="hidden">AUTHORED-HIDDEN</div>
13+
</div>
14+
<div freeze id="after">AUTHORED-AFTER</div>
15+
</body>
16+
</html>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html><html lang="en"><head>
2+
<meta charset="utf-8">
3+
<title>Freeze nested in no-snapshot</title>
4+
<script>window.__hyperclayEditMode = true;</script>
5+
</head>
6+
<body>
7+
<p>A [freeze] region is saved as authored, whatever the runtime does to it.</p>
8+
<div freeze="" id="before">AUTHORED-BEFORE</div>
9+
10+
<div freeze="" id="after">AUTHORED-AFTER</div>
11+
12+
13+
</body></html>

‎conformance/fixtures/manifest.json‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@
5555
"skipDocument": false,
5656
"requires": []
5757
},
58+
{
59+
"name": "sync-root-attrs",
60+
"description": "the sync payload drops the six root attributes that belong to one tab; the snapshot and the document keep them, because stripping the host's own injections is the host's job",
61+
"skipDocument": false,
62+
"syncGolden": true,
63+
"requires": []
64+
},
5865
{
5966
"name": "properties-not-attributes",
6067
"description": "state in a JS property is absent from both artifacts; the same value in an attribute is present",
@@ -80,6 +87,12 @@
8087
"skipDocument": false,
8188
"requires": ["form-persist"],
8289
"secrets": ["S3CRET-sentinel-should-never-serialize"]
90+
},
91+
{
92+
"name": "freeze-nested-no-snapshot",
93+
"description": "a [freeze] inside [no-snapshot] must not shift the authored content of the freeze regions around it",
94+
"skipDocument": false,
95+
"requires": ["freeze"]
8396
}
8497
]
8598
}

0 commit comments

Comments
 (0)