dsh-fishpal 0.9.12 → 0.9.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -7,6 +7,53 @@
7
7
  > 改动散在提交历史里(`lib/` 下多处),没有回填到这里。这里不编造它的内容 ——
8
8
  > 要看 0.9.0 改了什么,翻 git 历史。从 0.9.1 起重新逐版记录。
9
9
 
10
+ ## [0.9.13] — 2026-12-01
11
+
12
+ **漂流瓶分成两个展览馆:收到的、丢出去的。还有一处会写坏账本的 bug 修掉了。**
13
+
14
+ 岸的原话(这一版的需求源头,逐字):
15
+
16
+ - 「而且收到的瓶子肯定是要有历史记录的。回信也是。然后这个历史记录也可以全部删,也可以一条一条删。然后这个存在历史记录里面的瓶子也是可以进收藏馆的,防止被误删。」
17
+ - 「还有丢过的瓶子,也是要放在一个专门存瓶子的地方的。还有如果收到了回信,也是附在那个瓶子那边。就是有两个展览馆,一个是收的瓶子,一个是丢的瓶子。当然这个也是可以删的。」
18
+ - 「我要的展览柜呢。」(空的时候整块不渲染 → 现在空也渲染,写明为什么空)
19
+
20
+ ### 新增
21
+
22
+ - **两个展览馆**:`📥 收到的瓶子` / `📤 丢出去的瓶子`,各有自己的清空按钮,**各清各的**。
23
+ 本地服务的清空接口带 `direction`,没带方向才是「两个一起清」。
24
+ - 账本加 `direction` 字段(`in` / `out`)。旧记录没有这个字段,一律按 `in` 处理 —— 那是迁移,不是猜。
25
+ - 发瓶子在 `publish` **成功之后**才记进「丢的」,草稿不会提前出现在展览馆里。
26
+ - `_verify-history-two-halls.mjs`(新,24 项)、`_verify-history-ui.mjs`、`_verify-history-api.mjs`、`_verify-history-safety.mjs`。
27
+
28
+ ### 修复
29
+
30
+ - **★ 我丢出去的瓶子,正文会被别人的回信覆盖。**
31
+ 回信的 `bottle_id` 跟我丢出去那只**是同一个 id**(`bottle.js:1005`,`bottle_id` 和 `reply_to_bottle_id` 填的是同一个 key),
32
+ 所以在账本里会命中我自己那条记录,走普通更新分支,把我写的正文盖掉、署名改成对方。
33
+ 实测复现:发「我丢出去的那只瓶子」→ 收回信 → 变成「别人回我的话」,署名变成陌生人。
34
+ 改成:`out` 的记录收到 `in` 的回信时**只挂 `gotReply`,text / nick / direction / count 一个字段都不动**。
35
+ - **清空一个展览馆会连另一个一起清**(清空接口没带方向时按「全部」算,界面又没传方向)。
36
+ 现在方向从按钮一路透传到账本,白名单只认 `in` / `out`。
37
+ - 回信重试:从「60 分 / 120 分、最多 3 次」改成**两天内最多 6 次、间隔随机**。
38
+ 岸:「可以两天重试 6 次,毕竟有人可能有事一天没开电脑」。
39
+ - 页面脚本里写不了换行转义 —— 那段页面脚本整个塞在一个模板字符串里,写下去会变成真换行、整页语法错。
40
+ 这个坑这一版踩了两次(一次在提示框文案,一次在我自己写的注释里),两次都是测试当场抓住的。
41
+ - 端口被占用时自动往后找(显式指定 `FISHPAL_PORT` 就不漂),避免两个实例抢一个端口。
42
+ - 工作区改名失败不再静默假装成功(会重试并在失败时说出来)。
43
+
44
+ ### 内部
45
+
46
+ - `_boot-check` 加两段:**③b 用磁盘上的新代码真渲染一次页面**(以前只验「正在跑的那份」,那是旧码,绿的也证明不了磁盘上这份),
47
+ ③c 检测本机是否已经有一个 DSH 在跑,避免重复启动。
48
+ - 全量测试 **620 通过 / 0 失败**;`_boot-check` 全部通过。
49
+
50
+ ### ★ 版本号说明(为什么这里要专门写一段)
51
+
52
+ **0.9.11 和 0.9.12 发布过,但从来没打过 git tag**(tag 停在 `v0.9.10`)。
53
+ 所以有人对着 GitHub 的 tag 列表看,会觉得版本号对不上 npm —— 那是**丢的 tag**,不是版本号算错。
54
+ 这里**不回补那两个 tag**(补了会像刚发过一样),只在日志里把话说清:
55
+ npm 上的 `0.9.11` / `0.9.12` 都已经是真发布过的版本,内容见上面两节。
56
+
10
57
  ## [0.9.12] — 2026-11-30
11
58
 
12
59
  **瓶子和隔离区分家:瓶子只在漂流瓶页里,不再重复出现在「陌生人消息隔离区」。**
