Files
apes-sidebar/tests/diff-binary-row.spec.tsx
T
ymac e4b189b7b5 fix(diff): name the rows git sends without ---/+++
git emits no ---/+++ for a binary file, a pure rename and a mode-only
change, and parseUnifiedDiff only ever read paths from those two lines.
The section therefore kept oldPath/newPath === '', so DiffFiles drew a
header carrying just the binary badge and no file name (sandwiched
between two .md rows, which is exactly how it reads as if those .md files
were the binary ones), and a pure rename / mode-only change collapsed to
an empty disabled button (no path, and fileTag cannot see a rename).

Recover the paths in order of authority: ---/+++ when present, else the
section's own `rename from|to`, `Binary files A and B differ`, or the
`diff --git` header. Only the mode-only shape needs the last one: it takes
the ` b/` whose two sides name the same file, and leaves the path empty
rather than guessing. ---/+++ values also lose the TAB git appends to a
spaced path, and decodeGitPath undoes git's C-quoting so diff text from a
default-quotePath source still renders the real file name.

runGit gains `-c core.quotePath=false` for the same reason at the source:
git's default turns a space/CJK path into an octal-escaped quoted string,
which every diff surface then renders as the file name. The `-z`-framed
porcelain callers are unaffected (git never quotes NUL-framed output).

Verified against taktop_redo 8b4b933, the commit whose three .jpg rows
rendered as bare badges: all three now carry their full path, and real git
output for space/CJK paths parses with no escapes and no trailing TAB.
New tests: tests/diff-binary-row.spec.tsx (rendered rows) and the path
matrix in tests/diff-rows.spec.ts; tests/git-spawn.spec.ts now derives the
subcommand from runGit's prefix instead of counting its flags.

tests/git.spec.ts had pinned the empty path (`oldPath === ''`) as the known
behaviour of a mode/rename-only section; it now asserts the recovered paths
instead, and the binary section there asserts its own name too.
2026-09-14 00:08:38 +08:00

99 lines
3.8 KiB
TypeScript

// @vitest-environment jsdom
/**
* The rows git sends without `---`/`+++` (binary file, pure rename, mode-only
* change) must still name their file. The regression this locks down: the
* row rendered as a bare badge, so a reader saw「二进制」sandwiched between two
* .md rows and reasonably concluded the .md files were the binary ones.
*/
import { afterEach, describe, expect, it } from 'vitest'
import { createElement } from 'react'
import { createRoot, type Root } from 'react-dom/client'
import { act } from 'react-dom/test-utils'
import { DiffFiles } from '../src/client/diff/DiffFiles.tsx'
import { t } from '../src/client/locales.ts'
import { setupReactAct } from './test-utils.ts'
setupReactAct()
afterEach(() => { document.body.innerHTML = '' })
/** Render one diff and return its container plus the file-header buttons. */
function render(diff: string): { container: HTMLElement; rows: HTMLButtonElement[] } {
const container = document.createElement('div')
document.body.append(container)
const root: Root = createRoot(container)
act(() => { root.render(createElement(DiffFiles, { diff })) })
return { container, rows: [...container.querySelectorAll<HTMLButtonElement>('button')] }
}
describe('DiffFiles rows without ---/+++', () => {
it('names a binary row and keeps it unexpandable', () => {
const { container, rows } = render([
'diff --git a/docs/20_D2_Tree_current.jpg b/docs/20_D2_Tree_current.jpg',
'new file mode 100644',
'index 0000000..1234567',
'Binary files /dev/null and b/docs/20_D2_Tree_current.jpg differ',
'diff --git a/CURRENT_STATUS.md b/CURRENT_STATUS.md',
'--- a/CURRENT_STATUS.md',
'+++ b/CURRENT_STATUS.md',
'@@ -1 +1 @@',
'-a',
'+b',
].join('\n'))
expect(rows).toHaveLength(2)
const binaryRow = rows[0]!
expect(binaryRow.textContent).toContain('docs/20_D2_Tree_current.jpg')
expect(binaryRow.textContent).toContain(t('diffBinary'))
// No path, no way to tell what the badge belongs to: not expandable, and
// no toggle affordance either.
expect(binaryRow.disabled).toBe(true)
expect(binaryRow.hasAttribute('aria-expanded')).toBe(false)
// The text row next to it is untouched — it is the one that kept its name.
expect(rows[1]!.disabled).toBe(false)
expect(container.textContent).toContain('CURRENT_STATUS.md')
})
it('names a pure rename on both sides', () => {
const { rows } = render([
'diff --git a/old/name.md b/new/name.md',
'similarity index 100%',
'rename from old/name.md',
'rename to new/name.md',
].join('\n'))
expect(rows).toHaveLength(1)
const row = rows[0]!
expect(row.textContent).toContain('new/name.md')
expect(row.textContent).toContain('old/name.md')
expect(row.textContent).toContain(t('diffRenamed'))
expect(row.disabled).toBe(true)
})
it('names a mode-only row without pretending it was renamed', () => {
const { rows } = render([
'diff --git a/tools/run.sh b/tools/run.sh',
'old mode 100644',
'new mode 100755',
].join('\n'))
expect(rows).toHaveLength(1)
const row = rows[0]!
expect(row.textContent).toContain('tools/run.sh')
expect(row.textContent).not.toContain(t('diffRenamed'))
expect(row.disabled).toBe(true)
})
it('renders a C-quoted path as the file name it stands for', () => {
const { container, rows } = render([
'diff --git "a/docs/\\346\\226\\207\\344\\273\\266.md" "b/docs/\\346\\226\\207\\344\\273\\266.md"',
'new file mode 100644',
'index 0000000..1234567',
'Binary files /dev/null and "b/docs/\\346\\226\\207\\344\\273\\266.md" differ',
].join('\n'))
expect(rows[0]!.textContent).toContain('docs/文件.md')
expect(container.textContent).not.toContain('\\346')
})
})