'use strict' const test = require('node:test') const assert = require('node:assert') const fs = require('node:fs') const path = require('node:path') const SRC = fs.readFileSync(path.join(__dirname, '..', 'src', 'vault-browse.js'), 'utf8') function load() { const g = {} new Function('window', 'globalThis', SRC)(g, g) return g.IWBStorageFile.vaultBrowse } const DAY = 86400000 const NOW = new Date('2026-12-06T15:00:00+08:00').getTime() function sampleBoard(id, name, category, starred, nodes, ts, deleted) { return { id, name, category, starred, nodes: new Array(Math.max(0, nodes | 0)).fill(0).map((_, i) => ({ id: 'n' + i, type: 'prompt' })), lastUsed: ts, mtime: ts, deleted: deleted } } const m = (id, name, category, starred, nodes, ts) => ({ id, name, category, starred, nodes, lastUsed: ts, mtime: ts }) test('analyzeVault:跳过回收站、统计分类、带出索引时间', () => { const V = load() const dataText = JSON.stringify({ keys: { iwb_home_cats: JSON.stringify(['双11', '品牌']), iwb_web_workspaces: JSON.stringify({ workspaces: [{ id: 'b1', lastUsed: NOW, nodeCount: 5 }] }), } }) const canvases = [ { id: 'b1', text: JSON.stringify(sampleBoard('b1', '爱奇艺 KV', '双11', true, 72, 0)), mtime: NOW }, { id: 'b2', text: JSON.stringify(sampleBoard('b2', '中秋', '', false, 59, NOW - DAY)), mtime: NOW - DAY }, { id: 'b3', text: JSON.stringify(sampleBoard('b3', '已删', '品牌', false, 3, NOW, true)) }, ] const r = V.analyzeVault({ dataText, canvases }) assert.equal(r.boards.length, 2) assert.equal(r.boards[0].nodes, 72) assert.equal(r.boards[0].lastUsed, NOW) assert.equal(r.categories.find((c) => c.name === '双11').count, 1) assert.equal(r.categories.find((c) => c.name === '未分类').count, 1) assert.deepEqual(r.extras, ['双11', '品牌']) }) test('applyView:筛选(收藏/未分类/分类)与搜索', () => { const V = load() const boards = [ m('a', '爱奇艺双11 KV', '双11', true, 72, NOW), m('b', '爱奇艺中秋', '', false, 59, NOW - DAY), m('c', '品牌页', '品牌', false, 20, NOW - 3 * DAY), ] assert.equal(V.applyView(boards, { now: NOW, filter: 'all' }).list.length, 3) assert.equal(V.applyView(boards, { now: NOW, filter: 'starred' }).list.length, 1) assert.equal(V.applyView(boards, { now: NOW, filter: 'uncategorized' }).list.length, 1) assert.equal(V.applyView(boards, { now: NOW, filter: 'cat:品牌' }).list.length, 1) assert.equal(V.applyView(boards, { now: NOW, q: '中秋' }).list.length, 1) assert.equal(V.applyView(boards, { now: NOW, filter: 'recent' }).list.length, 3) }) test('applyView:排序与分组', () => { const V = load() const boards = [ m('a', 'B 画板', '品牌', false, 10, NOW), m('b', 'A 画板', '双11', false, 30, NOW - 2 * DAY), m('c', 'C 画板', '', false, 20, NOW - 10 * DAY), ] const byName = V.applyView(boards, { now: NOW, sort: 'name' }) assert.deepEqual(byName.list.map((x) => x.name), ['A 画板', 'B 画板', 'C 画板']) const byNodes = V.applyView(boards, { now: NOW, sort: 'nodes' }) assert.equal(byNodes.list[0].nodes, 30) const recent = V.applyView(boards, { now: NOW, sort: 'recent' }) assert.deepEqual(recent.groups.map((g) => g.label), ['今天', '近一周', '更早']) const byCat = V.applyView(boards, { now: NOW, sort: 'category' }) assert.deepEqual(byCat.groups.map((g) => g.label).sort(), ['双11', '品牌', '未分类'].sort()) }) test('uniqueBoardName:名称冲突加 (2)', () => { const V = load() assert.equal(V.uniqueBoardName('爱奇艺', []), '爱奇艺') assert.equal(V.uniqueBoardName('爱奇艺', ['爱奇艺']), '爱奇艺 (2)') assert.equal(V.uniqueBoardName('爱奇艺', ['爱奇艺', '爱奇艺 (2)']), '爱奇艺 (3)') })