|
| 1 | +// Issue #505:saveWithDedupe 的去重候选窗口固定 100 条,库一变大就静默不再合并。 |
| 2 | +// |
| 3 | +// 根因:候选集是 store.list({ type, limit: 100 }),order 默认 importance |
| 4 | +// ⇒「该类型最重要的 100 行」,与「找出同标题的那一行」无关。某类型里 importance |
| 5 | +// 高于待写行的行数一旦超过窗口,同标题行与待写行双双落在窗口外,find 恒不命中, |
| 6 | +// 于是走创建分支:不报错、不告警,合并语义随库增长而失效。 |
| 7 | +// |
| 8 | +// 这组用例刻意复刻报告里的实测形状(类型 1047 行、其中 importance=5 有 180 行), |
| 9 | +// 但用更小的数字达成同样的窗口溢出,避免测试本身变慢。 |
| 10 | +import test from "node:test"; |
| 11 | +import assert from "node:assert/strict"; |
| 12 | +import { createStore } from "../src/store.js"; |
| 13 | +import { createService } from "../src/service.js"; |
| 14 | + |
| 15 | +function freshService() { |
| 16 | + const store = createStore(":memory:"); |
| 17 | + const service = createService({ store, mirror: null, config: {} }); |
| 18 | + return { store, service }; |
| 19 | +} |
| 20 | + |
| 21 | +// 往某个类型里灌 n 条 importance=5 的行,把窗口挤满。 |
| 22 | +function crowdType(store, type, n) { |
| 23 | + for (let i = 0; i < n; i++) { |
| 24 | + store.save({ |
| 25 | + type, |
| 26 | + title: `crowd-${type}-${i}`, |
| 27 | + content: `padding row ${i}`, |
| 28 | + importance: 5 |
| 29 | + }); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +test("#505: 同类型 importance 更高的行超过窗口后,同标题第二次写入仍然合并", () => { |
| 34 | + const { store, service } = freshService(); |
| 35 | + // 窗口是 100。灌 120 条 importance=5,保证待写行(importance=3)与它的同标题 |
| 36 | + // 行都排不进前 100。 |
| 37 | + crowdType(store, "project", 120); |
| 38 | + |
| 39 | + const payload = { |
| 40 | + type: "project", |
| 41 | + title: "dedupe-target", |
| 42 | + content: "first content", |
| 43 | + importance: 3 |
| 44 | + }; |
| 45 | + const first = service.saveWithDedupe(payload); |
| 46 | + assert.equal(first.action, "created", "第一次写入应当是新建"); |
| 47 | + |
| 48 | + const second = service.saveWithDedupe({ ...payload, content: "second content" }); |
| 49 | + assert.equal( |
| 50 | + second.action, |
| 51 | + "merged", |
| 52 | + "第二次同标题写入必须并入已有行;返回 created 说明候选窗口把同标题行挡在窗口外(#505 静默降级)" |
| 53 | + ); |
| 54 | + assert.equal(second.memory.id, first.memory.id, "合并必须落在同一行"); |
| 55 | + |
| 56 | + const rows = store.list({ type: "project", limit: null }) |
| 57 | + .filter((m) => m.title.trim() === "dedupe-target"); |
| 58 | + assert.equal(rows.length, 1, "同标题只应存在一行"); |
| 59 | +}); |
| 60 | + |
| 61 | +test("#505: 定向候选不受 importance 分布影响(低 importance 的同标题行也能命中)", () => { |
| 62 | + const { store, service } = freshService(); |
| 63 | + // 先写一条 importance=1 的目标行,再用大量 importance=5 的行把它挤出窗口。 |
| 64 | + const first = service.saveWithDedupe({ |
| 65 | + type: "pattern", |
| 66 | + title: "low-importance-target", |
| 67 | + content: "seed", |
| 68 | + importance: 1 |
| 69 | + }); |
| 70 | + assert.equal(first.action, "created"); |
| 71 | + |
| 72 | + crowdType(store, "pattern", 120); |
| 73 | + |
| 74 | + const second = service.saveWithDedupe({ |
| 75 | + type: "pattern", |
| 76 | + title: "low-importance-target", |
| 77 | + content: "merged later", |
| 78 | + importance: 1 |
| 79 | + }); |
| 80 | + assert.equal(second.action, "merged", "低 importance 的同标题行同样必须能被找到"); |
| 81 | + assert.equal(second.memory.id, first.memory.id); |
| 82 | +}); |
| 83 | + |
| 84 | +test("#505: 标题首尾空白不影响去重命中(入库不 trim,比较 trim)", () => { |
| 85 | + const { store, service } = freshService(); |
| 86 | + crowdType(store, "constraint", 120); |
| 87 | + |
| 88 | + const first = service.saveWithDedupe({ |
| 89 | + type: "constraint", |
| 90 | + title: " spaced title ", |
| 91 | + content: "seed", |
| 92 | + importance: 3 |
| 93 | + }); |
| 94 | + assert.equal(first.action, "created"); |
| 95 | + |
| 96 | + // 同一个标题、首尾空白写法不同——按 trim 口径应当命中同一行。 |
| 97 | + const second = service.saveWithDedupe({ |
| 98 | + type: "constraint", |
| 99 | + title: "spaced title", |
| 100 | + content: "merged", |
| 101 | + importance: 3 |
| 102 | + }); |
| 103 | + assert.equal(second.action, "merged", "trim 后同标题必须合并,否则存量带空白的标题会退回静默降级"); |
| 104 | + assert.equal(second.memory.id, first.memory.id); |
| 105 | +}); |
| 106 | + |
| 107 | +test("#505: 定向候选不跨类型命中", () => { |
| 108 | + const { store, service } = freshService(); |
| 109 | + crowdType(store, "project", 120); |
| 110 | + |
| 111 | + const a = service.saveWithDedupe({ |
| 112 | + type: "project", |
| 113 | + title: "same-title-different-type", |
| 114 | + content: "in project", |
| 115 | + importance: 3 |
| 116 | + }); |
| 117 | + const b = service.saveWithDedupe({ |
| 118 | + type: "preference", |
| 119 | + title: "same-title-different-type", |
| 120 | + content: "in preference", |
| 121 | + importance: 3 |
| 122 | + }); |
| 123 | + assert.equal(a.action, "created"); |
| 124 | + assert.equal(b.action, "created", "不同 type 的同标题行不得合并"); |
| 125 | + assert.notEqual(a.memory.id, b.memory.id); |
| 126 | +}); |
| 127 | + |
| 128 | +test("#505: 归档行不作为合并目标(口径与 list 默认一致)", () => { |
| 129 | + const { store, service } = freshService(); |
| 130 | + crowdType(store, "decision", 120); |
| 131 | + |
| 132 | + const first = service.saveWithDedupe({ |
| 133 | + type: "decision", |
| 134 | + title: "archived-target", |
| 135 | + content: "seed", |
| 136 | + importance: 3 |
| 137 | + }); |
| 138 | + // 归档必须走 store.setArchived——store.update 的 SET 子句里没有 archived 列, |
| 139 | + // 用 update({archived:true}) 根本不会归档,测试会假绿。 |
| 140 | + store.setArchived(first.memory.id, true); |
| 141 | + |
| 142 | + const second = service.saveWithDedupe({ |
| 143 | + type: "decision", |
| 144 | + title: "archived-target", |
| 145 | + content: "after archive", |
| 146 | + importance: 3 |
| 147 | + }); |
| 148 | + assert.equal(second.action, "created", "已归档行不该被当作活跃合并目标"); |
| 149 | + assert.notEqual(second.memory.id, first.memory.id); |
| 150 | +}); |
0 commit comments