Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Sources/Zero/Services/GitService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ struct GitFileChange: Codable, Identifiable {
let id = UUID()
let path: String
let changeType: ChangeType

enum CodingKeys: String, CodingKey {
case path
case changeType
}

enum ChangeType: String, Codable {
case added = "A"
Expand Down Expand Up @@ -43,13 +48,27 @@ struct GitBranch: Codable, Identifiable {
let isRemote: Bool
let commitHash: String?
let commitMessage: String?

enum CodingKeys: String, CodingKey {
case name
case isCurrent
case isRemote
case commitHash
case commitMessage
}
}

struct GitStash: Codable, Identifiable {
let id = UUID()
let index: Int
let hash: String
let message: String

enum CodingKeys: String, CodingKey {
case index
case hash
case message
}
}

// MARK: - Git Service
Expand Down
57 changes: 57 additions & 0 deletions Tests/ZeroTests/GitServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,61 @@ final class GitServiceTests: XCTestCase {
// Then
XCTAssertEqual(mockRunner.executedScript, "cd /workspace && git commit -m 'feat: it'\"'\"'s done'")
}

func testGitFileChangeEncodingExcludesUIOnlyID() throws {
// Given
let model = GitFileChange(path: "Sources/Zero/main.swift", changeType: .modified)

// When
let encoded = try JSONEncoder().encode(model)
let payload = try jsonObject(from: encoded)

// Then
XCTAssertNil(payload["id"])
XCTAssertEqual(payload["path"] as? String, "Sources/Zero/main.swift")
XCTAssertEqual(payload["changeType"] as? String, "M")
}

func testGitBranchEncodingExcludesUIOnlyID() throws {
// Given
let model = GitBranch(
name: "main",
isCurrent: true,
isRemote: false,
commitHash: "abc123",
commitMessage: "initial"
)

// When
let encoded = try JSONEncoder().encode(model)
let payload = try jsonObject(from: encoded)

// Then
XCTAssertNil(payload["id"])
XCTAssertEqual(payload["name"] as? String, "main")
XCTAssertEqual(payload["isCurrent"] as? Bool, true)
}

func testGitStashEncodingExcludesUIOnlyID() throws {
// Given
let model = GitStash(index: 0, hash: "def456", message: "WIP")

// When
let encoded = try JSONEncoder().encode(model)
let payload = try jsonObject(from: encoded)

// Then
XCTAssertNil(payload["id"])
XCTAssertEqual(payload["index"] as? Int, 0)
XCTAssertEqual(payload["hash"] as? String, "def456")
}

private func jsonObject(from data: Data) throws -> [String: Any] {
let object = try JSONSerialization.jsonObject(with: data)
guard let dictionary = object as? [String: Any] else {
XCTFail("Expected dictionary JSON object")
return [:]
}
return dictionary
}
}