-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathsummary-git-path.test.ts
More file actions
175 lines (161 loc) · 5.41 KB
/
summary-git-path.test.ts
File metadata and controls
175 lines (161 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { describe, test, expect } from "bun:test"
import path from "path"
import { SessionSummary } from "../../src/session/summary"
import { Instance } from "../../src/project/instance"
import { Storage } from "../../src/storage/storage"
import { Log } from "../../src/util/log"
import { Identifier } from "../../src/id/id"
/**
* Tests for the unquoteGitPath function used in SessionSummary.diff().
*
* Git quotes file paths containing non-ASCII bytes using C-style escaping with
* octal sequences (e.g., \303\251 for UTF-8 "é"). This function decodes those
* paths back to their original Unicode representation. Without correct decoding,
* session diffs show garbled filenames for non-ASCII files (CJK, accented, emoji).
*
* We test indirectly via SessionSummary.diff() which applies unquoteGitPath to
* stored FileDiff entries.
*/
const projectRoot = path.join(__dirname, "../..")
Log.init({ print: false })
// Helper: write fake diffs to Storage for a session, then read them back via diff()
async function roundtrip(files: string[]): Promise<string[]> {
const sessionID = Identifier.ascending("session") as any
const diffs = files.map((file) => ({
file,
before: "",
after: "",
additions: 1,
deletions: 0,
status: "added" as const,
}))
await Storage.write(["session_diff", sessionID], diffs)
const result = await SessionSummary.diff({ sessionID })
return result.map((d) => d.file)
}
describe("SessionSummary.diff: unquoteGitPath decoding", () => {
test("plain ASCII paths pass through unchanged", async () => {
await Instance.provide({
directory: projectRoot,
fn: async () => {
const files = await roundtrip([
"src/index.ts",
"README.md",
"packages/opencode/test/file.test.ts",
])
expect(files).toEqual([
"src/index.ts",
"README.md",
"packages/opencode/test/file.test.ts",
])
},
})
})
test("git-quoted path with octal-encoded UTF-8 (2-byte: é = \\303\\251)", async () => {
await Instance.provide({
directory: projectRoot,
fn: async () => {
// Git quotes "café.txt" as "caf\\303\\251.txt"
const files = await roundtrip(['"caf\\303\\251.txt"'])
expect(files).toEqual(["café.txt"])
},
})
})
test("git-quoted path with 3-byte UTF-8 octal (CJK character 中 = \\344\\270\\255)", async () => {
await Instance.provide({
directory: projectRoot,
fn: async () => {
// Git quotes "中文.txt" as "\\344\\270\\255\\346\\226\\207.txt"
const files = await roundtrip(['"\\344\\270\\255\\346\\226\\207.txt"'])
expect(files).toEqual(["中文.txt"])
},
})
})
test("git-quoted path with standard escape sequences (\\n, \\t, \\\\, \\\")", async () => {
await Instance.provide({
directory: projectRoot,
fn: async () => {
const files = await roundtrip([
'"path\\\\with\\\\backslashes"',
'"file\\twith\\ttabs"',
'"line\\nbreak"',
])
expect(files).toEqual([
"path\\with\\backslashes",
"file\twith\ttabs",
"line\nbreak",
])
},
})
})
test("mixed octal and plain ASCII in one path", async () => {
await Instance.provide({
directory: projectRoot,
fn: async () => {
// "docs/résumé.md" → git quotes accented chars only
// é = \303\251 in UTF-8
const files = await roundtrip(['"docs/r\\303\\251sum\\303\\251.md"'])
expect(files).toEqual(["docs/résumé.md"])
},
})
})
test("unquoted path (no surrounding double quotes) passes through unchanged", async () => {
await Instance.provide({
directory: projectRoot,
fn: async () => {
// If git doesn't quote the path, it should pass through as-is
const files = await roundtrip(["normal/path.ts", "another-file.js"])
expect(files).toEqual(["normal/path.ts", "another-file.js"])
},
})
})
test("path with embedded double quote (\\\")", async () => {
await Instance.provide({
directory: projectRoot,
fn: async () => {
const files = await roundtrip(['"file\\"name.txt"'])
expect(files).toEqual(['file"name.txt'])
},
})
})
test("empty string passes through unchanged", async () => {
await Instance.provide({
directory: projectRoot,
fn: async () => {
const files = await roundtrip([""])
expect(files).toEqual([""])
},
})
})
test("Japanese filename with 3-byte UTF-8 sequences (テスト = \\343\\203\\206\\343\\202\\271\\343\\203\\210)", async () => {
await Instance.provide({
directory: projectRoot,
fn: async () => {
// テ = E3 83 86 = \343\203\206
// ス = E3 82 B9 = \343\202\271
// ト = E3 83 88 = \343\203\210
const files = await roundtrip(['"\\343\\203\\206\\343\\202\\271\\343\\203\\210.sql"'])
expect(files).toEqual(["テスト.sql"])
},
})
})
test("multiple files: some quoted, some not", async () => {
await Instance.provide({
directory: projectRoot,
fn: async () => {
const files = await roundtrip([
"plain.ts",
'"caf\\303\\251.txt"',
"normal/path.js",
'"\\344\\270\\255.md"',
])
expect(files).toEqual([
"plain.ts",
"café.txt",
"normal/path.js",
"中.md",
])
},
})
})
})