Alter URL encoding

This commit is contained in:
2026-07-11 22:50:45 -05:00
parent 20cd4ba6a0
commit 6ce1c39b8d
4 changed files with 177 additions and 17 deletions
+26 -2
View File
@@ -13,6 +13,8 @@ const {
base64UrlToBytes,
bytesToBase64Url,
decodeTreeState,
decodeAdaptiveBitset,
encodeAdaptiveBitset,
getBit,
loadManifest,
setBit,
@@ -136,6 +138,28 @@ test('Base64URL encoding is unpadded, URL-safe, and round trips', () => {
assert.throws(() => base64UrlToBytes('not valid!'), /Invalid Base64URL/);
});
test('adaptive bitsets choose sparse enabled and disabled representations', () => {
const mostlyDisabled = new Map(Array.from(
{ length: 628 },
(_, slot) => [slot, slot === 2 || slot === 400]
));
const sparseEnabled = encodeAdaptiveBitset(mostlyDisabled, 628);
assert.equal(sparseEnabled[0], 1);
const enabled = decodeAdaptiveBitset(sparseEnabled, 628);
assert.equal(enabled(2), true);
assert.equal(enabled(3), false);
assert.equal(enabled(400), true);
const nearlyAllEnabled = new Map(Array.from({ length: 100 }, (_, slot) => [slot, slot !== 7]));
const sparseDisabled = encodeAdaptiveBitset(nearlyAllEnabled, 100);
assert.equal(sparseDisabled[0], 2);
const enabledExcept = decodeAdaptiveBitset(sparseDisabled, 105);
assert.equal(enabledExcept(6), true);
assert.equal(enabledExcept(7), false);
assert.equal(enabledExcept(99), true);
assert.equal(enabledExcept(100), false);
});
test('loads text and JSON manifests while retaining tombstone slots', () => {
const text = loadManifest('# tree-state-manifest\nroot\n!deleted\nroot>child\n');
assert.equal(text.slots.length, 3);
@@ -170,7 +194,7 @@ test('serializes and restores checked and expanded state through a fragment', ()
checkbox(first.container, 'first').click();
first.container.querySelector('.checkbox-tree__toggle').click();
const fragment = first.tree.serializeStateToFragment(manifest);
assert.equal(fragment, '#v=1&c=Ag&x=AQ');
assert.equal(fragment, '#v=1&c=AAI&x=AAE');
const second = createTree({ manifest });
const result = second.tree.restoreStateFromLocation(manifest, { hash: fragment });
@@ -201,7 +225,7 @@ test('malformed, oversized, and unsupported URL state fails safely', () => {
const manifest = loadManifest({ schema: 1, nodes: ['parent', 'parent>first', 'parent>second'] });
assert.equal(decodeTreeState('#v=2&c=AQ', manifest).applied, false);
assert.equal(decodeTreeState('#v=1&c=!', manifest).applied, false);
assert.equal(decodeTreeState('#v=1&c=AQA', manifest).applied, false);
assert.equal(decodeTreeState('#v=1&c=Aw', manifest).applied, false);
const { tree } = createTree({ initiallySelected: true });
const result = tree.restoreStateFromLocation(manifest, { hash: '#v=1&c=!' });