Files
poli0981andClaude Opus 5.5 e86a1d77b3 fix(import): read every entry a file holds, and skip what Chrome would reject
Several shapes made v1.0.x import a smaller tree than the file held, with no
warning at all:

- a loose bookmark before the wrapper list (no </DT>) swallowed the whole
  list; with an explicit </DT> the bookmark "consumed" the next list;
- an unescaped <b> in one title re-opened around every later entry
  (dt > b > a, dl > b > dt) and hid them from a direct-children walk;
- a root list wrapped in <div>/<table> was invisible;
- PERSONAL_TOOLBAR_FOLDER="false" made a folder the bar.

Every reader now classifies addresses before anything is written (core/
href.ts): blank -> MISSING_URL, unparseable -> INVALID_URL (chrome.bookmarks.
create rejects those mid-write, after a Replace has already cleared the
tree), Firefox place: -> PLACE_QUERIES_SKIPPED in browser-written formats
(kept in BM JSON and CSV, our restore formats). A DT that still yields
nothing is counted as UNREADABLE_ENTRIES instead of vanishing.

Also:
- empty titles stay empty (the URL fallback ruined favicon-only bars);
- the HTML node cap is counted on the raw text before DOMParser builds a
  DOM; the depth cap no longer rejects an empty list one level past it;
- root markers for Other and Mobile (UNFILED_/MOBILE_BOOKMARKS_FOLDER in
  HTML, "root" in BM JSON, version stays 1), and a 1.0.x full-tree backup
  is recognised by shape and marked on read;
- BM JSON dates go through the shared normalizer; url "" (which Chrome
  makes a folder) is skipped;
- CSV: an unclosed quote is CSV_UNCLOSED_QUOTE with its line instead of one
  cell eating the file; formula cells get one reversible guard quote; sep=
  lines and padding rows are skipped; folder lookup is a Map, not a scan;
- Markdown escapes every CommonMark-active character and collapses line
  breaks in titles (raw HTML used to pass through);
- shared WarningBag/NodeBudget/title rule, an iterative walkTree, an
  isBookmark type guard, a CSV dialect module, chromeTimeToSeconds and
  microsToSeconds for the JSON formats that follow;
- locales split warnings/errors into <lang>-notices.ts to stay under the
  size limit; every warning and error code must have a message (test).

Four new fixtures, golden serializer suites for HTML and BM JSON (docs/11
named one; neither existed), and docs/02, 04, 05, 07, 09, 11, 15 updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 03:22:56 +07:00

53 lines
1.9 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { MAX_DEPTH, MAX_NODES } from '@/lib/core/limits';
import { bookmarkTitle, NodeBudget, stripBom, WarningBag } from '@/lib/core/parse/shared';
import { expectBmError } from '../../../helpers/errors';
describe('WarningBag', () => {
it('aggregates by code and ignores zero increments', () => {
const bag = new WarningBag();
bag.add('MISSING_URL');
bag.add('MISSING_URL', 2);
bag.add('INVALID_DATE', 0);
expect(bag.toArray()).toEqual([{ code: 'MISSING_URL', count: 3 }]);
});
it('carries a detail line when one is noted', () => {
const bag = new WarningBag();
bag.note('NEWER_VERSION', 'file version 2');
expect(bag.toArray()).toEqual([{ code: 'NEWER_VERSION', count: 1, detail: 'file version 2' }]);
});
});
describe('NodeBudget', () => {
it('allows exactly MAX_NODES and refuses one more', () => {
const budget = new NodeBudget();
for (let i = 0; i < MAX_NODES; i++) budget.count();
expectBmError(() => budget.count(), 'TOO_MANY_NODES');
});
it('allows MAX_DEPTH and refuses one more', () => {
const budget = new NodeBudget();
expect(() => budget.depth(MAX_DEPTH)).not.toThrow();
expectBmError(() => budget.depth(MAX_DEPTH + 1, 'roots[0]'), 'TOO_DEEP');
});
});
describe('bookmarkTitle', () => {
it('keeps titles verbatim — empty included — and counts the empty ones', () => {
const bag = new WarningBag();
expect(bookmarkTitle('', bag)).toBe('');
expect(bookmarkTitle(' ', bag)).toBe(' ');
expect(bookmarkTitle('Title', bag)).toBe('Title');
expect(bag.toArray()).toEqual([{ code: 'EMPTY_TITLE', count: 2 }]);
});
});
describe('stripBom', () => {
it('removes exactly one leading U+FEFF', () => {
expect(stripBom('\uFEFFx')).toBe('x');
expect(stripBom('\uFEFF\uFEFFx')).toBe('\uFEFFx');
expect(stripBom('x')).toBe('x');
});
});