Files
bookmarkmagic/tests/unit/edit/patch-tree.test.ts
poli0981andClaude Opus 5 e12e8009d7 feat(export,edit): the #export and #edit tabs (Phase 3)
Adds the export pipeline (tri-state folder picker, four format cards,
per-format options, download) and the edit tree (CRUD, search, duplicate
panel, drag & drop with keyboard "Move to…" parity, live event sync, WAI-ARIA
keyboard navigation). 338 tests, npm run verify green, manifest unchanged.

The Phase 3 exit criterion is covered by a test: an exported HTML file
re-imports losslessly, toolbar marker and all.

Pure logic lives in core/edit modules so the whole keyboard and selection
contract is testable without mounting components: core/select.ts (tri-state,
including the ancestor-splitting untick), core/search.ts, edit/patch-tree.ts
(immutable event patching), edit/tree-keyboard.ts, edit/move-target.ts.

An adversarial review found 14 defects, all fixed here. The pattern in the
serious ones was the same: mutate locally, then await a browser call that can
reject, and never look at the result.

- Every mutation now runs through a guard that reports the failure and
  resyncs from the browser. Previously a rejected delete removed an entire
  subtree from the UI while it still existed in the profile, with no error and
  no way back but a reload — and a rejected move left a node displayed under a
  folder it was not in.
- Permanent roots and policy-managed nodes were fully editable. Rename, delete
  and Move-to are now gated on both, in the mouse and keyboard paths, so calls
  the browser will always reject are never attempted. "Move to…" previously
  validated only the destination, so moving a root nested the two roots.
- New folder resolved the focused node's PARENT, so selecting a root yielded
  the synthetic root "0" and every create failed silently; selecting a folder
  created a sibling. It now targets the focused folder itself and expands it,
  so the inline rename input actually mounts.
- The tree kept DOM focus on the container and never published
  aria-activedescendant, so arrow-key navigation announced nothing.
- The container keydown handler ignored event.target and modifier state, so it
  stole keys aimed at row buttons and swallowed Ctrl/Cmd-F — which was not
  implemented at all.
- Search expansion was unioned in at render time but toggle() wrote only to
  `expanded`, making the disclosure button and ArrowLeft dead on every
  search-expanded folder. Search expansion is now merged into `expanded`.
- The tri-state checkbox never showed indeterminate: a parameterless Svelte
  action runs once at creation, so a partially selected folder looked
  identical to an unselected one.
- The export button re-serialized the whole tree on every render just to show
  a count, and its filename disagreed with the saved one.
- Markdown layout and CSV delimiter were unreachable from the Export tab.
- Search tested title and url separately; the spec defines one haystack.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-26 06:13:53 +07:00

138 lines
4.4 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import {
changeNode,
type EditNode,
findNode,
insertNode,
moveNode,
removeNode,
} from '@/lib/edit/patch-tree';
const tree = (): EditNode[] => [
{
id: 'bar',
title: 'Bookmarks bar',
children: [
{ id: 'a', parentId: 'bar', title: 'a', url: 'https://a.example/' },
{
id: 'dev',
parentId: 'bar',
title: 'Dev',
children: [{ id: 'gh', parentId: 'dev', title: 'GitHub', url: 'https://github.com/' }],
},
],
},
{ id: 'other', title: 'Other', children: [] },
];
const ids = (nodes: readonly EditNode[]): string[] => nodes.map((n) => n.id);
describe('insertNode', () => {
it('appends when no index is given', () => {
const next = insertNode(tree(), 'other', undefined, {
id: 'new',
title: 'n',
url: 'https://n/',
});
expect(ids(findNode(next, 'other')?.children ?? [])).toEqual(['new']);
});
it('respects an explicit index', () => {
const next = insertNode(tree(), 'bar', 0, { id: 'new', title: 'n', url: 'https://n/' });
expect(ids(findNode(next, 'bar')?.children ?? [])).toEqual(['new', 'a', 'dev']);
});
it('clamps an out-of-range index to the end', () => {
const next = insertNode(tree(), 'bar', 99, { id: 'new', title: 'n', url: 'https://n/' });
expect(ids(findNode(next, 'bar')?.children ?? [])).toEqual(['a', 'dev', 'new']);
});
it('ignores an event for a parent we do not hold', () => {
const before = tree();
const next = insertNode(before, 'nonexistent', 0, { id: 'new', title: 'n' });
expect(next).toEqual(before);
});
it('never targets a bookmark as a parent', () => {
const before = tree();
expect(insertNode(before, 'a', 0, { id: 'new', title: 'n' })).toEqual(before);
});
it('is immutable and shares untouched branches', () => {
const before = tree();
const next = insertNode(before, 'other', 0, { id: 'new', title: 'n' });
expect(before[1]?.children).toEqual([]);
// The "bar" branch was not touched, so it should be the same object.
expect(next[0]).toBe(before[0]);
});
});
describe('removeNode', () => {
it('removes a leaf', () => {
const next = removeNode(tree(), 'a');
expect(ids(findNode(next, 'bar')?.children ?? [])).toEqual(['dev']);
});
it('removes a folder together with its subtree', () => {
const next = removeNode(tree(), 'dev');
expect(findNode(next, 'gh')).toBeUndefined();
});
it('ignores an unknown id — events for already-removed nodes are routine', () => {
const before = tree();
expect(removeNode(before, 'ghost')).toEqual(before);
});
it('does not mutate the input', () => {
const before = tree();
removeNode(before, 'a');
expect(ids(before[0]?.children ?? [])).toEqual(['a', 'dev']);
});
});
describe('changeNode', () => {
it('applies a title change', () => {
expect(findNode(changeNode(tree(), 'gh', { title: 'Renamed' }), 'gh')?.title).toBe('Renamed');
});
it('applies a url change without touching the title', () => {
const next = changeNode(tree(), 'gh', { url: 'https://new.example/' });
expect(findNode(next, 'gh')).toMatchObject({ title: 'GitHub', url: 'https://new.example/' });
});
it('ignores an unknown id', () => {
const before = tree();
expect(changeNode(before, 'ghost', { title: 'x' })).toEqual(before);
});
});
describe('moveNode', () => {
it('reparents a node', () => {
const next = moveNode(tree(), 'gh', 'other', 0);
expect(ids(findNode(next, 'other')?.children ?? [])).toEqual(['gh']);
expect(findNode(next, 'dev')?.children).toEqual([]);
});
it('updates parentId on the moved node', () => {
expect(findNode(moveNode(tree(), 'gh', 'other', 0), 'gh')?.parentId).toBe('other');
});
it('reorders within the same parent using post-removal indices', () => {
// onMoved reports the index the node lands at AFTER being lifted out.
const next = moveNode(tree(), 'a', 'bar', 1);
expect(ids(findNode(next, 'bar')?.children ?? [])).toEqual(['dev', 'a']);
});
it('refuses to move a folder into its own descendant', () => {
// Allowing it would detach the whole subtree from the tree.
const before = tree();
expect(moveNode(before, 'dev', 'gh', 0)).toEqual(before);
expect(moveNode(before, 'dev', 'dev', 0)).toEqual(before);
});
it('ignores an unknown id', () => {
const before = tree();
expect(moveNode(before, 'ghost', 'other', 0)).toEqual(before);
});
});