Files
msd-core/tests/learnings.test.cjs
Tom Boucher 463cffd894 chore(#604): rename get-shit-done/ runtime directory to gsd-core/ (#615)
* chore(#604): rename get-shit-done/ runtime directory to gsd-core/

Renames the installed runtime directory `get-shit-done/` to `gsd-core/` so the
on-disk name matches the package (`@opengsd/gsd-core`), repo, and binary
(`gsd-tools`). The npm package name and binary are unchanged; npx/npm consumers
are unaffected.

Mechanical (bulk, ~90% of the diff):
- `git mv get-shit-done gsd-core`
- Swept path/identifier references across the repo via
  `perl -pe 's/get-shit-done(?!-\w)/gsd-core/g'`. The negative lookahead
  preserves the five legitimate slug variants that are NOT the directory:
  get-shit-done-{OLD,cc,classic,cli,redux} (old package/repo names).
- Build/manifest wiring: package.json (bin, files, coverage globs),
  tsconfig.build.json (outDir), ~86 .gitignore build-output entries,
  stryker.config.mjs, scan-ignore files, install.js path strings.
- Frozen (not rewritten): CHANGELOG.md history; translated docs
  (README.<locale>.md and docs/{ja-JP,ko-KR,pt-BR,zh-CN}/).

New logic (review here):
- src/installer-migrations/003-rename-get-shit-done-to-gsd-core.cts: a proper
  ADR-0008 installer migration. On upgrade it walks the legacy
  `~/.claude/get-shit-done/` tree, classifies each file via the prior install
  manifest, and emits remove-managed / backup-and-remove for managed files
  while PRESERVING unknown user-added files. Symlink-safe (skips a symlinked
  root and symlinked entries; bounds-checks every path under configDir). The
  framework rolls back on install failure. Emptied dirs may remain (framework
  has no recursive dir-removal primitive) — documented.
- scripts/lint-legacy-dir-name.cjs: CI regression guard forbidding the bare
  `get-shit-done` directory token (split token to avoid self-match; case-
  insensitive; `(?!-\w)` lookahead allows the slug variants; allowlists
  CHANGELOG, translated docs, and `gsd-allow-legacy-name` marker lines).
  Wired into the lint-tests CI job.
- Restored scripts/lint-package-identity-drift.cjs detection regexes (the
  mechanical sweep had wrongly rewritten the old-name patterns it exists to
  detect) and marked them as intentional legacy references.
- TDD tests for the migration and the guard; do.md slash-command guard regex
  tightened so a `/gsd-core/bin` path segment is not mistaken for a command;
  changeset + docs/installer-migrations.md row added.

Breaking: the installed runtime path moves `~/.claude/get-shit-done/` ->
`~/.claude/gsd-core/`. Migration 003 removes the stale legacy dir's managed
files (preserving user files) on upgrade. Users with custom hooks/configs
hardcoding the old path must update them.

Closes #604

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(#604): unsweep pending changesets + allowlist injection-example docs

CI fixes for the rename PR:
- Do not sweep pending .changeset/*.md (ephemeral release-note fragments,
  like CHANGELOG); reverted those body edits so 5 pre-existing malformed
  fragments (missing type/pr) no longer enter the PR diff and trip docs-lint.
  Allowlisted .changeset/ in the legacy-name guard accordingly.
- Allowlisted TEST-EXAMPLES.md and docs/explanation/security-model.md in
  prompt-injection-scan.sh: they contain intentional injection examples /
  security-model prose; the path-reference rewrites are kept.

CodeQL alerts on this PR are pre-existing (alert lines unchanged by this PR;
none in the new migration/guard) and are out of scope for the rename.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(#604): resolve CodeQL alerts surfaced on this PR

The rename diff touched files carrying pre-existing CodeQL findings; per the
no-pre-existing-dismissal rule, fixing every surfaced alert rather than waving
them off. All behavior-preserving:

- scripts/ci-test-scope.cjs: build the config-path match from string
  .includes() instead of a RegExp over an arg-derived value (js/regex-injection).
- src/profile-output.cts: escape backslashes before pipe-escaping desc/safeName
  so the table-cell escape is complete (js/incomplete-sanitization).
- tests/{bug-2643,bug-2808,docs-parity-live-registry}: two-pass HTML-comment
  strip so a bare/unclosed `<!--` cannot survive (js/incomplete-multi-character-sanitization).
- tests/inline-plan-threshold: drop the no-op `\s`->`\s` identity replace,
  keep the meaningful POSIX-class conversion (js/identity-replacement).

Verified: build:lib green; the touched test files + ci-test-scope + profile-output
suites pass; lint:legacy-name clean.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(#604): correctly resolve remaining CodeQL alerts (regex-injection + sanitization)

The prior commit's fixes for two alerts were ineffective:
- ci-test-scope.cjs js/regex-injection: the alert is the CLI-arg-derived `file`
  reaching static regex `.test(file)` calls (not the config rule). Removed ALL
  regex over file/t — startsWith/includes/=== string checks + an isWindowsHint
  helper — so there is no regex sink for the tainted value.
- js/incomplete-multi-character-sanitization (3 test files): a single
  `.replace(/<!--...-->/g,'')` can let `<!--` re-form. Replaced with a fixpoint
  loop (replace until stable) plus a final bare-opener strip.

Verified: no regex over file/t remains; ci-test-scope + the 3 test suites pass;
lint:legacy-name clean.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(#604): make ci-test-scope + comment-strippers regex-free to clear CodeQL

CodeQL flags the regex PATTERNS syntactically (regex-injection on the
--files arg split; incomplete-multi-character-sanitization on the <!--...-->
replace), so loop fixes do not satisfy it. Made these paths regex-free:
- ci-test-scope.cjs splitFiles: char-by-char separator tokenizer (no /[,\\s]+/).
- 3 test files: indexOf/slice HTML-comment stripper (no .replace(/<!--/)).
Behavior preserved; ci-test-scope + the 3 suites pass; guard clean.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(#604): unblock security base64 scan on the large rename diff

The security job hit its 10m timeout: base64-scan.sh choked on the binary
test fixture tests/feat-3594-parser-property-style.test.cjs (embedded NUL/
non-UTF8 bytes -> thousands of bogus blobs + "ignored null byte" warnings),
and the ~800-file rename diff is slow to scan regardless.

- scripts/base64-scan.sh: skip binary-by-content files (grep -Iq .) — they
  can't carry base64-obfuscated *text* and feeding NUL bytes through the
  per-line scanner is pathologically slow. collect_files already filtered
  binary *extensions*; this catches binary *content* in text extensions.
- .github/workflows/security-scan.yml: raise the security job timeout 10m->30m
  to accommodate very large diffs (the scan itself is unchanged).

Verified locally: scan skips the fixture, 0 "ignored null byte" warnings,
0 findings, exit 0.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(#604): sweep get-shit-done refs introduced by merging next

The branch was updated with next (#614/#384/#618 etc.), which reference the
get-shit-done/ dir (still named that on next). Swept the stale references in
the merged files to gsd-core so the rename stays consistent and lint:legacy-name
passes:
- commands/gsd/discuss-phase.md (runtime-launcher shim paths)
- src/core.cts (getAgentsDir layout comments)
- tests/bug-384-agents-runtime-aware.test.cjs (require path to runtime lib)

Verified: guard 0 violations; build green.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(#604): exclude gsd-core/ path segments from bug-3683 command cross-ref invariant

The #614 runtime-launcher shim added to discuss-phase.md references
`${_GSD_RUNTIME_ROOT}/gsd-core/bin/...`. bug-3683's REF_PATTERN excluded path-y
refs only via lookbehind, but `}` precedes `/gsd-core/` in the shim, so it
mis-read the directory path as a dangling `/gsd-core` command ref (same class as
the #604 bug-2954 fix). Added a trailing `(?![\w-]*\/)` so `/gsd-<x>/...` path
segments are not treated as slash-command references.

Verified locally on BOTH platforms before pushing:
- mac (node 26) full suite: 0 failures
- gsd-test-runner (linux, node22 image) full suite: 0 failures
- bug-3683 + bug-2954 pass.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(#604): lazily resolve findProjectRoot in gsd-tools (harden flaky CI)

CI intermittently failed state.test's gsd-tools subprocess with
"findProjectRoot is not a function" (flip-flopping across legs; not reproducible
on mac full suite, gsd-test linux full suite, test:unit, or state.test x8).
findProjectRoot is a re-export from core.cjs (sourced from project-root.cjs);
binding it via destructure at module-load can be undefined under a load-ordering
edge. Resolve it lazily at call time via a small wrapper so the lookup happens
after core.cjs is fully initialized.

Verified green on BOTH platforms before pushing:
- mac (node 26) full suite: 0 failures
- gsd-test-runner (linux, node22) full suite: 0 failures
- state.test.cjs: 106/106; gsd-tools loads cleanly.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(#604): allowlist verification-patterns.md placeholder examples in secret scan

The rename git-mv'd references/verification-patterns.md into gsd-core/, pulling
it into the secret-scan diff. It documents stub/placeholder RED-FLAG env-var
examples (illustrative Stripe test-key / database-URL / API-key placeholders) —
not real credentials. Added it to .secretscanignore with the strict annotation,
mirroring the existing gsd-core/workflows/plan-phase.md exception.

Verified locally: secret-scan-lint --strict OK; secret-scan --diff origin/next
exits 0 with 0 findings.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-02 18:35:29 -04:00

630 lines
22 KiB
JavaScript

/**
* Learnings Store Tests
*
* Tests for the global learnings CRUD library: write, read, list, query,
* delete, dedup, empty store, malformed file handling, copyFromProject, prune.
*/
const { test, describe, beforeEach, afterEach } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('fs');
const path = require('path');
const os = require('os');
const {
learningsWrite,
learningsRead,
learningsList,
learningsQuery,
learningsDelete,
learningsCopyFromProject,
learningsPrune,
} = require('../gsd-core/bin/lib/learnings.cjs');
const { runGsdTools, createTempProject, cleanup } = require('./helpers.cjs');
// ─── Test Helpers ────────────────────────────────────────────────────────────
/**
* Create a unique temp directory for each test.
* @returns {string}
*/
function makeTempDir() {
return fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-learnings-test-'));
}
/**
* Remove a directory recursively.
* @param {string} dir
*/
function cleanupDir(dir) {
cleanup(dir);
}
// ─── Write ───────────────────────────────────────────────────────────────────
describe('learningsWrite', () => {
let storeDir;
beforeEach(() => { storeDir = makeTempDir(); });
afterEach(() => { cleanupDir(storeDir); });
test('creates a learning file with all required fields', () => {
const result = learningsWrite({
source_project: 'test-project',
learning: 'Always validate inputs before processing',
context: 'security review',
tags: ['security', 'validation'],
}, { storeDir });
assert.ok(result.id, 'should return an id');
assert.strictEqual(result.created, true);
assert.ok(result.content_hash, 'should return a content_hash');
// Verify file exists and has correct structure
const filePath = path.join(storeDir, `${result.id}.json`);
assert.ok(fs.existsSync(filePath), 'file should exist on disk');
const record = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
assert.strictEqual(record.id, result.id);
assert.strictEqual(record.source_project, 'test-project');
assert.strictEqual(record.learning, 'Always validate inputs before processing');
assert.strictEqual(record.context, 'security review');
assert.deepStrictEqual(record.tags, ['security', 'validation']);
assert.strictEqual(record.content_hash, result.content_hash);
assert.ok(record.date, 'should have a date');
});
test('creates store directory on first write', () => {
const nestedDir = path.join(storeDir, 'nested', 'store');
assert.ok(!fs.existsSync(nestedDir), 'dir should not exist yet');
learningsWrite({
source_project: 'test',
learning: 'test learning',
}, { storeDir: nestedDir });
assert.ok(fs.existsSync(nestedDir), 'dir should be created on write');
});
test('defaults context to empty string and tags to empty array', () => {
const result = learningsWrite({
source_project: 'test',
learning: 'minimal entry',
}, { storeDir });
const record = learningsRead(result.id, { storeDir });
assert.strictEqual(record.context, '');
assert.deepStrictEqual(record.tags, []);
});
});
// ─── Deduplication ───────────────────────────────────────────────────────────
describe('deduplication', () => {
let storeDir;
beforeEach(() => { storeDir = makeTempDir(); });
afterEach(() => { cleanupDir(storeDir); });
test('same content from same project is not stored twice', () => {
const entry = {
source_project: 'my-project',
learning: 'Use content hashing for dedup',
tags: ['dedup'],
};
const first = learningsWrite(entry, { storeDir });
const second = learningsWrite(entry, { storeDir });
assert.strictEqual(first.created, true);
assert.strictEqual(second.created, false);
assert.strictEqual(first.content_hash, second.content_hash);
assert.strictEqual(first.id, second.id);
// Only one file on disk
const files = fs.readdirSync(storeDir).filter(f => f.endsWith('.json'));
assert.strictEqual(files.length, 1);
});
test('same learning from different projects creates separate entries', () => {
const learning = 'Same learning text';
const first = learningsWrite({
source_project: 'project-a',
learning,
}, { storeDir });
const second = learningsWrite({
source_project: 'project-b',
learning,
}, { storeDir });
assert.strictEqual(first.created, true);
assert.strictEqual(second.created, true);
assert.notStrictEqual(first.content_hash, second.content_hash);
const files = fs.readdirSync(storeDir).filter(f => f.endsWith('.json'));
assert.strictEqual(files.length, 2);
});
});
// ─── Read ────────────────────────────────────────────────────────────────────
describe('learningsRead', () => {
let storeDir;
beforeEach(() => { storeDir = makeTempDir(); });
afterEach(() => { cleanupDir(storeDir); });
test('returns a learning by ID', () => {
const { id } = learningsWrite({
source_project: 'test',
learning: 'readable entry',
tags: ['read'],
}, { storeDir });
const record = learningsRead(id, { storeDir });
assert.ok(record);
assert.strictEqual(record.id, id);
assert.strictEqual(record.learning, 'readable entry');
});
test('returns null for non-existent ID', () => {
const record = learningsRead('does-not-exist', { storeDir });
assert.strictEqual(record, null);
});
});
// ─── List ────────────────────────────────────────────────────────────────────
describe('learningsList', () => {
let storeDir;
beforeEach(() => { storeDir = makeTempDir(); });
afterEach(() => { cleanupDir(storeDir); });
test('returns empty array for empty store', () => {
const results = learningsList({ storeDir });
assert.deepStrictEqual(results, []);
});
test('returns empty array when store dir does not exist', () => {
const results = learningsList({ storeDir: path.join(storeDir, 'nonexistent') });
assert.deepStrictEqual(results, []);
});
test('returns all learnings sorted by date (newest first)', () => {
// Write three entries with controlled dates
const id1 = learningsWrite({
source_project: 'p1',
learning: 'first',
}, { storeDir }).id;
// Manually adjust dates to control sort order
const file1 = path.join(storeDir, `${id1}.json`);
const rec1 = JSON.parse(fs.readFileSync(file1, 'utf-8'));
rec1.date = '2025-01-01T00:00:00.000Z';
fs.writeFileSync(file1, JSON.stringify(rec1));
const id2 = learningsWrite({
source_project: 'p2',
learning: 'second',
}, { storeDir }).id;
const file2 = path.join(storeDir, `${id2}.json`);
const rec2 = JSON.parse(fs.readFileSync(file2, 'utf-8'));
rec2.date = '2025-06-15T00:00:00.000Z';
fs.writeFileSync(file2, JSON.stringify(rec2));
const id3 = learningsWrite({
source_project: 'p3',
learning: 'third',
}, { storeDir }).id;
const file3 = path.join(storeDir, `${id3}.json`);
const rec3 = JSON.parse(fs.readFileSync(file3, 'utf-8'));
rec3.date = '2025-03-10T00:00:00.000Z';
fs.writeFileSync(file3, JSON.stringify(rec3));
const results = learningsList({ storeDir });
assert.strictEqual(results.length, 3);
assert.strictEqual(results[0].learning, 'second'); // newest
assert.strictEqual(results[1].learning, 'third'); // middle
assert.strictEqual(results[2].learning, 'first'); // oldest
});
});
// ─── Query ───────────────────────────────────────────────────────────────────
describe('learningsQuery', () => {
let storeDir;
beforeEach(() => { storeDir = makeTempDir(); });
afterEach(() => { cleanupDir(storeDir); });
test('filters by tag', () => {
learningsWrite({
source_project: 'p1',
learning: 'auth lesson',
tags: ['auth', 'security'],
}, { storeDir });
learningsWrite({
source_project: 'p2',
learning: 'ui lesson',
tags: ['ui', 'css'],
}, { storeDir });
learningsWrite({
source_project: 'p3',
learning: 'auth pattern',
tags: ['auth', 'patterns'],
}, { storeDir });
const results = learningsQuery({ tag: 'auth' }, { storeDir });
assert.strictEqual(results.length, 2);
assert.ok(results.every(r => r.tags.includes('auth')));
});
test('returns all when no tag filter', () => {
learningsWrite({ source_project: 'p1', learning: 'a' }, { storeDir });
learningsWrite({ source_project: 'p2', learning: 'b' }, { storeDir });
const results = learningsQuery({}, { storeDir });
assert.strictEqual(results.length, 2);
});
test('returns empty array when tag not found', () => {
learningsWrite({
source_project: 'p1',
learning: 'something',
tags: ['other'],
}, { storeDir });
const results = learningsQuery({ tag: 'nonexistent' }, { storeDir });
assert.strictEqual(results.length, 0);
});
});
// ─── Delete ──────────────────────────────────────────────────────────────────
describe('learningsDelete', () => {
let storeDir;
beforeEach(() => { storeDir = makeTempDir(); });
afterEach(() => { cleanupDir(storeDir); });
test('removes a learning by ID', () => {
const { id } = learningsWrite({
source_project: 'test',
learning: 'to be deleted',
}, { storeDir });
assert.strictEqual(learningsDelete(id, { storeDir }), true);
assert.strictEqual(learningsRead(id, { storeDir }), null);
const files = fs.readdirSync(storeDir).filter(f => f.endsWith('.json'));
assert.strictEqual(files.length, 0);
});
test('returns false for non-existent ID', () => {
assert.strictEqual(learningsDelete('nonexistent', { storeDir }), false);
});
});
// ─── Malformed File Handling ─────────────────────────────────────────────────
describe('malformed file handling', () => {
let storeDir;
beforeEach(() => { storeDir = makeTempDir(); });
afterEach(() => { cleanupDir(storeDir); });
test('list skips malformed JSON files with warning', () => {
// Write a valid entry
learningsWrite({
source_project: 'test',
learning: 'valid entry',
}, { storeDir });
// Write a malformed JSON file
fs.writeFileSync(path.join(storeDir, 'bad-entry.json'), '{not valid json!!!', 'utf-8');
const results = learningsList({ storeDir });
assert.strictEqual(results.length, 1);
assert.strictEqual(results[0].learning, 'valid entry');
});
test('write dedup check skips malformed files without crashing', () => {
// Write a malformed JSON file first
fs.writeFileSync(path.join(storeDir, 'corrupt.json'), 'corrupted!', 'utf-8');
// Writing should still succeed
const result = learningsWrite({
source_project: 'test',
learning: 'new entry after corrupt',
}, { storeDir });
assert.strictEqual(result.created, true);
});
});
// ─── Copy From Project ───────────────────────────────────────────────────────
describe('learningsCopyFromProject', () => {
let storeDir;
let projectDir;
beforeEach(() => {
storeDir = makeTempDir();
projectDir = makeTempDir();
});
afterEach(() => {
cleanupDir(storeDir);
cleanupDir(projectDir);
});
test('copies learnings from LEARNINGS.md into global store', () => {
const learningsMd = `# Project Learnings
## Authentication Patterns
Always use OAuth2 for third-party auth.
Never store tokens in localStorage.
## Database Design
Normalize to 3NF unless read performance demands denormalization.
## Error Handling
Use custom error classes that extend Error.
`;
fs.writeFileSync(path.join(projectDir, 'LEARNINGS.md'), learningsMd, 'utf-8');
const result = learningsCopyFromProject(projectDir, {
storeDir,
sourceProject: 'my-app',
});
assert.strictEqual(result.created, 3);
assert.strictEqual(result.skipped, 0);
const all = learningsList({ storeDir });
assert.strictEqual(all.length, 3);
// Verify content was captured
const learningTexts = all.map(r => r.learning);
assert.ok(learningTexts.some(t => t.includes('OAuth2')));
assert.ok(learningTexts.some(t => t.includes('Normalize to 3NF')));
assert.ok(learningTexts.some(t => t.includes('custom error classes')));
});
test('deduplicates on second copy', () => {
const learningsMd = `# Learnings
## Testing
Always write tests first.
`;
fs.writeFileSync(path.join(projectDir, 'LEARNINGS.md'), learningsMd, 'utf-8');
learningsCopyFromProject(projectDir, { storeDir, sourceProject: 'app' });
const second = learningsCopyFromProject(projectDir, { storeDir, sourceProject: 'app' });
assert.strictEqual(second.created, 0);
assert.strictEqual(second.skipped, 1);
const all = learningsList({ storeDir });
assert.strictEqual(all.length, 1);
});
test('returns zero counts when LEARNINGS.md does not exist', () => {
const result = learningsCopyFromProject(projectDir, { storeDir });
assert.deepStrictEqual(result, { total: 0, created: 0, skipped: 0 });
});
test('skips sections with empty body', () => {
const learningsMd = `# Learnings
## Empty Section
## Has Content
Real content here.
`;
fs.writeFileSync(path.join(projectDir, 'LEARNINGS.md'), learningsMd, 'utf-8');
const result = learningsCopyFromProject(projectDir, { storeDir, sourceProject: 'app' });
assert.strictEqual(result.created, 1);
const all = learningsList({ storeDir });
assert.strictEqual(all.length, 1);
assert.ok(all[0].learning.includes('Real content'));
});
});
// ─── Prune ───────────────────────────────────────────────────────────────────
describe('learningsPrune', () => {
let storeDir;
beforeEach(() => { storeDir = makeTempDir(); });
afterEach(() => { cleanupDir(storeDir); });
test('removes entries older than threshold', () => {
// Create an old entry
const oldId = learningsWrite({
source_project: 'old-project',
learning: 'ancient wisdom',
}, { storeDir }).id;
// Backdate it to 100 days ago
const oldFile = path.join(storeDir, `${oldId}.json`);
const oldRec = JSON.parse(fs.readFileSync(oldFile, 'utf-8'));
oldRec.date = new Date(Date.now() - 100 * 24 * 60 * 60 * 1000).toISOString();
fs.writeFileSync(oldFile, JSON.stringify(oldRec));
// Create a recent entry
learningsWrite({
source_project: 'new-project',
learning: 'fresh knowledge',
}, { storeDir });
const result = learningsPrune('90d', { storeDir });
assert.strictEqual(result.removed, 1);
assert.strictEqual(result.kept, 1);
const remaining = learningsList({ storeDir });
assert.strictEqual(remaining.length, 1);
assert.strictEqual(remaining[0].learning, 'fresh knowledge');
});
test('keeps all entries when none are old enough', () => {
learningsWrite({ source_project: 'p', learning: 'recent' }, { storeDir });
const result = learningsPrune('30d', { storeDir });
assert.strictEqual(result.removed, 0);
assert.strictEqual(result.kept, 1);
});
test('returns zeros when store does not exist', () => {
const result = learningsPrune('90d', { storeDir: path.join(storeDir, 'nope') });
assert.deepStrictEqual(result, { removed: 0, kept: 0 });
});
test('throws on invalid duration format', () => {
assert.throws(
() => learningsPrune('invalid', { storeDir }),
/Invalid duration format/
);
});
});
// ─── CLI Integration ────────────────────────────────────────────────────────
describe('CLI integration', () => {
let tmpDir;
beforeEach(() => { tmpDir = createTempProject(); });
afterEach(() => { cleanup(tmpDir); });
test('learnings list returns valid JSON', () => {
const res = runGsdTools(['learnings', 'list'], tmpDir, { HOME: tmpDir });
assert.strictEqual(res.success, true);
const parsed = JSON.parse(res.output);
assert.ok(Array.isArray(parsed.learnings));
assert.strictEqual(typeof parsed.count, 'number');
});
test('learnings query --tag succeeds', () => {
const res = runGsdTools(['learnings', 'query', '--tag', 'auth'], tmpDir, { HOME: tmpDir });
assert.strictEqual(res.success, true);
const parsed = JSON.parse(res.output);
assert.ok(Array.isArray(parsed.learnings));
});
test('learnings prune with bad format exits non-zero', () => {
const res = runGsdTools(['learnings', 'prune', '--older-than', 'badformat'], tmpDir, { HOME: tmpDir });
assert.strictEqual(res.success, false);
assert.ok(res.error.includes('Invalid duration format'));
});
test('learnings unknown subcommand exits non-zero', () => {
const res = runGsdTools(['learnings', 'unknown'], tmpDir, { HOME: tmpDir });
assert.strictEqual(res.success, false);
assert.ok(res.error.includes('Unknown learnings subcommand'));
});
});
// ─── Dedupe Scaling (#306) ───────────────────────────────────────────────────
/**
* Build a LEARNINGS.md string with k unique ## sections.
* Mirrors the ## heading format that learningsCopyFromProject parses.
*/
function makeLearningsMd(k) {
const sections = [];
for (let i = 1; i <= k; i++) {
sections.push(`## Title ${i}\n\nBody content for item ${i}, unique text.`);
}
return `# Project Learnings\n\n${sections.join('\n\n')}\n`;
}
describe('learnings dedupe scaling (#306)', () => {
test('store directory scan count is independent of imported item count', (t) => {
// Test A: assert readdirSync call count does NOT scale with K (regression guard for #306)
// BEFORE the fix: count scales 1:1 with K (one scan per learningsWrite call).
// AFTER the fix: count is constant (one index-build scan per learningsCopyFromProject call).
const storeDir2 = makeTempDir();
const projectDir2 = makeTempDir();
t.after(() => {
cleanupDir(storeDir2);
cleanupDir(projectDir2);
});
const storeDir6 = makeTempDir();
const projectDir6 = makeTempDir();
t.after(() => {
cleanupDir(storeDir6);
cleanupDir(projectDir6);
});
// K=2
fs.writeFileSync(path.join(projectDir2, 'LEARNINGS.md'), makeLearningsMd(2), 'utf-8');
const spy2 = t.mock.method(fs, 'readdirSync');
const before2 = spy2.mock.calls.length;
learningsCopyFromProject(projectDir2, { storeDir: storeDir2, sourceProject: 'proj-a' });
const c1 = spy2.mock.calls.length - before2;
spy2.mock.restore();
// K=6
fs.writeFileSync(path.join(projectDir6, 'LEARNINGS.md'), makeLearningsMd(6), 'utf-8');
const spy6 = t.mock.method(fs, 'readdirSync');
const before6 = spy6.mock.calls.length;
learningsCopyFromProject(projectDir6, { storeDir: storeDir6, sourceProject: 'proj-b' });
const c6 = spy6.mock.calls.length - before6;
spy6.mock.restore();
assert.strictEqual(c1, c6,
`store directory scan count must be independent of imported item count (#306) — got ${c1} for K=2 vs ${c6} for K=6`);
});
test('dedupe semantics preserved: duplicate entry in existing store is skipped', (t) => {
// Test B part 1: importing a section that duplicates an already-stored entry → skipped
const storeDir = makeTempDir();
const projectDir = makeTempDir();
t.after(() => {
cleanupDir(storeDir);
cleanupDir(projectDir);
});
// Pre-seed one entry that matches the first section of our LEARNINGS.md
learningsWrite({
source_project: 'my-proj',
learning: 'Body content for item 1, unique text.',
context: 'Title 1',
tags: ['title'],
}, { storeDir });
// LEARNINGS.md has 3 sections; section 1 duplicates the pre-seeded entry
fs.writeFileSync(path.join(projectDir, 'LEARNINGS.md'), makeLearningsMd(3), 'utf-8');
const result = learningsCopyFromProject(projectDir, { storeDir, sourceProject: 'my-proj' });
assert.strictEqual(result.created, 2, 'two new sections should be created');
assert.strictEqual(result.skipped, 1, 'one duplicate section should be skipped');
const all = learningsList({ storeDir });
assert.strictEqual(all.length, 3);
});
test('dedupe semantics preserved: two identical sections in same file → one created, one skipped', (t) => {
// Test B part 2: exercises the index.set during-loop dedup path
const storeDir = makeTempDir();
const projectDir = makeTempDir();
t.after(() => {
cleanupDir(storeDir);
cleanupDir(projectDir);
});
// Two identical ## sections in the same LEARNINGS.md
const md = `# Learnings\n\n## Duplicate Section\n\nExact same body text.\n\n## Duplicate Section\n\nExact same body text.\n`;
fs.writeFileSync(path.join(projectDir, 'LEARNINGS.md'), md, 'utf-8');
const result = learningsCopyFromProject(projectDir, { storeDir, sourceProject: 'my-proj' });
assert.strictEqual(result.created, 1, 'exactly one entry should be created');
assert.strictEqual(result.skipped, 1, 'the duplicate should be skipped');
const all = learningsList({ storeDir });
assert.strictEqual(all.length, 1);
});
});