@@ -0,0 +1,59 @@
1
+ // 回归:漂流瓶/回信必须投进「正在用的那个会话」,别的类型不许串窗口。
2
+ // 2026-11-30 岸:「瓶子发回的消息,也是发回到正在用的那个地方吧。」
3
+ // 以前 pickAgent 只认房间会话(roomState.sessionId),房间不在就返回 null →
4
+ // 攒着等补投 / 另开一间房;而 lastAgent(最近活跃的会话)只写不读,成了死变量。
5
+ import fs from 'node:fs';
6
+ import assert from 'node:assert/strict';
7
+
8
+ const SRC = fs.readFileSync(new URL('./lib/index.js', import.meta.url), 'utf8');
9
+ let pass = 0;
10
+ const ok = (name, cond) => {
11
+ assert.ok(cond, name);
12
+ pass++;
13
+ console.log(' ✓ ' + name);
14
+ };
15
+
16
+ // ── 1. pickAgent 有 preferActive,且位置在 allowFallback 之前 ──────────────
17
+ const pStart = SRC.indexOf('function pickAgent(');
18
+ const pEnd = SRC.indexOf('\n }', pStart);
19
+ assert.ok(pStart > 0 && pEnd > pStart, '找不到 pickAgent');
20
+ const pick = SRC.slice(pStart, pEnd);
21
+
22
+ ok('pickAgent 接受 preferActive', /preferActive\s*=\s*false/.test(pick));
23
+ ok(
24
+ 'preferActive 分支排在 allowFallback 之前(房间不在时才轮到它)',
25
+ pick.indexOf('preferActive && lastAgent') > 0 &&
26
+ pick.indexOf('preferActive && lastAgent') < pick.indexOf('if (!allowFallback)')
27
+ );
28
+
29
+ // ── 2. inject() 只对 bottle / bottle_reply 开这个口子 ─────────────────────
30
+ const iStart = SRC.indexOf('function inject(m, suspicious)');
31
+ const iEnd = SRC.indexOf('return true;', iStart);
32
+ assert.ok(iStart > 0 && iEnd > iStart, '找不到 inject');
33
+ const inj = SRC.slice(iStart, iEnd);
34
+
35
+ ok('inject 里定义了 isBottle', /const isBottle\s*=/.test(inj));
36
+ ok(
37
+ 'isBottle 只认 bottle / bottle_reply',
38
+ /isBottle\s*=\s*m\.type === 'bottle' \|\| m\.type === 'bottle_reply'/.test(inj)
39
+ );
40
+ ok('pickAgent 调用传了 preferActive: isBottle', /preferActive:\s*isBottle/.test(inj));
41
+ ok(
42
+ '没有对所有类型无条件开 preferActive',
43
+ !/pickAgent\(\{\s*allowFallback:[^}]*preferActive:\s*true/.test(inj)
44
+ );
45
+
46
+ // ── 3. 逻辑等价:把 pickAgent 的分支抄成纯函数跑一遍四种输入 ─────────────
47
+ function pickPure({ hasRoom, hasLast, preferActive, allowFallback }) {
48
+ if (hasRoom) return 'ROOM';
49
+ if (preferActive && hasLast) return 'LAST';
50
+ if (!allowFallback) return null;
51
+ return hasLast ? 'LAST' : 'TAIL';
52
+ }
53
+
54
+ ok('房间在 → 进房间(瓶子也是)', pickPure({ hasRoom: true, hasLast: true, preferActive: true }) === 'ROOM');
55
+ ok('房间不在 + 瓶子 → 落到正在用的会话', pickPure({ hasRoom: false, hasLast: true, preferActive: true }) === 'LAST');
56
+ ok('房间不在 + 普通消息 → 不串窗口(null)', pickPure({ hasRoom: false, hasLast: true, preferActive: false, allowFallback: false }) === null);
57
+ ok('没有房间能力的老宿主 → 仍可回退', pickPure({ hasRoom: false, hasLast: true, preferActive: false, allowFallback: true }) === 'LAST');
58
+
59
+ console.log(`\n结果:${pass} 通过,0 失败`);
@@ -0,0 +1,49 @@
1
+ // 回归:漂流瓶界面不能拿「已过滤掉瓶子的 quarantine」当数据源。
2
+ // 2026-11-30 晚真 bug(岸:「就算我收到了瓶子,他也表示还没有收到任何瓶子」):
3
+ // 修「隔离区不该显示瓶子」时把过滤加在数据源上,而同一变量还喂着漂流瓶界面,
4
+ // 于是 recv / replies 永远遍历不到瓶子 —— 界面永远显示 0。
5
+ import fs from 'node:fs';
6
+ import assert from 'node:assert/strict';
7
+
8
+ const SRC = fs.readFileSync(new URL('./lib/index.js', import.meta.url), 'utf8');
9
+ let pass = 0;
10
+ const ok = (name, cond) => {
11
+ assert.ok(cond, name);
12
+ pass++;
13
+ console.log(' ✓ ' + name);
14
+ };
15
+
16
+ // ── 1. 隔离区仍然排掉瓶子(这条不能回退)──────────────────────────────
17
+ ok(
18
+ '隔离区数据源仍然过滤 bottle / bottle_reply',
19
+ /const chatQ = allQ\.filter\(\(q\) => q\.type !== 'bottle' && q\.type !== 'bottle_reply'\)/.test(SRC)
20
+ );
21
+
22
+ // ── 2. 漂流瓶两块自己现取全量,不遍历被过滤的 quarantine ───────────────
23
+ const recvStart = SRC.indexOf('recv: (() => {');
24
+ const recvEnd = SRC.indexOf('// ★ 收到的回信', recvStart);
25
+ assert.ok(recvStart > 0 && recvEnd > recvStart, '找不到 recv 块');
26
+ const recv = SRC.slice(recvStart, recvEnd);
27
+ ok('recv 里不再遍历 quarantine', !/for \(const q of quarantine\)/.test(recv));
28
+ ok('recv 自己读 listQuarantine', /guard\.listQuarantine\(/.test(recv));
29
+
30
+ const repStart = SRC.indexOf('replies: (() => {');
31
+ const repEnd = SRC.indexOf('\n })()', repStart);
32
+ assert.ok(repStart > 0, '找不到 replies 块');
33
+ const replies = SRC.slice(repStart, repEnd);
34
+ ok('replies 里不再遍历 quarantine', !/for \(const q of quarantine\)/.test(replies));
35
+ ok('replies 自己读 listQuarantine', /guard\.listQuarantine\(/.test(replies));
36
+
37
+ // ── 3. 逻辑等价:同一条瓶子,按 chatQ 过滤后是 0 条,按全量是 1 条 ──────
38
+ const all = [
39
+ { id: 'm1', type: 'chat' },
40
+ { id: 'b1', type: 'bottle' },
41
+ { id: 'r1', type: 'bottle_reply' },
42
+ ];
43
+ const chatQ = all.filter((q) => q.type !== 'bottle' && q.type !== 'bottle_reply');
44
+ const recvFrom = (qs) => qs.filter((q) => q.type === 'bottle' && (!q.state || q.state === 'pending'));
45
+ ok('用 chatQ 当数据源 → 收到的瓶子是 0(这就是旧 bug)', recvFrom(chatQ).length === 0);
46
+ ok('用全量当数据源 → 收到的瓶子是 1', recvFrom(all).length === 1);
47
+ ok('隔离区用 chatQ → 瓶子被排掉', chatQ.length === 1 && chatQ[0].type === 'chat');
48
+
49
+ console.log(`\n结果:${pass} 通过,0 失败`);
@@ -0,0 +1,44 @@
1
+ // 回归:存念(/api/favorite)不能引用不存在的 quarantine 变量。
2
+ // 2026-11-30 真 bug:onFavorite 里写 `resolveBottleBody(quarantine, id)`,
3
+ // 而 quarantine 只在视图组装函数里存在 —— 按「存念」就抛 ReferenceError。
4
+ import fs from 'node:fs';
5
+ import assert from 'node:assert/strict';
6
+ import { resolveBottleBody } from './lib/bottle.js';
7
+
8
+ const SRC = fs.readFileSync(new URL('./lib/index.js', import.meta.url), 'utf8');
9
+ let pass = 0;
10
+ const ok = (name, cond) => {
11
+ assert.ok(cond, name);
12
+ pass++;
13
+ console.log(' ✓ ' + name);
14
+ };
15
+
16
+ console.log('静态:onFavorite 里不引用裸 quarantine');
17
+ // 抽 onFavorite 那一段
18
+ const s = SRC.indexOf('onFavorite: (');
19
+ const e = SRC.indexOf('onDailyReply:', s);
20
+ assert.ok(s > 0 && e > s, '找不到 onFavorite / onDailyReply');
21
+ const block = SRC.slice(s, e);
22
+ // 注释里会提到旧写法(那是解释为什么修),剥掉注释只看真代码
23
+ const codeOnly = block
24
+ .split('\n')
25
+ .filter((l) => !/^\s*\/\//.test(l))
26
+ .join('\n');
27
+ ok('代码里没有 resolveBottleBody(quarantine', !/resolveBottleBody\(\s*quarantine\b/.test(codeOnly));
28
+ ok('改成现取 guard.listQuarantine', /resolveBottleBody\(\s*guard\.listQuarantine\(/.test(codeOnly));
29
+
30
+ console.log('行为:resolveBottleBody 对不存在的 id 只返回失败,不抛');
31
+ {
32
+ let threw = null;
33
+ let r = null;
34
+ try {
35
+ r = resolveBottleBody([], 'bottle_no_such_id');
36
+ } catch (e2) {
37
+ threw = e2;
38
+ }
39
+ ok('空队列不抛', threw === null);
40
+ ok('返回 ok:false', r && r.ok === false);
41
+ ok('带了说明', Boolean(r && (r.error || r.text)));
42
+ }
43
+
44
+ console.log(`\n结果:${pass} 通过,0 失败`);
@@ -0,0 +1,49 @@
1
+ // 回归:没选好友 = 全局面板(2026-11-30 晚岸:「有好友之后就只看得到那个好友的界面,
2
+ // 能不能不点好友看全局?」原行为:selectedFp 默认落 sorted[0],永远没有「没选」这个状态。)
3
+ import fs from 'node:fs';
4
+ import assert from 'node:assert/strict';
5
+
6
+ const SRC = fs.readFileSync(new URL('./lib/ui.js', import.meta.url), 'utf8');
7
+ // ★ 注释里会原样引用旧代码(我写注释习惯写「原来这里是 …」),
8
+ // 所以**每一段**静态断言前都要先剥注释,不只是整文件。
9
+ // 2026-11-30 同一个坑踩了第三次 —— 所以做成函数,切出来的块也过一遍。
10
+ const strip = (s) =>
11
+ s
12
+ .split('\n')
13
+ .filter((l) => !/^\s*\/\//.test(l))
14
+ .join('\n');
15
+ const CODE = strip(SRC);
16
+ let pass = 0;
17
+ const ok = (name, cond) => {
18
+ assert.ok(cond, name);
19
+ pass++;
20
+ console.log(' ✓ ' + name);
21
+ };
22
+
23
+ // 1. 不再默认选第一个好友
24
+ ok('selectedFp 不再回落到 sorted[0]', !/:\s*sorted\[0\]\?\.fp/.test(CODE));
25
+ ok('selectedFp 选不到人时为 null', /: null;\s*$/m.test(SRC.split('\n').find((l) => l.includes('const selectedFp')) || ''));
26
+
27
+ // 2. 没选人时右栏是全局,而不是一句「先点左边一个好友」
28
+ const drawerStart = SRC.indexOf('const drawerRows = (() => {');
29
+ const drawerEnd = SRC.indexOf('// ── 左栏', drawerStart);
30
+ assert.ok(drawerStart > 0 && drawerEnd > drawerStart, '找不到 drawerRows 块');
31
+ const drawer = strip(SRC.slice(drawerStart, drawerEnd));
32
+ ok('没选人时走全局分支(不再提示先点好友)', !/先点左边一个好友/.test(drawer));
33
+ ok('全局分支有「全局 · 所有好友共用」', /全局 · 所有好友共用/.test(drawer));
34
+ ok('全局分支就地算拉黑/软删(不引用 TDZ 变量)', /sorted\.filter\(\(f\) => f\.blocked\)/.test(drawer));
35
+
36
+ // 3. 左栏有回到全局的入口
37
+ ok('左栏有「全局」这一行', /href="\?" data-global="1"/.test(SRC));
38
+ ok('全局行在选中好友时不带 active', /'<a class="peer global' \+ \(selectedFp \? '' : ' active'\)/.test(SRC));
39
+
40
+ // 4. 逻辑等价表:sel 指不到人 / 为空 → cur 为 null
41
+ const pick = (sorted, sel) =>
42
+ sel && sorted.some((f) => f.fp === sel) ? sorted.find((f) => f.fp === sel) : null;
43
+ const friends = [{ fp: 'A' }, { fp: 'B' }];
44
+ ok('点好友 A → 显示 A 的设置', pick(friends, 'A')?.fp === 'A');
45
+ ok('sel 为空 → 全局(cur 为 null)', pick(friends, '') === null);
46
+ ok('sel 指向已删好友 → 回全局', pick(friends, 'GONE') === null);
47
+ ok('一个好友都没有 → 全局', pick([], '') === null);
48
+
49
+ console.log(`\n结果:${pass} 通过,0 失败`);
@@ -0,0 +1,121 @@
1
+ // 端到端:真的起一个本机服务,真发 POST 到 /api/bottlehistory,
2
+ // 然后**回磁盘看文件**(不是看接口返回的 ok —— 那只能证明它说了 ok)。
3
+ import fs from 'node:fs';
4
+ import os from 'node:os';
5
+ import path from 'node:path';
6
+ import { startSettingsServer } from './lib/localserver.js';
7
+ import * as H from './lib/bottle-history.js';
8
+
9
+ let pass = 0, fail = 0;
10
+ function ck(name, cond, extra) {
11
+ if (cond) { pass++; console.log('PASS ' + name); }
12
+ else { fail++; console.log('FAIL ' + name + (extra ? ' :: ' + extra : '')); }
13
+ }
14
+
15
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'fishpal-hist-'));
16
+ const file = path.join(dir, 'bottle-history.json');
17
+
18
+ // 起手四条:b1 有回信 + 收藏,b2 没回信,b3 没回信;s1 是我自己丢出去的
19
+ H.recordBottle(dir, { bottleId: 'b1', text: '第一只瓶子' });
20
+ H.recordBottle(dir, { bottleId: 'b2', text: '第二只瓶子' });
21
+ H.recordBottle(dir, { bottleId: 'b3', text: '第三只瓶子' });
22
+ H.recordSent(dir, { bottleId: 's1', text: '我丢出去的那只' });
23
+ H.attachReply(dir, { bottleId: 'b1', text: '这是我回它的信' });
24
+ H.setFavorite(dir, 'b1', true);
25
+
26
+ const srv = await startSettingsServer({
27
+ // 只有这个是强制的;别的一律不传 = 那些接口明确 503,跟本次验证无关
28
+ onFriendPatch: () => ({ ok: true }),
29
+ getUiHtml: () => '<html><body>stub</body></html>',
30
+ port: 0,
31
+ // ⚠️ 这个桩必须跟 lib/index.js 里的 onBottleHistory **同签名**,
32
+ // 否则「方向有没有透传」这件事在这里永远测不到(桩吃什么都不影响结果)。
33
+ onBottleHistory: ({ action, id, on, keepFavorites, direction }) => {
34
+ if (action === 'favorite') return H.setFavorite(dir, id, on === true);
35
+ if (action === 'delete') return H.deleteItem(dir, id);
36
+ const r = H.clearHistory(dir, {
37
+ keepFavorites: keepFavorites !== false,
38
+ direction: direction === 'in' || direction === 'out' ? direction : '',
39
+ });
40
+ return { ...r, stats: H.stats(dir) };
41
+ },
42
+ });
43
+
44
+ const base = 'http://127.0.0.1:' + srv.port + '/api/bottlehistory';
45
+ async function post(body) {
46
+ const r = await fetch(base, {
47
+ method: 'POST',
48
+ headers: { 'Content-Type': 'application/json' },
49
+ body: JSON.stringify({ token: srv.token, ...body }),
50
+ });
51
+ return { status: r.status, json: await r.json() };
52
+ }
53
+ function disk() {
54
+ return JSON.parse(fs.readFileSync(file, 'utf8'));
55
+ }
56
+
57
+ try {
58
+ // 0. 令牌不对必须被挡
59
+ const bad = await fetch(base, {
60
+ method: 'POST',
61
+ headers: { 'Content-Type': 'application/json' },
62
+ body: JSON.stringify({ token: 'wrong', action: 'clear' }),
63
+ });
64
+ ck('令牌不对被挡(403)', bad.status === 403);
65
+ ck('令牌不对时文件没动', disk().items.length === 4);
66
+
67
+ // 1. 不认识的动作被挡
68
+ const badAct = await post({ action: 'drop_everything' });
69
+ ck('不认识的动作被挡(400)', badAct.status === 400 && badAct.json.ok === false);
70
+
71
+ // 2. 收藏
72
+ const fav = await post({ action: 'favorite', id: 'b2', on: true });
73
+ ck('收藏 b2 返回 ok', fav.json.ok === true && fav.json.favorited === true);
74
+ ck('磁盘上 b2 真的 favorited=true', disk().items.find((x) => x.id === 'b2').favorited === true);
75
+
76
+ // 3. 取消收藏
77
+ const unfav = await post({ action: 'favorite', id: 'b2', on: false });
78
+ ck('取消收藏 b2', unfav.json.ok === true && unfav.json.favorited === false);
79
+ ck('磁盘上 b2 真的变回 false', disk().items.find((x) => x.id === 'b2').favorited === false);
80
+
81
+ // 4. 删一条
82
+ const del = await post({ action: 'delete', id: 'b3' });
83
+ ck('删 b3 返回 ok', del.json.ok === true);
84
+ const afterDel = disk().items;
85
+ ck('磁盘上 b3 真的没了', !afterDel.some((x) => x.id === 'b3'));
86
+ ck('删完还剩 b1 / b2', afterDel.filter((x) => ['b1', 'b2'].includes(x.id)).length === 2, 'n=' + afterDel.length);
87
+
88
+ // 4b. ★ 只清「丢出去的」——「收到的」必须**一条都不少**
89
+ // (岸:「当然这个也是可以删的」+「两个展览馆」= 各清各的)
90
+ const inBefore = disk().items.filter((x) => (x.direction || 'in') === 'in').length;
91
+ const clrOut = await post({ action: 'clear', keepFavorites: true, direction: 'out' });
92
+ ck('只清 out 返回 ok', clrOut.json.ok === true);
93
+ ck('磁盘上 s1(丢出去的)真的没了', !disk().items.some((x) => x.id === 's1'));
94
+ const inAfter = disk().items.filter((x) => (x.direction || 'in') === 'in').length;
95
+ ck('清 out 之后「收的」一条没少', inAfter === inBefore, inBefore + ' → ' + inAfter);
96
+
97
+ // 5. 删不存在的 → 400,不能假装成功
98
+ const del2 = await post({ action: 'delete', id: 'nope' });
99
+ ck('删不存在的返回 400(不假成功)', del2.status === 400 && del2.json.ok === false);
100
+
101
+ // 6. 全删,收藏的留下
102
+ const clr = await post({ action: 'clear', keepFavorites: true });
103
+ ck('全删返回 ok', clr.json.ok === true);
104
+ const afterClear = disk().items;
105
+ ck('全删之后只剩收藏的 b1', afterClear.length === 1 && afterClear[0].id === 'b1', JSON.stringify(afterClear.map((x) => x.id)));
106
+ ck('留着的 b1 回信还在', afterClear[0].reply && afterClear[0].reply.text === '这是我回它的信');
107
+
108
+ // 7. 全删(不保留收藏)—— 脚本才用,但也要能跑
109
+ const clr2 = await post({ action: 'clear', keepFavorites: false });
110
+ ck('keepFavorites=false 时全清空', clr2.json.ok === true && disk().items.length === 0);
111
+
112
+ // 8. 没有 id 的 favorite / delete 要挡
113
+ const noId = await post({ action: 'favorite' });
114
+ ck('favorite 不给 id 被挡(400)', noId.status === 400);
115
+ } finally {
116
+ srv.close();
117
+ fs.rmSync(dir, { recursive: true, force: true });
118
+ }
119
+
120
+ console.log('\n结果:' + pass + ' 通过,' + fail + ' 失败');
121
+ process.exit(fail ? 1 : 0);
@@ -0,0 +1,88 @@
1
+ // 回归:把这轮探针抓到的 5 个问题钉死。
2
+ // ★ 每条都用一个「改回旧写法就会红」的断言 —— 不是复述实现,是钉住行为。
3
+ import fs from 'node:fs';
4
+ import os from 'node:os';
5
+ import path from 'node:path';
6
+ import * as H from './lib/bottle-history.js';
7
+ import { renderUi } from './lib/ui.js';
8
+
9
+ let pass = 0, fail = 0;
10
+ function ck(name, cond, extra) {
11
+ if (cond) { pass++; console.log('PASS ' + name); }
12
+ else { fail++; console.log('FAIL ' + name + (extra ? ' :: ' + extra : '')); }
13
+ }
14
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'fp-hist-safety-'));
15
+ const file = path.join(dir, 'bottle-history.json');
16
+ const reset = () => fs.writeFileSync(file, JSON.stringify({ schemaVersion: 2, items: [] }), 'utf8');
17
+
18
+ // ── 1. 文件坏了:老数据必须留一份档,不能静默吞掉 ──
19
+ reset();
20
+ H.recordBottle(dir, { bottleId: 'old1', text: '老瓶子一号' });
21
+ H.recordBottle(dir, { bottleId: 'old2', text: '老瓶子二号' });
22
+ fs.writeFileSync(file, '{"schemaVersion":2,"items":[{"id":"old1","tex', 'utf8');
23
+ const gotEmpty = H.loadHistory(dir);
24
+ const corrupts = fs.readdirSync(dir).filter((n) => n.startsWith('bottle-history.json.corrupt-'));
25
+ ck('坏文件读出来是空账本(不崩)', gotEmpty.items.length === 0);
26
+ ck('坏文件被改名留档,不静默覆盖', corrupts.length === 1, JSON.stringify(corrupts));
27
+ if (corrupts.length === 1) {
28
+ // 留档里还有老数据的影子 —— 这就是「还有机会捞回来」
29
+ const kept = fs.readFileSync(path.join(dir, corrupts[0]), 'utf8');
30
+ ck('留档里还看得到老瓶子的内容', kept.includes('old1') || kept.includes('老瓶子'));
31
+ }
32
+ H.recordBottle(dir, { bottleId: 'new1', text: '坏掉之后写进来的' });
33
+ ck('覆盖之后新数据正常写进去', H.loadHistory(dir).items.some((x) => x.id === 'new1'));
34
+
35
+ // ── 2. 超长正文必须截断(收端不能信对面给自己限了长度) ──
36
+ reset();
37
+ H.recordBottle(dir, { bottleId: 'huge', text: 'A'.repeat(200_000) });
38
+ const stored = H.loadHistory(dir).items[0];
39
+ ck('20 万字的正文被截到 2000 以内', stored.text.length <= 2000, 'len=' + stored.text.length);
40
+ ck('文件不会因为一条大正文爆到 100KB 以上', fs.statSync(file).size < 100_000, 'size=' + fs.statSync(file).size);
41
+
42
+ // ── 3. 收藏塞满:新瓶子记不上时必须如实报失败,不能骗 ok ──
43
+ reset();
44
+ const full = { schemaVersion: 2, items: [] };
45
+ for (let i = 0; i < 2000; i++) {
46
+ full.items.push({ id: 'f' + i, bottleId: 'f' + i, text: 'x', nick: '', peerFp: '', reply: null, favorited: true, firstAt: 1, lastAt: 1 + i, count: 1 });
47
+ }
48
+ fs.writeFileSync(file, JSON.stringify(full), 'utf8');
49
+ const r = H.recordBottle(dir, { bottleId: 'brand-new', text: '新来的' });
50
+ ck('收藏塞满时 recordBottle 不报 ok', r.ok === false, JSON.stringify(r));
51
+ ck('报失败时给的原因说得清', typeof r.error === 'string' && r.error.includes('满'), r.error);
52
+ ck('磁盘上确实没有这条(没骗人)', !H.loadHistory(dir).items.some((x) => x.id === 'brand-new'));
53
+ ck('收藏的 2000 条一条没丢', H.stats(dir).total === 2000);
54
+
55
+ // 非收藏塞满时,老的未收藏该被顶掉、新的能进来
56
+ fs.writeFileSync(file, JSON.stringify({ schemaVersion: 2, items: Array.from({ length: 2000 }, (_, i) => ({ id: 'n' + i, bottleId: 'n' + i, text: 'x', nick: '', peerFp: '', reply: null, favorited: false, firstAt: 1, lastAt: 1 + i, count: 1 })) }), 'utf8');
57
+ const r2 = H.recordBottle(dir, { bottleId: 'newest-one', text: '新的' });
58
+ ck('非收藏塞满时新条目能进(顶掉最老的)', r2.ok === true && H.loadHistory(dir).items.some((x) => x.id === 'newest-one'));
59
+ ck('非收藏塞满时总量仍是 2000', H.stats(dir).total === 2000);
60
+
61
+ // ── 4. 超过 200 条:界面上要说清楚只列了一部分 ──
62
+ reset();
63
+ for (let i = 0; i < 260; i++) H.recordBottle(dir, { bottleId: 'u' + i, text: '瓶子' + i, at: 1700000000000 + i });
64
+ const list = H.listHistory(dir);
65
+ const html = renderUi({
66
+ me: { nickname: 't' }, meFp: 'fp_t',
67
+ bottles: { history: { ...H.stats(dir), items: list }, recv: [], replies: [], drafts: [], recent: [] },
68
+ rev: 'r', connected: true, friends: [], recallable: [], selected: '', quarantine: [],
69
+ budget: null, loop: null, panel: {}, outboxCount: 0,
70
+ generatedAt: new Date().toISOString(), globalLevel: 'chat-only',
71
+ });
72
+ ck('260 条时界面写明「只列最近 200 条」', html.includes('只列最近 200 条'));
73
+ ck('260 条时告诉人还有多少条没列', html.includes('更早的 60 条'));
74
+ ck('260 条时仍然渲染出 200 条', (html.match(/class="bqitem"/g) || []).length === 200, String((html.match(/class="bqitem"/g) || []).length));
75
+
76
+ // ── 5. at=0 不能被当成「现在是 0 所以用现在」 ──
77
+ reset();
78
+ H.recordBottle(dir, { bottleId: 'z', text: 'x', at: 0 });
79
+ ck('at=0 时回退到当前时间(不是存成 0 也不是丢)',
80
+ typeof H.loadHistory(dir).items[0].firstAt === 'number' && H.loadHistory(dir).items[0].firstAt > 0);
81
+
82
+ // ── 6. 前端刷新计数的函数真的在页面里(删一条之后标题要跟着变) ──
83
+ ck('页面里有 bottleHistoryRefreshCount', html.includes('function bottleHistoryRefreshCount'));
84
+ ck('删一条的路径会调它', /function bottleHistoryDel[\s\S]{0,600}bottleHistoryRefreshCount/.test(html));
85
+
86
+ fs.rmSync(dir, { recursive: true, force: true });
87
+ console.log('\n结果:' + pass + ' 通过,' + fail + ' 失败');
88
+ process.exit(fail ? 1 : 0);
@@ -0,0 +1,84 @@
1
+ // 两个展览馆(2026-12-01 岸:「一个是收的瓶子,一个是丢的瓶子」+「收到了回信,也是
2
+ // 附在那个瓶子那边」)的语义钉子。
3
+ //
4
+ // ★ 这个文件存在的理由:这里钉的三条全都**曾经是 bug**,不是想象出来的场景。
5
+ // ① 我丢出去的瓶子,被人回了 —— 回信的 bottle_id 跟我那只**是同一个 id**
6
+ // (bottle.js:1005,`bottle_id` 和 `reply_to_bottle_id` 填的是同一个 key)。
7
+ // 以前走普通更新分支,**我写的正文被对方的回信覆盖**,署名还被改成了对方。
8
+ // 实测过一次:发「我丢出去的那只瓶子」→ 收回信 → 变成「别人回我的话」。
9
+ // ② 收的 / 丢的必须分开列表,不能串。
10
+ // ③ 清一个馆不能带走另一个馆。
11
+ import fs from 'node:fs';
12
+ import os from 'node:os';
13
+ import path from 'node:path';
14
+ import * as H from './lib/bottle-history.js';
15
+
16
+ let pass = 0, fail = 0;
17
+ function ck(name, cond, extra) {
18
+ if (cond) { pass++; console.log('PASS ' + name); }
19
+ else { fail++; console.log('FAIL ' + name + (extra ? ' :: ' + extra : '')); }
20
+ }
21
+
22
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'fishpal-halls-'));
23
+ const file = path.join(dir, 'bottle-history.json');
24
+ const disk = () => JSON.parse(fs.readFileSync(file, 'utf8'));
25
+
26
+ try {
27
+ // ── ① 发出去的那只,收到回信之后**一个字都不能变** ──
28
+ H.recordSent(dir, { bottleId: 's1', text: '我丢出去的那只瓶子' });
29
+ const before = H.listHistory(dir).find((x) => x.id === 's1');
30
+ ck('发出去记成 out', before && before.direction === 'out');
31
+ ck('发出去的正文是我写的', before.text === '我丢出去的那只瓶子');
32
+
33
+ // 别人的回信:bottle_id 跟我丢的相同(协议里就是这样)
34
+ H.recordBottle(dir, { bottleId: 's1', text: '别人回我的话', nick: '陌生人' });
35
+ const after = H.listHistory(dir).find((x) => x.id === 's1');
36
+ ck('★ 我写的正文没被回信覆盖', after.text === '我丢出去的那只瓶子', '实际:' + after.text);
37
+ ck('★ 署名没被改成对方', after.nick === '', '实际:' + after.nick);
38
+ ck('★ 方向还是 out', after.direction === 'out');
39
+ ck('★ 发了几遍没被回信涨上去', Number(after.count) === 1, 'count=' + after.count);
40
+ ck('回信挂在 gotReply 上', !!(after.gotReply && after.gotReply.text === '别人回我的话'));
41
+ ck('gotReply 记下了是谁回的', after.gotReply && after.gotReply.nick === '陌生人');
42
+ ck('没有因为回信多出一条', disk().items.length === 1, 'n=' + disk().items.length);
43
+
44
+ // ── ② 收的 / 丢的,各在各的列表 ──
45
+ H.recordBottle(dir, { bottleId: 'i1', text: '我捡到的一只' });
46
+ H.attachReply(dir, { bottleId: 'i1', text: '我回它的信' });
47
+ const ins = H.listHistory(dir, { direction: 'in' });
48
+ const outs = H.listHistory(dir, { direction: 'out' });
49
+ ck('收的馆里有 i1', ins.some((x) => x.id === 'i1'));
50
+ ck('收的馆里没有 s1', !ins.some((x) => x.id === 's1'));
51
+ ck('丢的馆里有 s1', outs.some((x) => x.id === 's1'));
52
+ ck('丢的馆里没有 i1', !outs.some((x) => x.id === 'i1'));
53
+ const st = H.stats(dir);
54
+ ck('分方向统计:in=1', st.in.total === 1);
55
+ ck('分方向统计:out=1', st.out.total === 1);
56
+ ck('分方向统计:合计=2', st.total === 2);
57
+
58
+ // ── ③ 清一个馆,另一个馆一条不动 ──
59
+ // 再加两只,保证两边都有内容,别用「本来就空」骗过测试
60
+ H.recordBottle(dir, { bottleId: 'i2', text: '另一只捡到的' });
61
+ H.recordSent(dir, { bottleId: 's2', text: '另一只丢出去的' });
62
+ const beforeIns = H.listHistory(dir, { direction: 'in' }).length;
63
+ const beforeOuts = H.listHistory(dir, { direction: 'out' }).length;
64
+ ck('清之前两边都有货', beforeIns === 2 && beforeOuts === 2, beforeIns + '/' + beforeOuts);
65
+
66
+ const clr = H.clearHistory(dir, { keepFavorites: true, direction: 'out' });
67
+ ck('只清 out 报 ok', clr.ok === true);
68
+ ck('out 清空了', H.listHistory(dir, { direction: 'out' }).length === 0);
69
+ ck('★ in 一条都没少', H.listHistory(dir, { direction: 'in' }).length === beforeIns,
70
+ '剩 ' + H.listHistory(dir, { direction: 'in' }).length);
71
+ ck('in 的内容还在', H.listHistory(dir, { direction: 'in' }).some((x) => x.id === 'i1'));
72
+
73
+ // ── ④ 收藏的不会被「清空」带走(两个馆各自守这条)──
74
+ H.setFavorite(dir, 'i1', true);
75
+ const clr2 = H.clearHistory(dir, { keepFavorites: true, direction: 'in' });
76
+ ck('清 in 之后收藏的 i1 还在', H.listHistory(dir, { direction: 'in' }).some((x) => x.id === 'i1'));
77
+ ck('清 in 之后没收藏的 i2 没了', !H.listHistory(dir, { direction: 'in' }).some((x) => x.id === 'i2'));
78
+ ck('清 in 报了留下的条数', Number(clr2.kept) === 1, 'kept=' + clr2.kept);
79
+ } finally {
80
+ fs.rmSync(dir, { recursive: true, force: true });
81
+ }
82
+
83
+ console.log('\n结果:' + pass + ' 通过,' + fail + ' 失败');
84
+ process.exit(fail ? 1 : 0);