Skip to content

Commit 6597bae

Browse files
authored
Merge pull request #93 from zero-ide/feature/git-model-codingkeys
refactor: exclude ui-only git ids from codable payloads
2 parents e92722f + 47d355b commit 6597bae

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

Sources/Zero/Services/GitService.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ struct GitFileChange: Codable, Identifiable {
1515
let id = UUID()
1616
let path: String
1717
let changeType: ChangeType
18+
19+
enum CodingKeys: String, CodingKey {
20+
case path
21+
case changeType
22+
}
1823

1924
enum ChangeType: String, Codable {
2025
case added = "A"
@@ -43,13 +48,27 @@ struct GitBranch: Codable, Identifiable {
4348
let isRemote: Bool
4449
let commitHash: String?
4550
let commitMessage: String?
51+
52+
enum CodingKeys: String, CodingKey {
53+
case name
54+
case isCurrent
55+
case isRemote
56+
case commitHash
57+
case commitMessage
58+
}
4659
}
4760

4861
struct GitStash: Codable, Identifiable {
4962
let id = UUID()
5063
let index: Int
5164
let hash: String
5265
let message: String
66+
67+
enum CodingKeys: String, CodingKey {
68+
case index
69+
case hash
70+
case message
71+
}
5372
}
5473

5574
// MARK: - Git Service

Tests/ZeroTests/GitServiceTests.swift

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,4 +259,61 @@ final class GitServiceTests: XCTestCase {
259259
// Then
260260
XCTAssertEqual(mockRunner.executedScript, "cd /workspace && git commit -m 'feat: it'\"'\"'s done'")
261261
}
262+
263+
func testGitFileChangeEncodingExcludesUIOnlyID() throws {
264+
// Given
265+
let model = GitFileChange(path: "Sources/Zero/main.swift", changeType: .modified)
266+
267+
// When
268+
let encoded = try JSONEncoder().encode(model)
269+
let payload = try jsonObject(from: encoded)
270+
271+
// Then
272+
XCTAssertNil(payload["id"])
273+
XCTAssertEqual(payload["path"] as? String, "Sources/Zero/main.swift")
274+
XCTAssertEqual(payload["changeType"] as? String, "M")
275+
}
276+
277+
func testGitBranchEncodingExcludesUIOnlyID() throws {
278+
// Given
279+
let model = GitBranch(
280+
name: "main",
281+
isCurrent: true,
282+
isRemote: false,
283+
commitHash: "abc123",
284+
commitMessage: "initial"
285+
)
286+
287+
// When
288+
let encoded = try JSONEncoder().encode(model)
289+
let payload = try jsonObject(from: encoded)
290+
291+
// Then
292+
XCTAssertNil(payload["id"])
293+
XCTAssertEqual(payload["name"] as? String, "main")
294+
XCTAssertEqual(payload["isCurrent"] as? Bool, true)
295+
}
296+
297+
func testGitStashEncodingExcludesUIOnlyID() throws {
298+
// Given
299+
let model = GitStash(index: 0, hash: "def456", message: "WIP")
300+
301+
// When
302+
let encoded = try JSONEncoder().encode(model)
303+
let payload = try jsonObject(from: encoded)
304+
305+
// Then
306+
XCTAssertNil(payload["id"])
307+
XCTAssertEqual(payload["index"] as? Int, 0)
308+
XCTAssertEqual(payload["hash"] as? String, "def456")
309+
}
310+
311+
private func jsonObject(from data: Data) throws -> [String: Any] {
312+
let object = try JSONSerialization.jsonObject(with: data)
313+
guard let dictionary = object as? [String: Any] else {
314+
XCTFail("Expected dictionary JSON object")
315+
return [:]
316+
}
317+
return dictionary
318+
}
262319
}

0 commit comments

Comments
 (0)