@anureo/refactor 0.0.0-stage → 0.3.1

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.
@@ -0,0 +1,82 @@
1
+ import { expect, test } from 'bun:test'
2
+ import { mkdir, readFile, rm, stat } from 'node:fs/promises'
3
+ import { join } from 'node:path'
4
+ import { saveReport } from '../src/report-store'
5
+ import type { RefactorReport } from '../src/results'
6
+ import { withRun } from '../src/run-lifecycle'
7
+ import { fixture } from './helpers'
8
+
9
+ test('lifecycle holds the lock through body execution and records synchronous observer failures in order', async () => {
10
+ const context = await fixture()
11
+ const events: string[] = []
12
+ let competingBodyRan = false
13
+ const report = await withRun({
14
+ ...context,
15
+ onProgress: event => {
16
+ events.push(event)
17
+ throw new Error(`Observer ${events.length}`)
18
+ },
19
+ }, async (report, progress) => {
20
+ expect(report.status).toBe('incomplete')
21
+ expect(events).toEqual([`run: ${report.directory}`])
22
+ await expect(withRun(context, async () => { competingBodyRan = true })).rejects.toThrow('Cannot acquire refactor lock')
23
+ expect((await stat(join(context.cwd, '.anureo/refactor/writer.lock'))).isFile()).toBe(true)
24
+ progress('body: finished')
25
+ })
26
+ expect(competingBodyRan).toBe(false)
27
+ expect(events).toEqual([`run: ${report.directory}`, 'body: finished'])
28
+ expect(report.status).toBe('completed')
29
+ expect(report.progressErrors).toEqual(['Observer 1', 'Observer 2'])
30
+ await expectFinalizedRun(report)
31
+ })
32
+
33
+ test('lifecycle publishes classified body failures before releasing the lock', async () => {
34
+ const context = await fixture()
35
+ const report = await withRun(context, async () => {
36
+ throw Object.assign(new Error('Body unavailable'), {
37
+ code: 'RECEIPT_LOST', outcomeUnknown: true, details: { cleanupErrors: ['Release unavailable'] },
38
+ })
39
+ })
40
+ expect(report.status).toBe('incomplete')
41
+ expect(report.errors).toEqual(['Body unavailable'])
42
+ expect(report.failure).toEqual({ code: 'RECEIPT_LOST', outcomeUnknown: true, cleanupFailed: true })
43
+ await expectFinalizedRun(report)
44
+ })
45
+
46
+ // Obstruct real paths so failure precedence is exercised without replacing filesystem calls.
47
+ test.each([
48
+ { publicationFails: true, removalFails: false },
49
+ { publicationFails: false, removalFails: true },
50
+ { publicationFails: true, removalFails: true },
51
+ ])('finalization handles publication failure $publicationFails and removal failure $removalFails', async ({ publicationFails, removalFails }) => {
52
+ const context = await fixture()
53
+ const lockPath = join(context.cwd, '.anureo/refactor/writer.lock')
54
+ let directory = ''
55
+ let checkpoint = ''
56
+ const run = withRun(context, async report => {
57
+ directory = report.directory
58
+ await saveReport(report)
59
+ checkpoint = await readFile(join(directory, 'report.json'), 'utf8')
60
+ if (publicationFails) await mkdir(join(directory, 'report.json.tmp'))
61
+ if (removalFails) {
62
+ await rm(lockPath)
63
+ await mkdir(lockPath)
64
+ }
65
+ })
66
+ const error = await run.then(() => null, cause => cause)
67
+ expect(error).toMatchObject({ path: removalFails ? lockPath : join(directory, 'report.json.tmp') })
68
+ const saved = await readFile(join(directory, 'report.json'), 'utf8')
69
+ if (publicationFails) expect(saved).toBe(checkpoint)
70
+ else expect(JSON.parse(saved).status).toBe('completed')
71
+ if (removalFails) expect((await stat(lockPath)).isDirectory()).toBe(true)
72
+ else await expect(stat(lockPath)).rejects.toMatchObject({ code: 'ENOENT' })
73
+ })
74
+
75
+ /** Assert the returned record was fully published and writer ownership was released. */
76
+ async function expectFinalizedRun(report: RefactorReport): Promise<void> {
77
+ const saved = await readFile(join(report.directory, 'report.json'), 'utf8')
78
+ expect(JSON.parse(saved)).toEqual(report)
79
+ expect(saved.endsWith('\n')).toBe(true)
80
+ await expect(stat(join(report.directory, 'report.json.tmp'))).rejects.toMatchObject({ code: 'ENOENT' })
81
+ await expect(stat(join(report.cwd, '.anureo/refactor/writer.lock'))).rejects.toMatchObject({ code: 'ENOENT' })
82
+ }
@@ -0,0 +1,99 @@
1
+ // Verifies Git snapshots and source eligibility against real filesystem entries.
2
+ import { expect, test } from 'bun:test'
3
+ import { chmod, lstat, mkdir, rm, symlink, writeFile } from 'node:fs/promises'
4
+ import { join } from 'node:path'
5
+ import { captureTree, changes, treeVersion } from '../src/git-tree'
6
+ import { gitFiles } from '../src/git'
7
+ import { exec } from '../src/process'
8
+ import { isSource, isTextSource } from '../src/source-policy'
9
+ import { fixture, temporary } from './helpers'
10
+
11
+ /** Exercise Git visibility separately from snapshot exclusions, including force-added run storage. */
12
+ test('snapshots sort visible files, omit deleted entries and metadata, and retain non-source files', async () => {
13
+ const { cwd } = await fixture()
14
+ await writeFile(join(cwd, '.gitignore'), '.anureo/\nignored.ts\n')
15
+ await writeFile(join(cwd, 'ignored.ts'), 'ignored')
16
+ await writeFile(join(cwd, 'notes.txt'), 'untracked text')
17
+ await mkdir(join(cwd, '.anureo'))
18
+ await writeFile(join(cwd, '.anureo', 'report.ts'), 'run output')
19
+ await exec('git', ['-C', cwd, 'add', '--force', '.anureo/report.ts'])
20
+ await rm(join(cwd, 'b.ts'))
21
+
22
+ expect(await gitFiles(cwd)).toEqual(['.anureo/report.ts', '.gitignore', 'a.ts', 'b.ts', 'notes.txt'])
23
+ const tree = await captureTree(cwd)
24
+ expect([...tree.keys()]).toEqual(['.gitignore', 'a.ts', 'notes.txt'])
25
+ const version = treeVersion(tree)
26
+ await writeFile(join(cwd, '.anureo', 'report.ts'), 'changed run output')
27
+ expect(treeVersion(await captureTree(cwd))).toBe(version)
28
+ })
29
+
30
+ /** Compare real file/link edits so content, mode and link text remain independently observable. */
31
+ test.skipIf(process.platform === 'win32')('snapshot changes track content and modes, and hash symlink text without reading its destination', async () => {
32
+ const { cwd } = await fixture()
33
+ const outside = await temporary()
34
+ const destination = join(outside, 'destination.ts')
35
+ await writeFile(destination, 'external content')
36
+ await symlink(destination, join(cwd, 'linked.ts'))
37
+ const before = await captureTree(cwd)
38
+
39
+ await writeFile(destination, 'changed external content')
40
+ expect(changes(before, await captureTree(cwd))).toEqual([])
41
+ await writeFile(join(cwd, 'a.ts'), 'changed content')
42
+ const mode = (await lstat(join(cwd, 'b.ts'))).mode
43
+ await chmod(join(cwd, 'b.ts'), mode ^ 0o100)
44
+ await rm(join(cwd, 'linked.ts'))
45
+ await symlink(join(outside, 'missing.ts'), join(cwd, 'linked.ts'))
46
+ await writeFile(join(cwd, 'new.ts'), 'new content')
47
+
48
+ const after = await captureTree(cwd)
49
+ expect(changes(before, after)).toEqual(['a.ts', 'b.ts', 'linked.ts', 'new.ts'])
50
+ expect(treeVersion(after)).not.toBe(treeVersion(before))
51
+ expect(treeVersion(new Map([...after].reverse()))).not.toBe(treeVersion(after))
52
+ await rm(join(cwd, 'a.ts'))
53
+ expect(changes(after, await captureTree(cwd))).toEqual(['a.ts'])
54
+ })
55
+
56
+ /** Exercise scope precedence, path inspection and text detection independently. */
57
+ test('source inventory accepts unfamiliar names and rejects unsafe paths, binary and non-file targets', async () => {
58
+ const { cwd } = await fixture()
59
+ const outside = await temporary()
60
+ expect(isSource('src/main.ts')).toBe(true)
61
+ expect(isSource('types.d.mts')).toBe(true)
62
+ expect(isSource('bin/outil')).toBe(true)
63
+ for (const file of ['docs/main.ts', 'node_modules/main.ts', 'README.md']) {
64
+ expect(isSource(file)).toBe(false)
65
+ }
66
+ expect(isSource('README.md', ['README.md'])).toBe(true)
67
+ expect(isSource('README.md', ['README.md'], ['*.md'])).toBe(false)
68
+ expect(isSource('docs/main.ts', ['docs/main.ts'])).toBe(false)
69
+ for (const file of ['/absolute.ts', '../escape.ts', './a.ts', 'src//a.ts', 'src/../a.ts', 'src\\a.ts', 'docs/main.ts']) {
70
+ await expect(isTextSource(cwd, file)).rejects.toThrow(`Invalid source path: ${file}`)
71
+ }
72
+ await expect(isTextSource(cwd, 'a.ts')).resolves.toBe(true)
73
+ await expect(isTextSource(cwd, 'missing/new.ts')).resolves.toBe(false)
74
+ expect(await lstat(join(cwd, 'missing')).catch(() => null)).toBeNull()
75
+
76
+ await symlink(outside, join(cwd, 'linked'), 'junction')
77
+ await expect(isTextSource(cwd, 'linked/new.ts')).rejects.toThrow('Symlink source paths are unsupported: linked/new.ts')
78
+ await mkdir(join(cwd, 'directory.ts'))
79
+ await expect(isTextSource(cwd, 'directory.ts')).resolves.toBe(false)
80
+ await writeFile(join(cwd, 'archive.dsl'), Buffer.from([0xff, 0, 0x80]))
81
+ await expect(isTextSource(cwd, 'archive.dsl')).resolves.toBe(false)
82
+ await writeFile(join(cwd, '构建'), Buffer.from('\uFEFFrun job\r\n', 'utf16le'))
83
+ await expect(isTextSource(cwd, '构建')).resolves.toBe(true)
84
+ await writeFile(join(cwd, '构建'), Buffer.from([0xfe, 0xff, 0x00, 0x61]))
85
+ await expect(isTextSource(cwd, '构建')).resolves.toBe(true)
86
+ })
87
+
88
+ /** Ensure a replaced ancestor is an actual failure rather than an apparently missing tracked file. */
89
+ test.skipIf(process.platform === 'win32')('snapshot and source validation propagate non-ENOENT filesystem failures', async () => {
90
+ const { cwd } = await fixture()
91
+ await mkdir(join(cwd, 'parent'))
92
+ await writeFile(join(cwd, 'parent', 'child.ts'), 'tracked content')
93
+ await exec('git', ['-C', cwd, 'add', 'parent/child.ts'])
94
+ await rm(join(cwd, 'parent'), { recursive: true })
95
+ await writeFile(join(cwd, 'parent'), 'replaced ancestor')
96
+
97
+ await expect(isTextSource(cwd, 'parent/child.ts')).rejects.toMatchObject({ code: 'ENOTDIR' })
98
+ await expect(captureTree(cwd)).rejects.toMatchObject({ code: 'ENOTDIR' })
99
+ })
package/tsconfig.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "Preserve",
5
+ "moduleResolution": "Bundler",
6
+ "strict": true,
7
+ "noEmit": true,
8
+ "skipLibCheck": true,
9
+ "types": ["bun", "node"]
10
+ },
11
+ "include": ["src", "bin", "tests"]
12
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.