import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import path from 'node:path'; import { fixture } from './helpers.mjs'; import { proposePatch, proposeFile, readText, applyChange, hash } from '../src/server/files.mjs'; test('exact patches preserve BOM/CRLF, unicode and all unselected bytes and back up the original on apply', t => { const f = fixture(t), target = path.join(f.projectRoot, 'code.ts'); const before = Buffer.from('\uFEFF// 保留🙂\r\nexport const value = 1;\r\n\r\nexport function keep() { return "原文"; }\r\n'); fs.writeFileSync(target, before); const file = readText(f.project, 'code.ts'); assert.equal(file.bom, true); const result = proposePatch(f.store, f.session, { path: file.path, base_sha256: file.sha256.toUpperCase(), reason: 'precise replacement', edits: [{ old_text: 'export const value = 1;\n', new_text: 'export const value = 2;\nexport const added = true;\n' }] }); assert.equal(result.status, 'awaiting_review'); assert.equal(result.patch.lineEnding, 'crlf'); assert.equal(result.patch.preservedBom, true); assert.deepEqual(fs.readFileSync(target), before); const change = f.session.changes[0]; assert.equal(change.editKind, 'exact-patch'); assert.equal(change.baseContent, before.toString('utf8')); assert.equal(change.baseSha256, hash(before)); assert.equal(change.patch.ranges[0].startLine, 2); assert.equal(change.patch.ranges[0].anchorSha256, hash(Buffer.from('export const value = 1;\r\n'))); applyChange(f.store, f.session, result.change_id); const expected = Buffer.from(before.toString('utf8').replace('export const value = 1;\r\n', 'export const value = 2;\r\nexport const added = true;\r\n')); assert.deepEqual(fs.readFileSync(target), expected); assert.deepEqual(fs.readFileSync(path.join(f.data, 'backups', f.session.id, change.id + '.txt')), before); }); test('several patches are matched against one original snapshot in any input order and keep LF', t => { const f = fixture(t), target = path.join(f.projectRoot, 'code.js'); fs.writeFileSync(target, 'const first = 1;\nconst second = 2;\n// trailing\n'); const file = readText(f.project, 'code.js'); const result = proposePatch(f.store, f.session, { path: file.path, base_sha256: file.sha256, reason: 'two edits', edits: [ { old_text: 'const second = 2;\r\n', new_text: 'const second = 20;\r\n' }, { old_text: 'const first = 1;', new_text: 'const first = 10;' }] }); assert.equal(result.patch.edits, 2); assert.deepEqual(result.patch.ranges.map(value => value.startLine), [1, 2]); applyChange(f.store, f.session, result.change_id); assert.equal(fs.readFileSync(target, 'utf8'), 'const first = 10;\nconst second = 20;\n// trailing\n'); }); test('mixed line endings require exact anchors and preserve untouched formatting', t => { const f = fixture(t), target = path.join(f.projectRoot, 'mixed.txt'); fs.writeFileSync(target, 'first\r\nsecond\nthird\r\n'); const file = readText(f.project, 'mixed.txt'), args = { path: file.path, base_sha256: file.sha256, reason: 'mixed' }; assert.throws(() => proposePatch(f.store, f.session, { ...args, edits: [{ old_text: 'first\nsecond', new_text: 'changed' }] }), { status: 409 }); const result = proposePatch(f.store, f.session, { ...args, edits: [{ old_text: 'second\n', new_text: 'replacement\n' }] }); assert.equal(result.patch.lineEnding, 'mixed'); applyChange(f.store, f.session, result.change_id); assert.equal(fs.readFileSync(target, 'utf8'), 'first\r\nreplacement\nthird\r\n'); }); test('ambiguous, overlapping, missing and malformed patch anchors never create a proposal or write', t => { const f = fixture(t), target = path.join(f.projectRoot, 'anchors.txt'); const original = 'repeat repeat\nabcdef\n'; fs.writeFileSync(target, original); const file = readText(f.project, 'anchors.txt'), args = { path: file.path, base_sha256: file.sha256, reason: 'reject invalid' }; const invalid = [[], [{ old_text: '', new_text: 'x' }], [{ old_text: 'repeat', new_text: 'x' }], [{ old_text: 'missing', new_text: 'x' }], [{ old_text: 'abc', new_text: 'x' }, { old_text: 'bcd', new_text: 'y' }], [null], [{ old_text: 1, new_text: 'x' }], [{ old_text: 'abc', new_text: '\0' }], [{ old_text: 'abc', new_text: 42 }], Array.from({ length: 33 }, () => ({ old_text: 'abc', new_text: 'x' }))]; for (const edits of invalid) assert.throws(() => proposePatch(f.store, f.session, { ...args, edits })); assert.throws(() => proposePatch(f.store, f.session, { ...args, base_sha256: 'invalid', edits: [{ old_text: 'abc', new_text: 'x' }] })); assert.throws(() => proposePatch(f.store, f.session, { ...args, path: '../anchors.txt', edits: [{ old_text: 'abc', new_text: 'x' }] })); assert.equal(f.session.changes.length, 0); assert.equal(fs.readFileSync(target, 'utf8'), original); }); test('stale original hashes and changes before apply preserve owner edits and mark conflicts', t => { const f = fixture(t), target = path.join(f.projectRoot, 'value.txt'); fs.writeFileSync(target, 'value = 1\n'); const file = readText(f.project, 'value.txt'), args = { path: file.path, base_sha256: file.sha256, reason: 'fix', edits: [{ old_text: 'value = 1', new_text: 'value = 2' }] }; const result = proposePatch(f.store, f.session, args); fs.writeFileSync(target, 'owner = 3\n'); assert.throws(() => proposePatch(f.store, f.session, args), { status: 409 }); assert.throws(() => applyChange(f.store, f.session, result.change_id), { status: 409 }); assert.equal(f.session.changes[0].status, 'conflict'); assert.equal(fs.readFileSync(target, 'utf8'), 'owner = 3\n'); }); test('no-op BOM patches/full content produce no change and replacements cannot split Unicode bytes', t => { const f = fixture(t), target = path.join(f.projectRoot, 'unicode.txt'); const contents = '\uFEFFkeep🙂\n'; fs.writeFileSync(target, contents); const file = readText(f.project, 'unicode.txt'), args = { path: file.path, base_sha256: file.sha256, reason: 'noop' }; assert.equal(proposePatch(f.store, f.session, { ...args, edits: [{ old_text: 'keep', new_text: 'keep' }] }).status, 'unchanged'); assert.equal(proposeFile(f.store, f.session, { path: file.path, content: contents }).status, 'unchanged'); assert.throws(() => proposePatch(f.store, f.session, { ...args, edits: [{ old_text: '\ud83d', new_text: 'x' }] })); assert.throws(() => proposePatch(f.store, f.session, { ...args, edits: [{ old_text: 'keep', new_text: '\ud83d' }] })); assert.equal(f.session.changes.length, 0); assert.equal(fs.readFileSync(target, 'utf8'), contents); });