Add shareable invitation links
Codes now travel as links with the code in the URL fragment, which the app reads and clears before mounting (and on hashchange). An offer link opens a preview that suggests an idle slot, preferring the one paired with the sender's device, and only fills in the code; the user still creates the answer. Answer links reach the waiting sender tab over a same-origin BroadcastChannel, are acknowledged once and reported as used afterwards, and fall back to copy and paste when no tab answers. The panel offers Share link where the Web Share API accepts URLs, Copy link, and the bare code under a disclosure. The browser check covers the offer dialog, clean URL, answer relay, reuse, fallback on another origin, an invalid fragment, axe on both dialogs and exact bytes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8bd37ac8b1
commit
43ee7f119f
14 files changed
+422
-59
No files matched your search
@@ -0,0 +1,39 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { deliverAnswer, listenForAnswers, takeFragment } from '../src/lib/invitations.ts';
|
||||
|
||||
test('answers reach the waiting offer once and report later deliveries as used', async () => {
|
||||
let waiting = true;
|
||||
const received: string[] = [];
|
||||
const stop = listenForAnswers('ref-a', code => {
|
||||
if (!waiting) return false;
|
||||
waiting = false; received.push(code);
|
||||
return true;
|
||||
});
|
||||
const other = listenForAnswers('ref-b', () => assert.fail('another offer must ignore this answer'));
|
||||
try {
|
||||
assert.equal(await deliverAnswer('ref-a', 'HC1.answer'), 'delivered');
|
||||
assert.equal(await deliverAnswer('ref-a', 'HC1.answer'), 'used');
|
||||
assert.deepEqual(received, ['HC1.answer']);
|
||||
} finally { stop(); other(); }
|
||||
});
|
||||
|
||||
test('answers without a waiting tab fall back to manual pasting', async () => {
|
||||
assert.equal(await deliverAnswer('nobody', 'HC1.answer', 50), null);
|
||||
const stop = listenForAnswers('ref-a', () => true);
|
||||
stop();
|
||||
assert.equal(await deliverAnswer('ref-a', 'HC1.answer', 50), null);
|
||||
});
|
||||
|
||||
test('only code fragments are taken from the URL', () => {
|
||||
const replaced: string[] = [];
|
||||
const url = { href: 'https://example.test/app/?x=1#HC1.abc', hash: '#HC1.abc' };
|
||||
Object.assign(globalThis, { location: url, history: { state: null, replaceState: (_: unknown, __: string, next: string) => replaced.push(next) } });
|
||||
try {
|
||||
assert.equal(takeFragment(), 'HC1.abc');
|
||||
assert.deepEqual(replaced, ['https://example.test/app/?x=1']);
|
||||
Object.assign(url, { href: 'https://example.test/app/#main-content', hash: '#main-content' });
|
||||
assert.equal(takeFragment(), '');
|
||||
assert.equal(replaced.length, 1);
|
||||
} finally { Reflect.deleteProperty(globalThis, 'location'); Reflect.deleteProperty(globalThis, 'history'); }
|
||||
});
|
||||
Reference in new issue
Block a user