basalt-ui 1.0.0 → 1.0.2
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/AGENTS.md +12 -12
- package/CHANGELOG.md +19 -0
- package/README.md +31 -44
- package/agent/rules/basalt-charts.md +1 -1
- package/agent/rules/basalt-mantine.md +3 -3
- package/agent/rules/basalt-router.md +1 -1
- package/agent/rules/basalt-state.md +2 -2
- package/agent/rules/basalt-tokens.md +3 -3
- package/agent/skills/basalt-app/SKILL.md +137 -0
- package/agent/skills/basalt-charts/SKILL.md +179 -0
- package/agent/skills/basalt-design/SKILL.md +249 -0
- package/agent/templates/CLAUDE-block.md.tpl +8 -5
- package/agent/templates/__root.tpl +1 -1
- package/agent/templates/query-client.ts.tpl +1 -1
- package/configs/check.yml +8 -8
- package/configs/lefthook.yml +9 -5
- package/configs/oxlint-plugin.js +119 -7
- package/configs/oxlint-plugin.test.ts +73 -3
- package/configs/oxlint.json +2 -71
- package/dist/cli/index.d.ts +71 -28
- package/dist/cli/index.d.ts.map +1 -1
- package/dist/cli/index.js +134 -121
- package/dist/surfaces.d.ts +41 -17
- package/dist/surfaces.d.ts.map +1 -1
- package/dist/surfaces.js +37 -38
- package/dist/tokens/index.d.ts +1 -0
- package/dist/tokens/index.d.ts.map +1 -1
- package/dist/tokens/index.js +3 -0
- package/llms.txt +2 -2
- package/package.json +2 -2
- package/src/cli/check-theme.test.ts +12 -2
- package/src/cli/help.test.ts +93 -0
- package/src/cli/index.ts +332 -184
- package/src/cli/placement-engine.test.ts +194 -0
- package/src/cli/tarball-parity.test.ts +44 -0
- package/src/cli/template-format.test.ts +64 -0
- package/src/cli/template-ledger.test.ts +219 -0
- package/src/surfaces.ts +78 -43
- package/src/tokens/index.ts +6 -0
- /package/bin/{basalt.mjs → basalt-ui.mjs} +0 -0
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Placement-engine contract tests. init/sync write into consumers' git-tracked trees, which makes
|
|
3
|
+
* this the highest-risk code in the repo — every ownership promise the managed/seed model makes is
|
|
4
|
+
* asserted here:
|
|
5
|
+
*
|
|
6
|
+
* - sync is idempotent (second run is a byte-level no-op, manifest stable)
|
|
7
|
+
* - a locally-edited managed file is never clobbered (skipped + reported; --check exits 1)
|
|
8
|
+
* - --force overwrites managed files but never touches seeds
|
|
9
|
+
* - a seed is recreated only when missing
|
|
10
|
+
* - block-splicing preserves surrounding host content, appends when markers are absent, and
|
|
11
|
+
* errors loudly on duplicate markers instead of silently picking one
|
|
12
|
+
* - --check performs zero writes (filesystem is byte-identical afterwards)
|
|
13
|
+
* - exit codes: 0 clean / 1 drift
|
|
14
|
+
*/
|
|
15
|
+
import { afterEach, beforeEach, describe, expect, it } from 'bun:test'
|
|
16
|
+
import {
|
|
17
|
+
existsSync,
|
|
18
|
+
mkdirSync,
|
|
19
|
+
mkdtempSync,
|
|
20
|
+
readdirSync,
|
|
21
|
+
readFileSync,
|
|
22
|
+
rmSync,
|
|
23
|
+
statSync,
|
|
24
|
+
writeFileSync,
|
|
25
|
+
} from 'node:fs'
|
|
26
|
+
import { tmpdir } from 'node:os'
|
|
27
|
+
import { join, relative, resolve } from 'node:path'
|
|
28
|
+
import { fileURLToPath } from 'node:url'
|
|
29
|
+
|
|
30
|
+
import { init, sync } from './index.ts'
|
|
31
|
+
|
|
32
|
+
const PKG_ROOT = fileURLToPath(new URL('../../', import.meta.url))
|
|
33
|
+
const SHIPPED_RULE = readFileSync(resolve(PKG_ROOT, 'agent/rules/basalt-tokens.md'), 'utf8')
|
|
34
|
+
const SHIPPED_SKILL = readFileSync(resolve(PKG_ROOT, 'agent/skills/basalt-design/SKILL.md'), 'utf8')
|
|
35
|
+
|
|
36
|
+
let dir: string
|
|
37
|
+
|
|
38
|
+
beforeEach(() => {
|
|
39
|
+
dir = mkdtempSync(resolve(tmpdir(), 'basalt-engine-'))
|
|
40
|
+
writeFileSync(join(dir, 'package.json'), JSON.stringify({ name: 'fixture' }))
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
afterEach(() => {
|
|
44
|
+
rmSync(dir, { recursive: true, force: true })
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
function read(relPath: string): string {
|
|
48
|
+
return readFileSync(join(dir, relPath), 'utf8')
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function write(relPath: string, content: string): void {
|
|
52
|
+
const abs = join(dir, relPath)
|
|
53
|
+
mkdirSync(resolve(abs, '..'), { recursive: true })
|
|
54
|
+
writeFileSync(abs, content, 'utf8')
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Byte-level snapshot of every file under the fixture dir — the zero-writes/idempotency oracle. */
|
|
58
|
+
function snapshotDir(root: string = dir): Map<string, string> {
|
|
59
|
+
const out = new Map<string, string>()
|
|
60
|
+
function walk(d: string): void {
|
|
61
|
+
for (const name of readdirSync(d)) {
|
|
62
|
+
const abs = resolve(d, name)
|
|
63
|
+
if (statSync(abs).isDirectory()) walk(abs)
|
|
64
|
+
else out.set(relative(root, abs), readFileSync(abs, 'utf8'))
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
walk(root)
|
|
68
|
+
return out
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function silenced<T>(fn: () => T): T {
|
|
72
|
+
const originalLog = console.log
|
|
73
|
+
const originalError = console.error
|
|
74
|
+
console.log = () => {}
|
|
75
|
+
console.error = () => {}
|
|
76
|
+
try {
|
|
77
|
+
return fn()
|
|
78
|
+
} finally {
|
|
79
|
+
console.log = originalLog
|
|
80
|
+
console.error = originalError
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
describe('managed vs seed placement', () => {
|
|
85
|
+
it('init places rules AND skills as managed files, plus the toolchain seeds', () => {
|
|
86
|
+
silenced(() => init(dir))
|
|
87
|
+
expect(read('.claude/rules/basalt-tokens.md')).toBe(SHIPPED_RULE)
|
|
88
|
+
expect(read('.claude/skills/basalt-design/SKILL.md')).toBe(SHIPPED_SKILL)
|
|
89
|
+
expect(existsSync(join(dir, '.claude/skills/basalt-app/SKILL.md'))).toBe(true)
|
|
90
|
+
expect(existsSync(join(dir, '.claude/skills/basalt-charts/SKILL.md'))).toBe(true)
|
|
91
|
+
// Seeds are references, not copies — the toolchain auto-updates through node_modules.
|
|
92
|
+
expect(read('.oxlintrc.json')).toContain('./node_modules/basalt-ui/configs/oxlint.json')
|
|
93
|
+
expect(read('lefthook.yml')).toContain('node_modules/basalt-ui/configs/lefthook.yml')
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
it('sync is idempotent: the second run is a byte-level no-op and the manifest is stable', () => {
|
|
97
|
+
silenced(() => init(dir))
|
|
98
|
+
expect(silenced(() => sync({}, dir))).toBe(0)
|
|
99
|
+
const before = snapshotDir()
|
|
100
|
+
expect(silenced(() => sync({}, dir))).toBe(0)
|
|
101
|
+
expect(snapshotDir()).toEqual(before)
|
|
102
|
+
expect(silenced(() => sync({ check: true }, dir))).toBe(0)
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
it('a locally-edited managed file is never clobbered: skipped by sync, --check exits 1', () => {
|
|
106
|
+
silenced(() => init(dir))
|
|
107
|
+
write('.claude/rules/basalt-tokens.md', '# my local law\n')
|
|
108
|
+
expect(silenced(() => sync({}, dir))).toBe(0)
|
|
109
|
+
expect(read('.claude/rules/basalt-tokens.md')).toBe('# my local law\n')
|
|
110
|
+
expect(silenced(() => sync({ check: true }, dir))).toBe(1)
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
it('--force overwrites a drifted managed file but never touches a seed', () => {
|
|
114
|
+
silenced(() => init(dir))
|
|
115
|
+
write('.claude/rules/basalt-tokens.md', '# my local law\n')
|
|
116
|
+
write('.claude/skills/basalt-design/SKILL.md', '# my local skill\n')
|
|
117
|
+
write('DESIGN.md', '# my design\n')
|
|
118
|
+
write('lefthook.yml', 'pre-commit:\n commands: {}\n')
|
|
119
|
+
silenced(() => sync({ force: true }, dir))
|
|
120
|
+
expect(read('.claude/rules/basalt-tokens.md')).toBe(SHIPPED_RULE)
|
|
121
|
+
expect(read('.claude/skills/basalt-design/SKILL.md')).toBe(SHIPPED_SKILL)
|
|
122
|
+
expect(read('DESIGN.md')).toBe('# my design\n')
|
|
123
|
+
expect(read('lefthook.yml')).toBe('pre-commit:\n commands: {}\n')
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
it('a seed is recreated only when missing; an edited seed never drifts --check', () => {
|
|
127
|
+
silenced(() => init(dir))
|
|
128
|
+
rmSync(join(dir, 'DESIGN.md'))
|
|
129
|
+
silenced(() => sync({}, dir))
|
|
130
|
+
expect(existsSync(join(dir, 'DESIGN.md'))).toBe(true)
|
|
131
|
+
write('DESIGN.md', '# my design\n')
|
|
132
|
+
silenced(() => sync({}, dir))
|
|
133
|
+
expect(read('DESIGN.md')).toBe('# my design\n')
|
|
134
|
+
expect(silenced(() => sync({ check: true }, dir))).toBe(0)
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
it('a pre-existing consumer file at a managed dest is kept by init, then treated as drift', () => {
|
|
138
|
+
write('.claude/rules/basalt-tokens.md', '# predates basalt\n')
|
|
139
|
+
silenced(() => init(dir))
|
|
140
|
+
expect(read('.claude/rules/basalt-tokens.md')).toBe('# predates basalt\n')
|
|
141
|
+
expect(silenced(() => sync({ check: true }, dir))).toBe(1)
|
|
142
|
+
silenced(() => sync({ force: true }, dir))
|
|
143
|
+
expect(read('.claude/rules/basalt-tokens.md')).toBe(SHIPPED_RULE)
|
|
144
|
+
})
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
describe('block splicing (managed with markers)', () => {
|
|
148
|
+
it('appends the block when markers are absent, preserving existing host content', () => {
|
|
149
|
+
write('CLAUDE.md', '# fixture app\n\nMy own instructions.\n')
|
|
150
|
+
silenced(() => init(dir))
|
|
151
|
+
const host = read('CLAUDE.md')
|
|
152
|
+
expect(host.startsWith('# fixture app\n\nMy own instructions.')).toBe(true)
|
|
153
|
+
expect(host).toContain('<!-- basalt:begin')
|
|
154
|
+
expect(host).toContain('<!-- basalt:end -->')
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
it('replaces only the marked region on sync, preserving content before and after', () => {
|
|
158
|
+
silenced(() => init(dir))
|
|
159
|
+
// A stale region from an older basalt-ui, embedded in consumer-owned host content.
|
|
160
|
+
write(
|
|
161
|
+
'CLAUDE.md',
|
|
162
|
+
'# above\n\n<!-- basalt:begin 0.9.9 -->\nstale body\n<!-- basalt:end -->\n\n# below\n',
|
|
163
|
+
)
|
|
164
|
+
silenced(() => sync({ force: true }, dir))
|
|
165
|
+
const after = read('CLAUDE.md')
|
|
166
|
+
expect(after.startsWith('# above\n')).toBe(true)
|
|
167
|
+
expect(after.trimEnd().endsWith('# below')).toBe(true)
|
|
168
|
+
expect(after).not.toContain('stale body')
|
|
169
|
+
expect(after.match(/<!-- basalt:begin/g)?.length).toBe(1)
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
it('errors loudly on duplicate begin markers instead of silently picking one', () => {
|
|
173
|
+
silenced(() => init(dir))
|
|
174
|
+
const block = read('CLAUDE.md')
|
|
175
|
+
write('CLAUDE.md', `${block}\n\n${block}`)
|
|
176
|
+
expect(() => silenced(() => sync({}, dir))).toThrow(/duplicate/)
|
|
177
|
+
expect(() => silenced(() => sync({ check: true }, dir))).toThrow(/duplicate/)
|
|
178
|
+
})
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
describe('--check performs zero writes', () => {
|
|
182
|
+
it('leaves the filesystem byte-identical on a clean tree AND on a drifted tree', () => {
|
|
183
|
+
silenced(() => init(dir))
|
|
184
|
+
const clean = snapshotDir()
|
|
185
|
+
expect(silenced(() => sync({ check: true }, dir))).toBe(0)
|
|
186
|
+
expect(snapshotDir()).toEqual(clean)
|
|
187
|
+
|
|
188
|
+
write('.claude/rules/basalt-tokens.md', '# my local law\n')
|
|
189
|
+
rmSync(join(dir, 'DESIGN.md'))
|
|
190
|
+
const drifted = snapshotDir()
|
|
191
|
+
expect(silenced(() => sync({ check: true }, dir))).toBe(1)
|
|
192
|
+
expect(snapshotDir()).toEqual(drifted)
|
|
193
|
+
})
|
|
194
|
+
})
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fast tarball-parity approximation: every source path the CLI reads via readSource must exist on
|
|
3
|
+
* disk AND be covered by a package.json `files` entry — otherwise `init`/`sync` silently skip it
|
|
4
|
+
* for every npm consumer while working fine in this repo (exactly how the skills shipped outside
|
|
5
|
+
* the tarball at 1.0.0). The authoritative check against the real packed artifact runs in
|
|
6
|
+
* scripts/pack-test.sh (check-tarball-parity.mjs); this one runs on every `bun test`.
|
|
7
|
+
*/
|
|
8
|
+
import { describe, expect, it } from 'bun:test'
|
|
9
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
10
|
+
import { resolve } from 'node:path'
|
|
11
|
+
import { fileURLToPath } from 'node:url'
|
|
12
|
+
|
|
13
|
+
import { managedFiles } from './index.ts'
|
|
14
|
+
|
|
15
|
+
const PKG_ROOT = fileURLToPath(new URL('../../', import.meta.url))
|
|
16
|
+
const pkg = JSON.parse(readFileSync(resolve(PKG_ROOT, 'package.json'), 'utf8')) as {
|
|
17
|
+
files: string[]
|
|
18
|
+
bin: Record<string, string>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** True when `path` is matched by one of the `files` entries (dir prefix or exact file). */
|
|
22
|
+
function coveredByFilesField(path: string): boolean {
|
|
23
|
+
return pkg.files.some((entry) => {
|
|
24
|
+
const dir = entry.endsWith('/') ? entry : `${entry}/`
|
|
25
|
+
return path === entry || path.startsWith(dir)
|
|
26
|
+
})
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const sources = [...new Set(managedFiles({ hasRouter: true, hasQuery: true }).map((f) => f.source))]
|
|
30
|
+
|
|
31
|
+
describe('tarball parity (files-field approximation)', () => {
|
|
32
|
+
it.each(sources)('CLI-read source %s exists and is covered by the files field', (source) => {
|
|
33
|
+
expect(existsSync(resolve(PKG_ROOT, source))).toBe(true)
|
|
34
|
+
expect(coveredByFilesField(source)).toBe(true)
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('every bin entry points at a shipped file', () => {
|
|
38
|
+
for (const binPath of Object.values(pkg.bin)) {
|
|
39
|
+
const rel = binPath.replace(/^\.\//, '')
|
|
40
|
+
expect(existsSync(resolve(PKG_ROOT, rel))).toBe(true)
|
|
41
|
+
expect(coveredByFilesField(rel)).toBe(true)
|
|
42
|
+
}
|
|
43
|
+
})
|
|
44
|
+
})
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Format-idempotency for every shipped template that lands in a consumer VERBATIM — the `managed`
|
|
3
|
+
* files (rules, skills, the marker-spliced CLAUDE block; see `./index.ts`'s `managedFiles()`).
|
|
4
|
+
* `seed` templates are excluded: they are a one-time starting point the consumer immediately owns
|
|
5
|
+
* and may reformat however they like, so their shipped formatting is not load-bearing.
|
|
6
|
+
*
|
|
7
|
+
* basalt-ui itself MANDATES oxfmt in the shipped lefthook/CI templates — so if a shipped `copy`/
|
|
8
|
+
* `block` template's bytes are not already oxfmt-clean, the first thing a consumer does (run
|
|
9
|
+
* `basalt sync`, or just commit) puts oxlint/oxfmt and `sync --check` in permanent disagreement:
|
|
10
|
+
* `basalt sync` writes the drifted bytes, the lefthook pre-commit hook's own `oxfmt --check`
|
|
11
|
+
* rejects the commit, running `oxfmt` to fix it re-drifts the file from what `sync --check`
|
|
12
|
+
* expects. This is exactly how `CLAUDE-block.md.tpl` shipped: no blank line after
|
|
13
|
+
* `<!-- basalt:begin -->`, which oxfmt's own block-HTML-comment rule then rewrites, re-triggering
|
|
14
|
+
* drift forever. Assert every copy/block template round-trips through the exact oxfmt binary +
|
|
15
|
+
* config basalt-ui ships, unchanged — this class of "basalt's own gates fighting each other" bug
|
|
16
|
+
* must not recur.
|
|
17
|
+
*/
|
|
18
|
+
import { spawnSync } from 'node:child_process'
|
|
19
|
+
import { readFileSync } from 'node:fs'
|
|
20
|
+
import { resolve } from 'node:path'
|
|
21
|
+
import { describe, expect, it } from 'bun:test'
|
|
22
|
+
|
|
23
|
+
import { RULE_NAMES, SKILL_NAMES } from './index'
|
|
24
|
+
|
|
25
|
+
const PKG_ROOT = resolve(import.meta.dir, '../..')
|
|
26
|
+
const REPO_ROOT = resolve(PKG_ROOT, '../..')
|
|
27
|
+
const OXFMT_BIN = resolve(REPO_ROOT, 'node_modules/.bin/oxfmt')
|
|
28
|
+
const OXFMT_CONFIG = resolve(PKG_ROOT, 'configs/oxfmt.json')
|
|
29
|
+
|
|
30
|
+
/** Format `content` through the exact oxfmt binary + config basalt-ui ships; assert it is a no-op. */
|
|
31
|
+
function expectOxfmtNoop(fileName: string, content: string): void {
|
|
32
|
+
const result = spawnSync(OXFMT_BIN, ['--stdin-filepath', fileName, '-c', OXFMT_CONFIG], {
|
|
33
|
+
input: content,
|
|
34
|
+
encoding: 'utf8',
|
|
35
|
+
})
|
|
36
|
+
expect(result.status).toBe(0)
|
|
37
|
+
expect(result.stdout).toBe(content)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
describe('shipped managed templates are oxfmt-clean', () => {
|
|
41
|
+
it('CLAUDE-block.md.tpl (managed, marker-spliced) round-trips unchanged with placeholders filled', () => {
|
|
42
|
+
const tpl = readFileSync(resolve(PKG_ROOT, 'agent/templates/CLAUDE-block.md.tpl'), 'utf8')
|
|
43
|
+
const filled = tpl
|
|
44
|
+
.replace(/\{\{BASALT_VERSION\}\}/g, '1.0.0')
|
|
45
|
+
.replace(/\{\{APP_NAME\}\}/g, 'app')
|
|
46
|
+
expectOxfmtNoop('CLAUDE.md', filled)
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
it.each(RULE_NAMES)('agent/rules/basalt-%s.md (managed) round-trips unchanged', (name) => {
|
|
50
|
+
const content = readFileSync(resolve(PKG_ROOT, `agent/rules/basalt-${name}.md`), 'utf8')
|
|
51
|
+
expectOxfmtNoop(`basalt-${name}.md`, content)
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it.each(SKILL_NAMES)('agent/skills/%s/SKILL.md (managed) round-trips unchanged', (name) => {
|
|
55
|
+
const content = readFileSync(resolve(PKG_ROOT, `agent/skills/${name}/SKILL.md`), 'utf8')
|
|
56
|
+
expectOxfmtNoop('SKILL.md', content)
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it('configs/oxfmt.json (seeded verbatim as .oxfmtrc.json) round-trips unchanged', () => {
|
|
60
|
+
// A seed, but its bytes land verbatim and oxfmt itself reads the result — keep it clean too.
|
|
61
|
+
const content = readFileSync(resolve(PKG_ROOT, 'configs/oxfmt.json'), 'utf8')
|
|
62
|
+
expectOxfmtNoop('.oxfmtrc.json', content)
|
|
63
|
+
})
|
|
64
|
+
})
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Regression tests for the `classify()` ledger widening (see `./index.ts`'s `normalizeForLedger` +
|
|
3
|
+
* `writeUnit` + `classify`).
|
|
4
|
+
*
|
|
5
|
+
* `writeUnit` now records a NORMALIZED hash in the manifest (instead of a raw one) for every sync
|
|
6
|
+
* it performs, going forward — that closes the false-positive gap for any consumer whose manifest
|
|
7
|
+
* entry was written by a CLI carrying this fix. But a manifest entry written by an OLDER
|
|
8
|
+
* (pre-1.0.2) CLI holds a RAW hash — `classify`'s legacy path (`sha256(current) === manifestHash`)
|
|
9
|
+
* exists to keep matching that raw entry directly, and it is faithfully exercised below by seeding
|
|
10
|
+
* the manifest with a genuine `sha256(olderBlock)`, not `sha256(normalizeForLedger(olderBlock))`.
|
|
11
|
+
*
|
|
12
|
+
* Two scenarios collide differently against that raw legacy entry:
|
|
13
|
+
* - reformat ONLY (template body unchanged since the entry was written) → the same-version
|
|
14
|
+
* fallback (`normalizeForLedger(current) === normalizeForLedger(desired)`) still catches it,
|
|
15
|
+
* independent of the manifest hash → `unchanged`, healed silently, no `--force` — and the
|
|
16
|
+
* healing sync rewrites the manifest with the NORMALIZED hash, closing the gap for good for
|
|
17
|
+
* that consumer.
|
|
18
|
+
* - reformat AND a cross-version word change (the CLI bin rename baked into the block's own
|
|
19
|
+
* prose) together → NONE of `classify`'s three paths match a raw legacy hash → `drifted`, a
|
|
20
|
+
* real transitional gap that needs one `--force` to heal. See the comment above the legacy-hash
|
|
21
|
+
* path in `classify` (`./index.ts`) for the documented tradeoff.
|
|
22
|
+
*
|
|
23
|
+
* Also proves the two pre-existing same-version guarantees still hold:
|
|
24
|
+
* 2. a real word-level edit at the SAME version → still `drifted` → skipped without `--force`.
|
|
25
|
+
* 3. reformatting + a real edit together → still `drifted`.
|
|
26
|
+
*/
|
|
27
|
+
import { afterEach, beforeEach, describe, expect, it } from 'bun:test'
|
|
28
|
+
import { createHash } from 'node:crypto'
|
|
29
|
+
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
|
|
30
|
+
import { tmpdir } from 'node:os'
|
|
31
|
+
import { join, resolve } from 'node:path'
|
|
32
|
+
|
|
33
|
+
import { init, MANIFEST_PATH, normalizeForLedger, sync } from './index.ts'
|
|
34
|
+
|
|
35
|
+
function sha256(text: string): string {
|
|
36
|
+
return createHash('sha256').update(text, 'utf8').digest('hex')
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
let dir: string
|
|
40
|
+
|
|
41
|
+
function read(relPath: string): string {
|
|
42
|
+
return readFileSync(join(dir, relPath), 'utf8')
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function write(relPath: string, content: string): void {
|
|
46
|
+
writeFileSync(join(dir, relPath), content, 'utf8')
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Captures both console.log and console.error — sync() reports drift on either depending on flags. */
|
|
50
|
+
function capture(fn: () => number): { code: number; log: string } {
|
|
51
|
+
const originalLog = console.log
|
|
52
|
+
const originalError = console.error
|
|
53
|
+
let log = ''
|
|
54
|
+
console.log = (...args: unknown[]) => {
|
|
55
|
+
log += `${args.join(' ')}\n`
|
|
56
|
+
}
|
|
57
|
+
console.error = (...args: unknown[]) => {
|
|
58
|
+
log += `${args.join(' ')}\n`
|
|
59
|
+
}
|
|
60
|
+
try {
|
|
61
|
+
return { code: fn(), log }
|
|
62
|
+
} finally {
|
|
63
|
+
console.log = originalLog
|
|
64
|
+
console.error = originalError
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function silenced<T>(fn: () => T): T {
|
|
69
|
+
const originalLog = console.log
|
|
70
|
+
const originalError = console.error
|
|
71
|
+
console.log = () => {}
|
|
72
|
+
console.error = () => {}
|
|
73
|
+
try {
|
|
74
|
+
return fn()
|
|
75
|
+
} finally {
|
|
76
|
+
console.log = originalLog
|
|
77
|
+
console.error = originalError
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The exact argo repro: lefthook's oxfmt inserted an EXTRA blank line after
|
|
83
|
+
* `<!-- basalt:begin … -->` and another before `<!-- basalt:end -->` (a 35-line template rendered
|
|
84
|
+
* as 37 lines post-oxfmt) — a pure formatting delta, zero word changes. Inserted unconditionally
|
|
85
|
+
* (not just when absent) so the fixture reproduces the delta regardless of how many blank lines the
|
|
86
|
+
* live template already ships around its markers.
|
|
87
|
+
*/
|
|
88
|
+
function simulateOxfmtReformat(host: string): string {
|
|
89
|
+
const beginLineEnd = host.indexOf('\n', host.indexOf('<!-- basalt:begin')) + 1
|
|
90
|
+
const endMarkerStart = host.indexOf('<!-- basalt:end -->')
|
|
91
|
+
return (
|
|
92
|
+
host.slice(0, beginLineEnd) +
|
|
93
|
+
'\n' +
|
|
94
|
+
host.slice(beginLineEnd, endMarkerStart) +
|
|
95
|
+
'\n' +
|
|
96
|
+
host.slice(endMarkerStart)
|
|
97
|
+
)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
beforeEach(() => {
|
|
101
|
+
dir = mkdtempSync(resolve(tmpdir(), 'basalt-ledger-'))
|
|
102
|
+
writeFileSync(join(dir, 'package.json'), JSON.stringify({ name: 'fixture' }))
|
|
103
|
+
silenced(() => init(dir))
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
afterEach(() => {
|
|
107
|
+
rmSync(dir, { recursive: true, force: true })
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
describe('classify ledger: cross-version format-only drift vs a real edit', () => {
|
|
111
|
+
it('a raw legacy manifest hash + reformat + a cross-version word change classifies drifted — the documented transitional gap, healed by one --force', () => {
|
|
112
|
+
const currentBlock = read('CLAUDE.md').trimEnd() // == today's desired (v1.0.1-shaped) block
|
|
113
|
+
expect(currentBlock).toContain('basalt-ui')
|
|
114
|
+
|
|
115
|
+
// Simulate the v1.0.0 body: genuinely different WORDS (the pre-rename bin name), not just an
|
|
116
|
+
// older version token — this is what makes the scenario cross-version, not same-version.
|
|
117
|
+
const olderBlock = currentBlock.replace(/basalt-ui/g, 'basalt')
|
|
118
|
+
expect(olderBlock).not.toBe(currentBlock)
|
|
119
|
+
expect(olderBlock).toContain('bunx basalt sync')
|
|
120
|
+
|
|
121
|
+
// What a PRE-1.0.2 CLI's writeUnit actually recorded at that sync: a RAW hash of the exact
|
|
122
|
+
// bytes it wrote — the fix only normalizes hashes written by a CLI carrying it, so a genuine
|
|
123
|
+
// legacy manifest entry looks like this, not `sha256(normalizeForLedger(olderBlock))`.
|
|
124
|
+
const manifestHash = sha256(olderBlock)
|
|
125
|
+
write(
|
|
126
|
+
MANIFEST_PATH,
|
|
127
|
+
JSON.stringify(
|
|
128
|
+
{ version: 1, basaltVersion: '1.0.0', files: { 'CLAUDE.md': manifestHash } },
|
|
129
|
+
null,
|
|
130
|
+
2,
|
|
131
|
+
),
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
// What actually sits on disk: the older body, reformatted by the consumer's own lefthook
|
|
135
|
+
// oxfmt AFTER that older sync — the real argo repro shape.
|
|
136
|
+
const onDisk = simulateOxfmtReformat(olderBlock)
|
|
137
|
+
expect(onDisk).not.toBe(olderBlock)
|
|
138
|
+
write('CLAUDE.md', `${onDisk}\n`)
|
|
139
|
+
|
|
140
|
+
const { code, log } = capture(() => sync({}, dir))
|
|
141
|
+
expect(code).toBe(0)
|
|
142
|
+
// NOT healed — none of classify's three paths match a raw legacy hash once the words have
|
|
143
|
+
// also changed cross-version. This is the residual transitional gap documented on `classify`.
|
|
144
|
+
expect(read('CLAUDE.md')).toBe(`${onDisk}\n`)
|
|
145
|
+
expect(log).toContain('locally edited')
|
|
146
|
+
|
|
147
|
+
expect(silenced(() => sync({ check: true }, dir))).toBe(1)
|
|
148
|
+
|
|
149
|
+
// One --force heals it straight to the CURRENT (v1.0.1-shaped) rendering.
|
|
150
|
+
silenced(() => sync({ force: true }, dir))
|
|
151
|
+
expect(read('CLAUDE.md').trimEnd()).toBe(currentBlock)
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
it('a raw legacy manifest hash + reformat only (no word change) still classifies unchanged via the same-version fallback, heals without --force, and the manifest is rewritten with the normalized hash', () => {
|
|
155
|
+
const currentBlock = read('CLAUDE.md').trimEnd() // == today's desired block; body unchanged
|
|
156
|
+
|
|
157
|
+
// What a PRE-1.0.2 CLI's writeUnit recorded for THIS SAME body: a raw hash, not normalized.
|
|
158
|
+
const manifestHash = sha256(currentBlock)
|
|
159
|
+
write(
|
|
160
|
+
MANIFEST_PATH,
|
|
161
|
+
JSON.stringify(
|
|
162
|
+
{ version: 1, basaltVersion: '1.0.0', files: { 'CLAUDE.md': manifestHash } },
|
|
163
|
+
null,
|
|
164
|
+
2,
|
|
165
|
+
),
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
// What sits on disk: the SAME body, reformatted by the consumer's own lefthook oxfmt — no
|
|
169
|
+
// template-body change since the manifest entry was written.
|
|
170
|
+
const onDisk = simulateOxfmtReformat(currentBlock)
|
|
171
|
+
expect(onDisk).not.toBe(currentBlock)
|
|
172
|
+
write('CLAUDE.md', `${onDisk}\n`)
|
|
173
|
+
|
|
174
|
+
const { code, log } = capture(() => sync({}, dir))
|
|
175
|
+
expect(code).toBe(0)
|
|
176
|
+
// Healed straight to the current rendering, no --force needed: the same-version fallback
|
|
177
|
+
// (`normalizeForLedger(current) === normalizeForLedger(desired)`) matches regardless of what
|
|
178
|
+
// the manifest hash says.
|
|
179
|
+
expect(read('CLAUDE.md').trimEnd()).toBe(currentBlock)
|
|
180
|
+
expect(log).not.toContain('locally edited')
|
|
181
|
+
|
|
182
|
+
// The gap is now closed for good for this consumer: the healing sync rewrote the manifest
|
|
183
|
+
// entry with the NORMALIZED hash (writeUnit's form), not the raw legacy one.
|
|
184
|
+
const manifest = JSON.parse(read(MANIFEST_PATH)) as { files: Record<string, string> }
|
|
185
|
+
expect(manifest.files['CLAUDE.md']).toBe(sha256(normalizeForLedger(currentBlock)))
|
|
186
|
+
|
|
187
|
+
expect(silenced(() => sync({ check: true }, dir))).toBe(0)
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
it('a genuine word-level edit inside the block classifies drifted and is skipped without --force', () => {
|
|
191
|
+
const original = read('CLAUDE.md')
|
|
192
|
+
const edited = original.replace('React 19', 'React 18 (pinned by our team)')
|
|
193
|
+
expect(edited).not.toBe(original)
|
|
194
|
+
write('CLAUDE.md', edited)
|
|
195
|
+
|
|
196
|
+
const { code, log } = capture(() => sync({}, dir))
|
|
197
|
+
expect(code).toBe(0)
|
|
198
|
+
// Untouched — a real edit is never silently overwritten.
|
|
199
|
+
expect(read('CLAUDE.md')).toBe(edited)
|
|
200
|
+
expect(log).toContain('locally edited')
|
|
201
|
+
|
|
202
|
+
expect(silenced(() => sync({ check: true }, dir))).toBe(1)
|
|
203
|
+
|
|
204
|
+
silenced(() => sync({ force: true }, dir))
|
|
205
|
+
expect(read('CLAUDE.md')).toBe(original)
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
it('reformatting AND a real edit together still classifies drifted (whitespace normalization never masks word changes)', () => {
|
|
209
|
+
const original = read('CLAUDE.md')
|
|
210
|
+
const reformattedAndEdited = simulateOxfmtReformat(original).replace(
|
|
211
|
+
'React 19',
|
|
212
|
+
'React 18 (pinned by our team)',
|
|
213
|
+
)
|
|
214
|
+
write('CLAUDE.md', reformattedAndEdited)
|
|
215
|
+
|
|
216
|
+
expect(silenced(() => sync({ check: true }, dir))).toBe(1)
|
|
217
|
+
expect(read('CLAUDE.md')).toBe(reformattedAndEdited)
|
|
218
|
+
})
|
|
219
|
+
})
|