dsh-cortex-memory 0.4.146 → 0.4.147
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 +2396 -2369
- package/README.md +8 -6
- package/bench/semantic-cn.js +4 -4
- package/bench/synthetic.js +2 -2
- package/bin/copy-db.mjs +1 -1
- package/client.js +1 -1
- package/docs/quota-compaction-design.md +2 -2
- package/icon.svg +12 -2
- package/package.json +1 -1
- package/src/engines/consolidate.js +2 -2
- package/src/engines/health.js +1 -1
- package/src/engines/life.js +2 -2
- package/src/engines/narrative.js +2 -2
- package/src/host/scheduler.js +1 -1
- package/src/store/repositories/traces.js +1 -1
- package/test/integration/_helpers.js +0 -128
- package/test/integration/acceptance.test.js +0 -803
- package/test/integration/ann-load.test.js +0 -61
- package/test/integration/api-contract.test.js +0 -1476
- package/test/integration/backup-privacy.test.js +0 -214
- package/test/integration/consolidate.test.js +0 -79
- package/test/integration/edge-cases.test.js +0 -1089
- package/test/integration/exchange.test.js +0 -326
- package/test/integration/life-loop.test.js +0 -189
- package/test/integration/load.test.js +0 -751
- package/test/integration/markdown-sync.test.js +0 -35
- package/test/integration/policy-query-api.test.js +0 -306
- package/test/integration/resilience-writes.test.js +0 -241
- package/test/integration/tools-contract.test.js +0 -1324
- package/test/unit/alias.test.js +0 -87
- package/test/unit/ann-edge.test.js +0 -102
- package/test/unit/ann.test.js +0 -77
- package/test/unit/api-client.test.js +0 -576
- package/test/unit/api-validate.test.js +0 -19
- package/test/unit/api.test.js +0 -555
- package/test/unit/appraisal.test.js +0 -48
- package/test/unit/attachments.test.js +0 -33
- package/test/unit/auto-reflect.test.js +0 -141
- package/test/unit/autonomy.test.js +0 -53
- package/test/unit/batch.test.js +0 -46
- package/test/unit/bench.test.js +0 -40
- package/test/unit/benchmark-synthetic.test.js +0 -52
- package/test/unit/capture-filter.test.js +0 -296
- package/test/unit/cli-default-root.test.js +0 -21
- package/test/unit/client-contract.test.js +0 -32
- package/test/unit/compaction-preview.test.js +0 -91
- package/test/unit/compression.test.js +0 -76
- package/test/unit/consolidate-dedupe.test.js +0 -174
- package/test/unit/consolidate-errors.test.js +0 -63
- package/test/unit/consolidate-gate.test.js +0 -26
- package/test/unit/consolidate-preview.test.js +0 -168
- package/test/unit/consolidate-protection.test.js +0 -156
- package/test/unit/consolidate-reason.test.js +0 -206
- package/test/unit/consolidate-retry.test.js +0 -91
- package/test/unit/consolidate-selection.test.js +0 -103
- package/test/unit/core.test.js +0 -77
- package/test/unit/coverage-rewrite.test.js +0 -38
- package/test/unit/dedupe-episodes.test.js +0 -34
- package/test/unit/dedupe-threshold.test.js +0 -14
- package/test/unit/develop.test.js +0 -130
- package/test/unit/development.test.js +0 -68
- package/test/unit/digest.test.js +0 -26
- package/test/unit/dsr.test.js +0 -111
- package/test/unit/edges-candidates.test.js +0 -113
- package/test/unit/emotion.test.js +0 -62
- package/test/unit/encode.test.js +0 -98
- package/test/unit/evolution.test.js +0 -130
- package/test/unit/external-ann-search.test.js +0 -50
- package/test/unit/external-ann.test.js +0 -29
- package/test/unit/extract-question.test.js +0 -41
- package/test/unit/fact-guard.test.js +0 -51
- package/test/unit/fact-noise-llm.test.js +0 -44
- package/test/unit/facts-belief.test.js +0 -32
- package/test/unit/facts-compat.test.js +0 -62
- package/test/unit/facts.test.js +0 -107
- package/test/unit/feature-overrides.test.js +0 -42
- package/test/unit/fts-migration.test.js +0 -42
- package/test/unit/graph.test.js +0 -79
- package/test/unit/growth-reflect.test.js +0 -124
- package/test/unit/guard-runtime.test.js +0 -40
- package/test/unit/guardian.test.js +0 -91
- package/test/unit/health-consolidation.test.js +0 -52
- package/test/unit/health-fts-scope.test.js +0 -33
- package/test/unit/health-privacy.test.js +0 -102
- package/test/unit/health-summaries.test.js +0 -24
- package/test/unit/health-text-maintain.test.js +0 -33
- package/test/unit/health-today.test.js +0 -45
- package/test/unit/hnsw.test.js +0 -35
- package/test/unit/host-runtime.test.js +0 -142
- package/test/unit/host.test.js +0 -58
- package/test/unit/identity.test.js +0 -98
- package/test/unit/induct-evaluate.test.js +0 -41
- package/test/unit/ingest-extracted.test.js +0 -35
- package/test/unit/inhibition.test.js +0 -90
- package/test/unit/initiative.test.js +0 -90
- package/test/unit/jobs.test.js +0 -93
- package/test/unit/json-snapshot.test.js +0 -57
- package/test/unit/label-entities-skip.test.js +0 -18
- package/test/unit/life-consolidate.test.js +0 -34
- package/test/unit/life.test.js +0 -149
- package/test/unit/llm-facts-integration.test.js +0 -27
- package/test/unit/llm-facts.test.js +0 -16
- package/test/unit/llm.test.js +0 -45
- package/test/unit/locomo.test.js +0 -45
- package/test/unit/longmemeval.test.js +0 -56
- package/test/unit/maintain-dispute.test.js +0 -62
- package/test/unit/maintain-duration.test.js +0 -194
- package/test/unit/markdown-import.test.js +0 -44
- package/test/unit/markdown.test.js +0 -34
- package/test/unit/memory-edit.test.js +0 -112
- package/test/unit/metrics.test.js +0 -26
- package/test/unit/migration.test.js +0 -38
- package/test/unit/model-config.test.js +0 -52
- package/test/unit/mood-saturation.test.js +0 -33
- package/test/unit/narrative-weave.test.js +0 -271
- package/test/unit/narrative.test.js +0 -62
- package/test/unit/playbook-consolidate.test.js +0 -24
- package/test/unit/privacy-scope.test.js +0 -39
- package/test/unit/profile-paths.test.js +0 -21
- package/test/unit/profile.test.js +0 -26
- package/test/unit/prompt.test.js +0 -42
- package/test/unit/providers.test.js +0 -42
- package/test/unit/qa.test.js +0 -100
- package/test/unit/query.test.js +0 -53
- package/test/unit/reflect-backfill.test.js +0 -158
- package/test/unit/reflect-coverage.test.js +0 -35
- package/test/unit/reflect-overview.test.js +0 -22
- package/test/unit/reflect-phrasing.test.js +0 -75
- package/test/unit/reflect-sampling.test.js +0 -80
- package/test/unit/reflect-source-budget.test.js +0 -24
- package/test/unit/relationship-dynamics.test.js +0 -29
- package/test/unit/retention.test.js +0 -31
- package/test/unit/retrieval-ranking.test.js +0 -31
- package/test/unit/retrieve.test.js +0 -117
- package/test/unit/revive-on-read.test.js +0 -84
- package/test/unit/rotation.test.js +0 -25
- package/test/unit/scanner.test.js +0 -50
- package/test/unit/scenario.test.js +0 -78
- package/test/unit/scheduler-autoconsolidate.test.js +0 -95
- package/test/unit/scheduler-autodedupe.test.js +0 -52
- package/test/unit/scheduler-autosleep.test.js +0 -50
- package/test/unit/scheduler-cadence.test.js +0 -33
- package/test/unit/scheduler-deep.test.js +0 -62
- package/test/unit/scheduler-tick-gate.test.js +0 -38
- package/test/unit/scheduler.test.js +0 -137
- package/test/unit/self-state.test.js +0 -94
- package/test/unit/service.test.js +0 -72
- package/test/unit/shiori-import.test.js +0 -77
- package/test/unit/skills.test.js +0 -104
- package/test/unit/spacing.test.js +0 -14
- package/test/unit/store.test.js +0 -111
- package/test/unit/summon-left-slot.test.js +0 -51
- package/test/unit/summon-rank.test.js +0 -51
- package/test/unit/temporal-graph.test.js +0 -44
- package/test/unit/tiers-gist.test.js +0 -140
- package/test/unit/tiers.test.js +0 -39
- package/test/unit/tokenizer-fts.test.js +0 -35
- package/test/unit/tools.test.js +0 -122
- package/test/unit/upgrade-0412.test.js +0 -104
- package/test/unit/upgrade-0413.test.js +0 -84
- package/test/unit/upgrade-0414.test.js +0 -101
- package/test/unit/vector-ann.test.js +0 -53
- package/test/unit/vector-async.test.js +0 -69
- package/test/unit/vector-coverage.test.js +0 -34
- package/test/unit/vector-override.test.js +0 -37
- package/test/unit/vector-typed-array.test.js +0 -47
- package/test/unit/vector.test.js +0 -68
- package/test/unit/working.test.js +0 -99
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { createScheduler } from '../../src/host/scheduler.js';
|
|
4
|
-
|
|
5
|
-
const NOW = Date.parse('2026-11-16T08:00:00.000Z');
|
|
6
|
-
|
|
7
|
-
function fakeCortex(lastRunAt) {
|
|
8
|
-
const runs = [];
|
|
9
|
-
return {
|
|
10
|
-
runs,
|
|
11
|
-
config: { scope: 'global', deepConsolidateOnSchedule: false, features: { llm: true, consolidateOnTick: true, consolidateTraceGapMinutes: 30 } },
|
|
12
|
-
repos: { traces: { listSince: () => [] } },
|
|
13
|
-
consolidation: {
|
|
14
|
-
list: () => (lastRunAt ? [{ at: new Date(lastRunAt).toISOString() }] : []),
|
|
15
|
-
run: async () => {
|
|
16
|
-
runs.push(1);
|
|
17
|
-
return { run: { status: 'applied', model: 'llm-bounded', error: null }, decisions: [{ action: 'keep' }], applied: [] };
|
|
18
|
-
},
|
|
19
|
-
},
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
test('tick 开着也不会一分钟一次:间隔不够就跳过', async () => {
|
|
24
|
-
const cortex = fakeCortex(NOW - 5 * 60_000);
|
|
25
|
-
const scheduler = createScheduler({ cortex, now: () => NOW, logger: null, consolidateOnTick: true });
|
|
26
|
-
const report = await scheduler.runOnce();
|
|
27
|
-
assert.equal(cortex.runs.length, 0);
|
|
28
|
-
assert.equal(report.steps.consolidation.skipped, 'too-soon');
|
|
29
|
-
assert.equal(report.steps.consolidation.minutesSinceLast, 5);
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
test('tick 开着且间隔已过,才真的跑一次', async () => {
|
|
33
|
-
const cortex = fakeCortex(NOW - 40 * 60_000);
|
|
34
|
-
const scheduler = createScheduler({ cortex, now: () => NOW, logger: null, consolidateOnTick: true });
|
|
35
|
-
const report = await scheduler.runOnce();
|
|
36
|
-
assert.equal(cortex.runs.length, 1);
|
|
37
|
-
assert.equal(report.steps.consolidation.run.status, 'applied');
|
|
38
|
-
});
|
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { mkdtempSync, rmSync } from 'node:fs';
|
|
4
|
-
import os from 'node:os';
|
|
5
|
-
import path from 'node:path';
|
|
6
|
-
import { createCortex } from '../../src/service/cortex.js';
|
|
7
|
-
import { createScheduler } from '../../src/host/scheduler.js';
|
|
8
|
-
|
|
9
|
-
test('scheduler runOnce is safe and reports steps', async () => {
|
|
10
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-scheduler-'));
|
|
11
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') } });
|
|
12
|
-
try {
|
|
13
|
-
cortex.write({ text: '一条记忆。', explicit: true });
|
|
14
|
-
const scheduler = createScheduler({ cortex, intervalMs: 60_000 });
|
|
15
|
-
const report = await scheduler.runOnce();
|
|
16
|
-
assert.ok(report.steps.autonomy);
|
|
17
|
-
assert.ok(report.steps.guardian);
|
|
18
|
-
assert.equal(report.steps.guardian.ok, true);
|
|
19
|
-
assert.equal(scheduler.status().running, false);
|
|
20
|
-
} finally {
|
|
21
|
-
cortex.close();
|
|
22
|
-
rmSync(dir, { recursive: true, force: true });
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
test('scheduler can run consolidation on a tick when explicitly enabled', async () => {
|
|
27
|
-
let called = 0;
|
|
28
|
-
const scheduler = createScheduler({
|
|
29
|
-
// 这个测试只验证 tick 巩固;深整理另有专门的测试,关掉它避免多记一次。
|
|
30
|
-
cortex: { autonomy: null, retention: null, guardian: null, runner: null, config: { scope: 'global', deepConsolidateOnSchedule: false }, consolidation: { run: async () => { called += 1; return { run: { status: 'applied' } }; } } },
|
|
31
|
-
consolidateOnTick: true,
|
|
32
|
-
intervalMs: 60_000,
|
|
33
|
-
});
|
|
34
|
-
const report = await scheduler.runOnce();
|
|
35
|
-
assert.equal(called, 1);
|
|
36
|
-
assert.equal(report.steps.consolidation.run.status, 'applied');
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test('scheduler writes an automatic diary at the 12:00 slot', async () => {
|
|
40
|
-
const calls = [];
|
|
41
|
-
const at = new Date(2026, 10, 12, 12, 0, 0).getTime();
|
|
42
|
-
const scheduler = createScheduler({
|
|
43
|
-
cortex: {
|
|
44
|
-
autonomy: null,
|
|
45
|
-
retention: null,
|
|
46
|
-
guardian: null,
|
|
47
|
-
runner: null,
|
|
48
|
-
config: { scope: 'global', features: { autoDiary: true, autoDream: false } },
|
|
49
|
-
scopeWritable: () => true,
|
|
50
|
-
life: {
|
|
51
|
-
diaryList: () => [],
|
|
52
|
-
diaryGenerate: async (args) => {
|
|
53
|
-
calls.push(args);
|
|
54
|
-
return { id: 'dly_test', text: '自动日记', model: 'llm', sourceCount: 1, source: args.source };
|
|
55
|
-
},
|
|
56
|
-
},
|
|
57
|
-
},
|
|
58
|
-
now: () => at,
|
|
59
|
-
isIdle: () => true,
|
|
60
|
-
intervalMs: 60_000,
|
|
61
|
-
});
|
|
62
|
-
const report = await scheduler.runOnce();
|
|
63
|
-
assert.equal(calls.length, 1);
|
|
64
|
-
assert.equal(calls[0].source, 'cortex:auto:slot:12');
|
|
65
|
-
assert.equal(report.steps.autoDiary.source, 'cortex:auto:slot:12');
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
test('scheduler writes an automatic feeling when idle', async () => {
|
|
69
|
-
const calls = [];
|
|
70
|
-
const at = new Date(2026, 10, 12, 13, 0, 0).getTime();
|
|
71
|
-
const scheduler = createScheduler({
|
|
72
|
-
cortex: {
|
|
73
|
-
autonomy: null,
|
|
74
|
-
retention: null,
|
|
75
|
-
guardian: null,
|
|
76
|
-
runner: null,
|
|
77
|
-
config: { scope: 'global', features: { autoDiary: false, autoDream: false, autoFeeling: true } },
|
|
78
|
-
scopeWritable: () => true,
|
|
79
|
-
emotion: { events: () => [] },
|
|
80
|
-
life: {
|
|
81
|
-
feelingGenerate: async (args) => {
|
|
82
|
-
calls.push(args);
|
|
83
|
-
return { text: '助手现在很安静。', emotions: [{ name: '安', score: 1 }], target: '用户', model: 'llm', sourceCount: 1 };
|
|
84
|
-
},
|
|
85
|
-
},
|
|
86
|
-
},
|
|
87
|
-
now: () => at,
|
|
88
|
-
isIdle: () => true,
|
|
89
|
-
intervalMs: 60_000,
|
|
90
|
-
});
|
|
91
|
-
const report = await scheduler.runOnce();
|
|
92
|
-
assert.equal(calls.length, 1);
|
|
93
|
-
assert.equal(calls[0].source, 'cortex:auto:feeling');
|
|
94
|
-
assert.equal(report.steps.autoFeeling.text, '助手现在很安静。');
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
test('scheduler asks the LLM to review candidate edges', async () => {
|
|
98
|
-
const actions = [];
|
|
99
|
-
const scheduler = createScheduler({
|
|
100
|
-
cortex: {
|
|
101
|
-
autonomy: null,
|
|
102
|
-
retention: null,
|
|
103
|
-
guardian: null,
|
|
104
|
-
runner: null,
|
|
105
|
-
config: { scope: 'global', features: { autoDiary: false, autoDream: false, autoFeeling: false, aiEdgeReview: true } },
|
|
106
|
-
scopeWritable: () => true,
|
|
107
|
-
graphCandidates: () => [{ id: 'edge-1', fromId: 'a', toId: 'b', rel: 'likes' }],
|
|
108
|
-
reviewGraphCandidate: async () => ({ decision: 'approve', confidence: 0.8, reason: 'evidence matches' }),
|
|
109
|
-
graphCandidateAction: (args) => { actions.push(args); return { id: args.id }; },
|
|
110
|
-
},
|
|
111
|
-
now: () => Date.now(),
|
|
112
|
-
isIdle: () => false,
|
|
113
|
-
intervalMs: 60_000,
|
|
114
|
-
});
|
|
115
|
-
const report = await scheduler.runOnce();
|
|
116
|
-
assert.equal(actions.length, 1);
|
|
117
|
-
assert.equal(actions[0].action, 'approve');
|
|
118
|
-
assert.equal(report.steps.aiEdgeReview.reviewed, 1);
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
test('scheduler start/stop uses an unref timer and does not double start', () => {
|
|
122
|
-
const timers = [];
|
|
123
|
-
const cleared = [];
|
|
124
|
-
const scheduler = createScheduler({
|
|
125
|
-
cortex: { autonomy: null, retention: null, guardian: null, runner: null },
|
|
126
|
-
intervalMs: 5000,
|
|
127
|
-
setIntervalImpl: (fn, ms) => { const timer = { fn, ms, unref() {} }; timers.push(timer); return timer; },
|
|
128
|
-
clearIntervalImpl: (timer) => { cleared.push(timer); },
|
|
129
|
-
});
|
|
130
|
-
assert.equal(scheduler.start(), true);
|
|
131
|
-
assert.equal(scheduler.start(), false);
|
|
132
|
-
assert.equal(timers.length, 1);
|
|
133
|
-
assert.equal(scheduler.status().running, true);
|
|
134
|
-
assert.equal(scheduler.stop(), true);
|
|
135
|
-
assert.equal(cleared.length, 1);
|
|
136
|
-
assert.equal(scheduler.status().running, false);
|
|
137
|
-
});
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { mkdtempSync, rmSync } from 'node:fs';
|
|
4
|
-
import os from 'node:os';
|
|
5
|
-
import path from 'node:path';
|
|
6
|
-
import { openDatabase, migrate, closeDatabase } from '../../src/store/database.js';
|
|
7
|
-
import {
|
|
8
|
-
DEFAULT_DRIVES,
|
|
9
|
-
driftDrives,
|
|
10
|
-
applyDriveDeltas,
|
|
11
|
-
driftMood,
|
|
12
|
-
applyMood,
|
|
13
|
-
defaultSelfState,
|
|
14
|
-
createSelfStore,
|
|
15
|
-
} from '../../src/self/state.js';
|
|
16
|
-
|
|
17
|
-
test('drives drift toward baseline and never leave their bounds', () => {
|
|
18
|
-
const low = { ...DEFAULT_DRIVES, care: 0.1 };
|
|
19
|
-
const after = driftDrives(low, { elapsedHours: 24 });
|
|
20
|
-
assert.ok(after.care > 0.1 && after.care <= 1);
|
|
21
|
-
const near = driftDrives({ ...DEFAULT_DRIVES, care: 0.61 }, { elapsedHours: 1 });
|
|
22
|
-
assert.ok(Math.abs(near.care - 0.6) < 0.1);
|
|
23
|
-
const clamped = applyDriveDeltas(DEFAULT_DRIVES, { care: 5, curiosity: -5 });
|
|
24
|
-
assert.equal(clamped.care, 1);
|
|
25
|
-
assert.equal(clamped.curiosity, 0);
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
test('mood drifts to baseline and appraisal moves it', () => {
|
|
29
|
-
const sad = { valence: -1, arousal: 0.9, energy: 0.2 };
|
|
30
|
-
const rested = driftMood(sad, { elapsedHours: 6 });
|
|
31
|
-
assert.ok(rested.valence > -1);
|
|
32
|
-
assert.ok(rested.energy > 0.2);
|
|
33
|
-
const moved = applyMood({ valence: 0, arousal: 0.2, energy: 0.6 }, { valence: 0.8, arousal: 0.4, salience: 0.9 });
|
|
34
|
-
assert.ok(moved.valence > 0);
|
|
35
|
-
assert.ok(moved.arousal > 0.2);
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
test('self store persists drives, mood and attention', () => {
|
|
39
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-self-'));
|
|
40
|
-
const { db } = openDatabase({ dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') });
|
|
41
|
-
migrate(db);
|
|
42
|
-
try {
|
|
43
|
-
const store = createSelfStore(db);
|
|
44
|
-
const initial = store.get('global');
|
|
45
|
-
assert.equal(initial.stage, 'infancy');
|
|
46
|
-
assert.equal(initial.drives.care, DEFAULT_DRIVES.care);
|
|
47
|
-
const updated = store.update('global', {
|
|
48
|
-
drives: { care: 0.95 },
|
|
49
|
-
mood: { valence: 0.5 },
|
|
50
|
-
attention: { focus: '用户' },
|
|
51
|
-
});
|
|
52
|
-
assert.equal(updated.drives.care, 0.95);
|
|
53
|
-
assert.equal(updated.mood.valence, 0.5);
|
|
54
|
-
assert.equal(updated.attention.focus, '用户');
|
|
55
|
-
const reloaded = store.get('global');
|
|
56
|
-
assert.equal(reloaded.drives.care, 0.95);
|
|
57
|
-
assert.equal(reloaded.attention.focus, '用户');
|
|
58
|
-
} finally {
|
|
59
|
-
closeDatabase(db);
|
|
60
|
-
rmSync(dir, { recursive: true, force: true });
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
test('appraisal moves drives in the expected direction', () => {
|
|
65
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-self-appraisal-'));
|
|
66
|
-
const { db } = openDatabase({ dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') });
|
|
67
|
-
migrate(db);
|
|
68
|
-
try {
|
|
69
|
-
const store = createSelfStore(db);
|
|
70
|
-
const before = store.get('global');
|
|
71
|
-
const after = store.applyAppraisal('global', {
|
|
72
|
-
novelty: 0.9,
|
|
73
|
-
care: 0.8,
|
|
74
|
-
valence: 0.7,
|
|
75
|
-
arousal: 0.6,
|
|
76
|
-
salience: 0.9,
|
|
77
|
-
driveDeltas: { curiosity: 0.15, care: 0.1, coherence: -0.05 },
|
|
78
|
-
});
|
|
79
|
-
assert.ok(after.drives.curiosity > before.drives.curiosity);
|
|
80
|
-
assert.ok(after.drives.care > before.drives.care);
|
|
81
|
-
assert.ok(after.mood.valence > before.mood.valence);
|
|
82
|
-
} finally {
|
|
83
|
-
closeDatabase(db);
|
|
84
|
-
rmSync(dir, { recursive: true, force: true });
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
test('default self state is a complete snapshot', () => {
|
|
89
|
-
const state = defaultSelfState('global');
|
|
90
|
-
assert.equal(state.scopeId, 'global');
|
|
91
|
-
assert.ok(state.drives.coherence >= 0);
|
|
92
|
-
assert.ok(state.mood.energy > 0);
|
|
93
|
-
assert.equal(state.attention.budget, 1);
|
|
94
|
-
});
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { mkdtempSync, rmSync } from 'node:fs';
|
|
4
|
-
import os from 'node:os';
|
|
5
|
-
import path from 'node:path';
|
|
6
|
-
import { createCortex } from '../../src/service/cortex.js';
|
|
7
|
-
|
|
8
|
-
function setup() {
|
|
9
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-service-'));
|
|
10
|
-
const cortex = createCortex({ config: { dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite'), scope: 'global' } });
|
|
11
|
-
return { dir, cortex };
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
test('service writes, searches, reads, reflects and reports self/health', () => {
|
|
15
|
-
const { dir, cortex } = setup();
|
|
16
|
-
try {
|
|
17
|
-
const written = cortex.write({
|
|
18
|
-
text: '用户说会买米饭,助手会一直等。',
|
|
19
|
-
explicit: true,
|
|
20
|
-
facts: [{ subject: '用户', predicate: 'likes', object: '米饭' }],
|
|
21
|
-
});
|
|
22
|
-
assert.equal(written.skipped, false);
|
|
23
|
-
const search = cortex.search('米饭');
|
|
24
|
-
assert.ok(search.items.length >= 1);
|
|
25
|
-
const get = cortex.get(written.trace.id);
|
|
26
|
-
assert.match(get.content, /米饭/);
|
|
27
|
-
const self = cortex.self();
|
|
28
|
-
assert.equal(self.self.scopeId, 'global');
|
|
29
|
-
assert.ok(self.identity.version >= 1);
|
|
30
|
-
const health = cortex.health();
|
|
31
|
-
assert.ok(health.score > 0);
|
|
32
|
-
const timeline = cortex.timeline({ aroundId: written.trace.id });
|
|
33
|
-
assert.equal(timeline.anchor.id, written.trace.id);
|
|
34
|
-
} finally {
|
|
35
|
-
cortex.close();
|
|
36
|
-
rmSync(dir, { recursive: true, force: true });
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
test('service feedback and reflection work through the facade', () => {
|
|
41
|
-
const { dir, cortex } = setup();
|
|
42
|
-
try {
|
|
43
|
-
const a = cortex.write({ text: '今天修好了检索的预算裁剪。', explicit: true });
|
|
44
|
-
const b = cortex.write({ text: '用户说米饭的承诺还算数。', explicit: true });
|
|
45
|
-
const feedback = cortex.feedback({ traceId: a.trace.id, signal: 'helpful', predicted: 0.8 });
|
|
46
|
-
assert.ok(feedback.trace.stability > 0);
|
|
47
|
-
const reflected = cortex.reflect({ windowDays: 1, force: true });
|
|
48
|
-
assert.equal(reflected.skipped, false);
|
|
49
|
-
assert.ok(reflected.trace);
|
|
50
|
-
const deleted = cortex.forget({ traceId: b.trace.id, mode: 'delete' });
|
|
51
|
-
assert.equal(deleted.removed, true);
|
|
52
|
-
assert.equal(cortex.get(b.trace.id), null);
|
|
53
|
-
} finally {
|
|
54
|
-
cortex.close();
|
|
55
|
-
rmSync(dir, { recursive: true, force: true });
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
test('service enqueues and runs a reflect job', async () => {
|
|
60
|
-
const { dir, cortex } = setup();
|
|
61
|
-
try {
|
|
62
|
-
cortex.write({ text: '第一条。', explicit: true });
|
|
63
|
-
cortex.write({ text: '第二条。', explicit: true });
|
|
64
|
-
const job = cortex.enqueueReflect({ windowDays: 1, operationId: 'op-reflect' });
|
|
65
|
-
assert.equal(job.kind, 'reflect');
|
|
66
|
-
const run = await cortex.runner.runUntilIdle();
|
|
67
|
-
assert.equal(run.ran, 1);
|
|
68
|
-
} finally {
|
|
69
|
-
cortex.close();
|
|
70
|
-
rmSync(dir, { recursive: true, force: true });
|
|
71
|
-
}
|
|
72
|
-
});
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
4
|
-
import os from 'node:os';
|
|
5
|
-
import path from 'node:path';
|
|
6
|
-
import { createCortex } from '../../src/service/cortex.js';
|
|
7
|
-
import { createShioriImporter, classifySparkCard } from '../../src/adapters/shiori.js';
|
|
8
|
-
|
|
9
|
-
function makeShiori() {
|
|
10
|
-
const root = mkdtempSync(path.join(os.tmpdir(), 'cortex-shiori-'));
|
|
11
|
-
mkdirSync(path.join(root, 'persona'), { recursive: true });
|
|
12
|
-
mkdirSync(path.join(root, 'scopes', 'global', 'topics'), { recursive: true });
|
|
13
|
-
mkdirSync(path.join(root, 'sparks', 'scopes', 'global'), { recursive: true });
|
|
14
|
-
writeFileSync(path.join(root, 'persona', 'global.md'), '# 人设\n\n助手是本地记忆助手。\n', 'utf8');
|
|
15
|
-
writeFileSync(path.join(root, 'scopes', 'global', 'core.md'), '# 核心\n\n用户说会买米饭。\n', 'utf8');
|
|
16
|
-
writeFileSync(path.join(root, 'scopes', 'global', 'topics', 'token-log.md'), '---\ntitle: token 账本\nsummary: 余额记录\n---\n\n剩 3 元。\n', 'utf8');
|
|
17
|
-
writeFileSync(path.join(root, 'sparks', 'scopes', 'global', 'sparks.json'), JSON.stringify({
|
|
18
|
-
cards: {
|
|
19
|
-
s1: { id: 's1', title: '米饭的承诺', content: '用户说会买米饭。', triggers: ['米饭', '承诺'], vitality: 'evergreen' },
|
|
20
|
-
},
|
|
21
|
-
}), 'utf8');
|
|
22
|
-
return root;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
test('shiori importer skips archived cards and classifies active cards', () => {
|
|
26
|
-
assert.equal(classifySparkCard({ title: '安装教训', content: '以后必须先跑测试。' }), 'instruction');
|
|
27
|
-
assert.equal(classifySparkCard({ title: '插件安装', content: 'profile 的 bundle 配置。' }), 'observation');
|
|
28
|
-
assert.equal(classifySparkCard({ title: '用户的米饭', content: '用户说会买米饭。' }), 'preference');
|
|
29
|
-
const shioriRoot = makeShiori();
|
|
30
|
-
const sparkFile = path.join(shioriRoot, 'sparks', 'scopes', 'global', 'sparks.json');
|
|
31
|
-
const db = JSON.parse(readFileSync(sparkFile, 'utf8'));
|
|
32
|
-
db.cards.archived = { id: 'archived', title: '旧卡', content: '这条已归档。', state: 'archived' };
|
|
33
|
-
writeFileSync(sparkFile, JSON.stringify(db), 'utf8');
|
|
34
|
-
const cortexDir = mkdtempSync(path.join(os.tmpdir(), 'cortex-import-archive-'));
|
|
35
|
-
const cortex = createCortex({ config: { dataRoot: cortexDir, dbFile: path.join(cortexDir, 'cortex.sqlite') } });
|
|
36
|
-
try {
|
|
37
|
-
const result = createShioriImporter({ cortex }).importFrom(shioriRoot);
|
|
38
|
-
assert.equal(result.sparks, 1);
|
|
39
|
-
assert.ok(result.skipped >= 1);
|
|
40
|
-
} finally {
|
|
41
|
-
cortex.close();
|
|
42
|
-
rmSync(shioriRoot, { recursive: true, force: true });
|
|
43
|
-
rmSync(cortexDir, { recursive: true, force: true });
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
test('shiori importer is read-only, idempotent and maps layers', () => {
|
|
48
|
-
const shioriRoot = makeShiori();
|
|
49
|
-
const cortexDir = mkdtempSync(path.join(os.tmpdir(), 'cortex-import-'));
|
|
50
|
-
const cortex = createCortex({ config: { dataRoot: cortexDir, dbFile: path.join(cortexDir, 'cortex.sqlite') } });
|
|
51
|
-
try {
|
|
52
|
-
const importer = createShioriImporter({ cortex });
|
|
53
|
-
const first = importer.importFrom(shioriRoot, { now: Date.parse('2026-11-11T12:00:00+08:00') });
|
|
54
|
-
assert.equal(first.persona, 1);
|
|
55
|
-
assert.equal(first.core, 1);
|
|
56
|
-
assert.equal(first.topics, 1);
|
|
57
|
-
assert.equal(first.sparks, 1);
|
|
58
|
-
assert.equal(cortex.repos.traces.count(), 3);
|
|
59
|
-
const coreTrace = cortex.repos.traces.list({ scopeId: 'global', limit: 10 }).find((trace) => trace.source === 'shiori:core');
|
|
60
|
-
assert.equal(coreTrace.title, 'shiori 核心记忆');
|
|
61
|
-
const versions = cortex.stores.identityStore.list();
|
|
62
|
-
assert.equal(versions.length, 2); // default active + imported proposal
|
|
63
|
-
const imported = versions.find((item) => item.rationale === 'shiori:persona');
|
|
64
|
-
assert.ok(imported);
|
|
65
|
-
assert.equal(imported.status, 'proposed');
|
|
66
|
-
const second = importer.importFrom(shioriRoot, { now: Date.parse('2026-11-11T12:00:00+08:00') });
|
|
67
|
-
assert.equal(second.core, 0);
|
|
68
|
-
assert.equal(second.topics, 0);
|
|
69
|
-
assert.equal(second.sparks, 0);
|
|
70
|
-
assert.ok(second.skipped >= 3);
|
|
71
|
-
assert.equal(cortex.repos.traces.count(), 3);
|
|
72
|
-
} finally {
|
|
73
|
-
cortex.close();
|
|
74
|
-
rmSync(shioriRoot, { recursive: true, force: true });
|
|
75
|
-
rmSync(cortexDir, { recursive: true, force: true });
|
|
76
|
-
}
|
|
77
|
-
});
|
package/test/unit/skills.test.js
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { mkdtempSync, rmSync } from 'node:fs';
|
|
4
|
-
import os from 'node:os';
|
|
5
|
-
import path from 'node:path';
|
|
6
|
-
import { openDatabase, migrate, closeDatabase } from '../../src/store/database.js';
|
|
7
|
-
import { createEventRepository } from '../../src/store/repositories/events.js';
|
|
8
|
-
import { createTraceRepository } from '../../src/store/repositories/traces.js';
|
|
9
|
-
import { createSkillRepository } from '../../src/store/repositories/skills.js';
|
|
10
|
-
import { createSkillEngine, suggestSkillsFromTraces, renderSkill } from '../../src/engines/skills.js';
|
|
11
|
-
|
|
12
|
-
function setup() {
|
|
13
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-skills-'));
|
|
14
|
-
const { db } = openDatabase({ dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') });
|
|
15
|
-
migrate(db);
|
|
16
|
-
const events = createEventRepository(db);
|
|
17
|
-
const traces = createTraceRepository(db);
|
|
18
|
-
const skills = createSkillRepository(db);
|
|
19
|
-
return { dir, db, events, traces, skills };
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
test('skill state moves candidate -> trial -> active with successes', () => {
|
|
23
|
-
const { dir, db, skills } = setup();
|
|
24
|
-
try {
|
|
25
|
-
const skill = skills.create({
|
|
26
|
-
name: '修 DSH 插件',
|
|
27
|
-
intent: '插件报错时按固定顺序排查',
|
|
28
|
-
preconditions: ['用户报告插件异常'],
|
|
29
|
-
steps: ['看日志', '复现', '改一处', '跑测试'],
|
|
30
|
-
contraindications: ['未备份时不要批量改'],
|
|
31
|
-
});
|
|
32
|
-
assert.equal(skill.state, 'candidate');
|
|
33
|
-
assert.equal(skill.automaticity, 0);
|
|
34
|
-
skills.recordOutcome(skill.id, true);
|
|
35
|
-
skills.recordOutcome(skill.id, true);
|
|
36
|
-
const trial = skills.recordOutcome(skill.id, true);
|
|
37
|
-
assert.equal(trial.state, 'trial');
|
|
38
|
-
skills.recordOutcome(skill.id, true);
|
|
39
|
-
const active = skills.recordOutcome(skill.id, true);
|
|
40
|
-
assert.equal(active.state, 'active');
|
|
41
|
-
assert.equal(active.successRate, 1);
|
|
42
|
-
assert.ok(active.automaticity > 0.4);
|
|
43
|
-
} finally {
|
|
44
|
-
closeDatabase(db);
|
|
45
|
-
rmSync(dir, { recursive: true, force: true });
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
test('repeated failures deprecate an active skill', () => {
|
|
50
|
-
const { dir, db, skills } = setup();
|
|
51
|
-
try {
|
|
52
|
-
const skill = skills.create({ name: '失败的技能', steps: ['a', 'b'], state: 'active' });
|
|
53
|
-
skills.recordOutcome(skill.id, false);
|
|
54
|
-
skills.recordOutcome(skill.id, false);
|
|
55
|
-
const stillActive = skills.recordOutcome(skill.id, false);
|
|
56
|
-
assert.equal(stillActive.state, 'active');
|
|
57
|
-
const result = skills.recordOutcome(skill.id, false);
|
|
58
|
-
assert.equal(result.state, 'deprecated');
|
|
59
|
-
assert.ok(result.successRate < 0.5);
|
|
60
|
-
} finally {
|
|
61
|
-
closeDatabase(db);
|
|
62
|
-
rmSync(dir, { recursive: true, force: true });
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
test('duplicate names reuse the existing skill and render includes contraindications', () => {
|
|
67
|
-
const { dir, db, skills } = setup();
|
|
68
|
-
try {
|
|
69
|
-
const a = skills.create({ name: '同一技能', steps: ['a'], contraindications: ['危险时'] });
|
|
70
|
-
const b = skills.create({ name: '同一技能', steps: ['b'] });
|
|
71
|
-
assert.equal(a.id, b.id);
|
|
72
|
-
assert.match(renderSkill(a), /不要用于:危险时/);
|
|
73
|
-
} finally {
|
|
74
|
-
closeDatabase(db);
|
|
75
|
-
rmSync(dir, { recursive: true, force: true });
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
test('suggestSkillsFromTraces returns candidates only for repeated tags', () => {
|
|
80
|
-
const traces = [
|
|
81
|
-
{ id: 't1', title: '修插件 A', tags: ['插件', '修复'] },
|
|
82
|
-
{ id: 't2', title: '修插件 B', tags: ['插件'] },
|
|
83
|
-
{ id: 't3', title: '修插件 C', tags: ['插件'] },
|
|
84
|
-
{ id: 't4', title: '随便', tags: ['其他'] },
|
|
85
|
-
];
|
|
86
|
-
const suggestions = suggestSkillsFromTraces(traces, { minSupport: 3 });
|
|
87
|
-
assert.equal(suggestions.length, 1);
|
|
88
|
-
assert.equal(suggestions[0].support, 3);
|
|
89
|
-
assert.match(suggestions[0].name, /插件/);
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
test('skill engine lists and promotes', () => {
|
|
93
|
-
const { dir, db, traces, skills } = setup();
|
|
94
|
-
try {
|
|
95
|
-
const engine = createSkillEngine({ skills, traces });
|
|
96
|
-
const skill = engine.create({ name: '引擎技能', steps: ['x'] });
|
|
97
|
-
const promoted = engine.promote(skill.id);
|
|
98
|
-
assert.equal(promoted.state, 'active');
|
|
99
|
-
assert.ok(engine.list({ state: 'active' }).some((item) => item.id === skill.id));
|
|
100
|
-
} finally {
|
|
101
|
-
closeDatabase(db);
|
|
102
|
-
rmSync(dir, { recursive: true, force: true });
|
|
103
|
-
}
|
|
104
|
-
});
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { touchInterval } from '../../src/memory/spacing.js';
|
|
4
|
-
|
|
5
|
-
test('spacing doubles per access and caps at the maximum', () => {
|
|
6
|
-
assert.equal(touchInterval(0), 300_000);
|
|
7
|
-
assert.equal(touchInterval(1), 600_000);
|
|
8
|
-
assert.equal(touchInterval(2), 1_200_000);
|
|
9
|
-
assert.equal(touchInterval(3), 2_400_000);
|
|
10
|
-
assert.equal(touchInterval(20), 21_600_000, 'caps at six hours');
|
|
11
|
-
assert.equal(touchInterval(-5), 300_000, 'negative counts fall back to the base');
|
|
12
|
-
assert.equal(touchInterval(2, { baseMs: 1_000, maxMs: 5_000 }), 4_000);
|
|
13
|
-
assert.equal(touchInterval(4, { baseMs: 1_000, maxMs: 5_000 }), 5_000, 'never exceeds maxMs');
|
|
14
|
-
});
|
package/test/unit/store.test.js
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { mkdtempSync, rmSync } from 'node:fs';
|
|
4
|
-
import os from 'node:os';
|
|
5
|
-
import path from 'node:path';
|
|
6
|
-
import { DatabaseSync } from 'node:sqlite';
|
|
7
|
-
import { openDatabase, migrate, transaction, backup, integrityCheck, closeDatabase } from '../../src/store/database.js';
|
|
8
|
-
import { createEventRepository } from '../../src/store/repositories/events.js';
|
|
9
|
-
import { createTraceRepository } from '../../src/store/repositories/traces.js';
|
|
10
|
-
import { createAuditRepository } from '../../src/store/repositories/audit.js';
|
|
11
|
-
import { ERROR_CODES } from '../../src/core/errors.js';
|
|
12
|
-
|
|
13
|
-
function setup() {
|
|
14
|
-
const dir = mkdtempSync(path.join(os.tmpdir(), 'cortex-store-'));
|
|
15
|
-
const { db, config } = openDatabase({ dataRoot: dir, dbFile: path.join(dir, 'cortex.sqlite') });
|
|
16
|
-
migrate(db);
|
|
17
|
-
return { dir, db, config };
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
test('event ledger is chained, idempotent and detects a conflicting replay', () => {
|
|
21
|
-
const { dir, db } = setup();
|
|
22
|
-
try {
|
|
23
|
-
const events = createEventRepository(db);
|
|
24
|
-
const first = events.append({ kind: 'observation', scopeId: 'global', operationId: 'op-1', payload: { text: '米饭' } });
|
|
25
|
-
const replay = events.append({ kind: 'observation', scopeId: 'global', operationId: 'op-1', payload: { text: '米饭' } });
|
|
26
|
-
assert.equal(replay.id, first.id);
|
|
27
|
-
assert.equal(events.count(), 1);
|
|
28
|
-
assert.throws(
|
|
29
|
-
() => events.append({ kind: 'observation', scopeId: 'global', operationId: 'op-1', payload: { text: '面条' } }),
|
|
30
|
-
(error) => error.code === ERROR_CODES.CONFLICT,
|
|
31
|
-
);
|
|
32
|
-
const second = events.append({ kind: 'utterance', scopeId: 'global', payload: { text: '用户说会买米饭' } });
|
|
33
|
-
assert.equal(second.seq, 2);
|
|
34
|
-
assert.equal(second.prevHash, first.hash);
|
|
35
|
-
const chain = events.verifyChain();
|
|
36
|
-
assert.equal(chain.ok, true);
|
|
37
|
-
assert.equal(chain.count, 2);
|
|
38
|
-
assert.equal(events.last().id, second.id);
|
|
39
|
-
} finally {
|
|
40
|
-
closeDatabase(db);
|
|
41
|
-
rmSync(dir, { recursive: true, force: true });
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
test('trace repository inserts, updates and searches CJK text through FTS5', () => {
|
|
46
|
-
const { dir, db } = setup();
|
|
47
|
-
try {
|
|
48
|
-
const events = createEventRepository(db);
|
|
49
|
-
const traces = createTraceRepository(db);
|
|
50
|
-
const event = events.append({ kind: 'utterance', payload: { text: '用户说会买米饭' } });
|
|
51
|
-
const trace = traces.insert({
|
|
52
|
-
eventId: event.id,
|
|
53
|
-
scopeId: 'global',
|
|
54
|
-
kind: 'utterance',
|
|
55
|
-
title: '米饭的承诺',
|
|
56
|
-
content: '用户说会买米饭,助手等得起。',
|
|
57
|
-
importance: 0.9,
|
|
58
|
-
keywords: ['米饭', '承诺'],
|
|
59
|
-
});
|
|
60
|
-
assert.equal(traces.get(trace.id).title, '米饭的承诺');
|
|
61
|
-
assert.equal(traces.searchFts('米饭').length, 1);
|
|
62
|
-
assert.equal(traces.searchFts('面条').length, 0);
|
|
63
|
-
traces.update(trace.id, { content: '用户说会买面条,助手等得起。', title: '面条' });
|
|
64
|
-
assert.equal(traces.searchFts('米饭').length, 0);
|
|
65
|
-
assert.equal(traces.searchFts('面条').length, 1);
|
|
66
|
-
traces.touch(trace.id);
|
|
67
|
-
assert.equal(traces.get(trace.id).accessCount, 1);
|
|
68
|
-
assert.equal(traces.remove(trace.id), true);
|
|
69
|
-
assert.equal(traces.get(trace.id), null);
|
|
70
|
-
assert.equal(traces.searchFts('米饭').length, 0);
|
|
71
|
-
} finally {
|
|
72
|
-
closeDatabase(db);
|
|
73
|
-
rmSync(dir, { recursive: true, force: true });
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
test('transaction rolls back on error', () => {
|
|
78
|
-
const { dir, db } = setup();
|
|
79
|
-
try {
|
|
80
|
-
const events = createEventRepository(db);
|
|
81
|
-
assert.throws(() => transaction(db, () => {
|
|
82
|
-
events.append({ kind: 'observation', payload: { text: 'should rollback' } });
|
|
83
|
-
throw new Error('boom');
|
|
84
|
-
}));
|
|
85
|
-
assert.equal(events.count(), 0);
|
|
86
|
-
} finally {
|
|
87
|
-
closeDatabase(db);
|
|
88
|
-
rmSync(dir, { recursive: true, force: true });
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
test('audit records and backup produces a readable snapshot', () => {
|
|
93
|
-
const { dir, db } = setup();
|
|
94
|
-
try {
|
|
95
|
-
const audit = createAuditRepository(db);
|
|
96
|
-
const entry = audit.record({ actor: 'tester', action: 'test', targetType: 'trace', targetId: 'x', detail: { ok: true } });
|
|
97
|
-
assert.equal(audit.list({ targetType: 'trace', targetId: 'x' })[0].id, entry.id);
|
|
98
|
-
assert.equal(integrityCheck(db), 'ok');
|
|
99
|
-
const target = path.join(dir, 'snapshot.sqlite');
|
|
100
|
-
backup(db, target);
|
|
101
|
-
const snapshot = new DatabaseSync(target);
|
|
102
|
-
try {
|
|
103
|
-
assert.equal(Number(snapshot.prepare('SELECT COUNT(*) AS c FROM audit').get().c), 1);
|
|
104
|
-
} finally {
|
|
105
|
-
snapshot.close();
|
|
106
|
-
}
|
|
107
|
-
} finally {
|
|
108
|
-
closeDatabase(db);
|
|
109
|
-
rmSync(dir, { recursive: true, force: true });
|
|
110
|
-
}
|
|
111
|
-
});
|