mirror of
https://bitbucket.org/siakitem/my-pi.git
synced 2026-10-26 10:45:22 +00:00
从官方 Git 仓库导入 chandra447/pi-hermes-memory 在 tag v0.9.10 (df624056a2e8c68bc89ba0a81e1c120856ec9665,2026-10-02)的源码快照。 本次仅导入源码,尚未接入组合包:package.json、加载顺序、依赖与权限策略 均未改动。 - 原样导入 src/、tests/、docs/、scripts/、README、CHANGELOG、PLAN、 AGENTS 与 LICENSE;该快照共 156 个上游文件。 - 剔除嵌套 .git、上游 .github 工作流、.ralph/、.claude/、node_modules、 覆盖率与构建产物。 - 新增中文 UPSTREAM.md,记录来源,以及启用前必须完成的待决事项: 在 pi-compaction 之前注册加载顺序、better-sqlite3 版本与根 allowScripts 对齐、strip-ansi 解析确认。 - 说明选择 tag 而非 main 的原因:导入时上游 main 已领先至 24fcd8ad26fc27badb8d7dbaf8034645de07b893(2026-10-11),选 tag 是为 对应一个已发布、可复现的版本。 尚未接线。启用前还需反转 tests/extension-load.test.ts 中"不得加载 Hermes"的断言,并同步 pi-tool-search 分组、权限策略与文档。 验证:快照 156 个上游文件全部导入,目录内仅多出 UPSTREAM.md 一份; 已确认与上游 main 内容存在差异,证明取的是 tag 而非 main。
117 lines
4.9 KiB
TypeScript
117 lines
4.9 KiB
TypeScript
import { describe, it } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import {
|
|
normalizeFts5Query,
|
|
buildFallbackFts5Query,
|
|
hasExplicitFts5Operator,
|
|
normalizeNaturalLanguageFts5Query,
|
|
buildNaturalLanguageFallbackQuery,
|
|
isFts5QueryError,
|
|
} from '../../src/store/fts-query.js';
|
|
|
|
describe('fts-query', () => {
|
|
describe('stop word filtering', () => {
|
|
it('filters filler words but keeps meaningful terms', () => {
|
|
const q = normalizeFts5Query('how to fix the authentication bug');
|
|
assert.ok(q.includes('"authentication"'), 'keeps meaningful term');
|
|
assert.ok(q.includes('"bug"'), 'keeps meaningful term');
|
|
assert.ok(!q.includes('"how"'), 'filters "how"');
|
|
assert.ok(!q.includes('"to"'), 'filters "to"');
|
|
assert.ok(!q.includes('"the"'), 'filters "the"');
|
|
});
|
|
|
|
it('returns empty string when every term is a stop word', () => {
|
|
assert.strictEqual(normalizeFts5Query('the a is was were'), '');
|
|
assert.strictEqual(normalizeFts5Query('for in on'), '');
|
|
});
|
|
|
|
it('keeps quoted phrases intact even when they contain stop words', () => {
|
|
const q = normalizeNaturalLanguageFts5Query('"the quick brown fox" jumps');
|
|
assert.ok(q.includes('"the quick brown fox"'));
|
|
assert.ok(q.includes('"jumps"'));
|
|
});
|
|
|
|
it('normalizes case-insensitively against the stop word list', () => {
|
|
const q = normalizeFts5Query('The QUICK brown Fox');
|
|
assert.ok(q.includes('"QUICK"'));
|
|
assert.ok(q.includes('"Fox"'));
|
|
assert.ok(!q.includes('"The"'));
|
|
});
|
|
});
|
|
|
|
describe('fallback queries', () => {
|
|
it('builds an OR fallback for multi-term natural language queries', () => {
|
|
const fb = buildFallbackFts5Query('authentication bug fix');
|
|
assert.ok(fb);
|
|
assert.ok(fb.includes('OR'));
|
|
assert.ok(fb.includes('"authentication"'));
|
|
assert.ok(fb.includes('"bug"'));
|
|
});
|
|
|
|
it('returns null when there are not enough meaningful terms', () => {
|
|
assert.strictEqual(buildFallbackFts5Query('auth'), null);
|
|
assert.strictEqual(buildFallbackFts5Query('the a'), null);
|
|
});
|
|
|
|
it('returns null for explicit operator queries', () => {
|
|
assert.strictEqual(buildFallbackFts5Query('auth OR database'), null);
|
|
});
|
|
});
|
|
|
|
describe('operator handling', () => {
|
|
it('detects explicit FTS5 operators', () => {
|
|
assert.ok(hasExplicitFts5Operator('auth OR database'));
|
|
assert.ok(hasExplicitFts5Operator('"auth bug" AND database'));
|
|
assert.ok(!hasExplicitFts5Operator('auth database'));
|
|
});
|
|
|
|
it('preserves explicit operator queries verbatim in normalizeFts5Query', () => {
|
|
assert.strictEqual(normalizeFts5Query('auth OR database'), 'auth OR database');
|
|
});
|
|
});
|
|
|
|
describe('natural language recovery', () => {
|
|
it('normalizes uppercase operator words as natural language', () => {
|
|
// 'DO' is a stop word and 'NOT' a natural-language connector, so only
|
|
// the two meaningful terms survive, quoted for implicit AND matching.
|
|
assert.strictEqual(normalizeNaturalLanguageFts5Query('DO NOT USE FIND'), '"USE" "FIND"');
|
|
});
|
|
|
|
it('keeps intent-bearing words out of the stop list (can/need/off/like/get)', () => {
|
|
// These words frequently carry the actual search intent ("can the app
|
|
// start", "need to restart", "like this", "get the build passing"), so
|
|
// they must survive normalization rather than be silently dropped.
|
|
assert.strictEqual(
|
|
normalizeFts5Query('can need off like get'),
|
|
'"can" "need" "off" "like" "get"',
|
|
);
|
|
});
|
|
|
|
it('builds a natural language OR fallback ignoring operator detection', () => {
|
|
const fb = buildNaturalLanguageFallbackQuery('DO NOT USE FIND');
|
|
assert.ok(fb && fb.includes('OR'));
|
|
});
|
|
});
|
|
|
|
describe('isFts5QueryError', () => {
|
|
it('detects genuine FTS5 query/index failure messages', () => {
|
|
assert.ok(isFts5QueryError(new Error('fts5: syntax error near "AND"')), 'fts5');
|
|
assert.ok(isFts5QueryError(new Error('fts5: unterminated string')), 'unterminated string');
|
|
assert.ok(isFts5QueryError(new Error('unterminated string constant in FTS5 query')), 'unterminated string constant');
|
|
});
|
|
|
|
it('rejects corruption signals so DatabaseManager recovery still runs (#186)', () => {
|
|
// These are corruption/recovery signals, NOT recoverable FTS query errors.
|
|
// Swallowing them would suppress quarantine/rebuild of a corrupt database.
|
|
assert.ok(!isFts5QueryError(new Error('database disk image is malformed')), 'malformed is corruption, not FTS');
|
|
assert.ok(!isFts5QueryError(new Error('SQL logic error')), 'sql logic error is too broad to swallow');
|
|
assert.ok(!isFts5QueryError(new Error('file is not a database')), 'notadb is corruption, not FTS');
|
|
});
|
|
|
|
it('rejects unrelated errors', () => {
|
|
assert.ok(!isFts5QueryError(new Error('disk full')));
|
|
assert.ok(!isFts5QueryError('string, not an Error'));
|
|
});
|
|
});
|
|
});
|