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
import Foundation
import XCTest
@testable import OldFella
final class ProfileEditTests: XCTestCase {
private let did = "did:plc:reader"
private let linkCollection = "app.bsky.actor.link"
private let profileCollection = "app.bsky.actor.profile"
func testSaveStagesEditedLinkAndPublishesOrderedReferencesWithProfileFields() async throws {
let fixture = try SecureSessionUploadFixture()
defer { fixture.cleanUp() }
var writes: [[String: Any]] = []
StubURLProtocol.handler = { request in
guard let url = request.url else { throw ServiceError.invalidURL }
switch url.lastPathComponent {
case "com.atproto.repo.getRecord": return try self.recordResponse(url)
case "com.atproto.repo.putRecord":
let data = try XCTUnwrap(StubURLProtocol.recordedBody(for: url))
let body = try XCTUnwrap(JSONSerialization.jsonObject(with: data) as? [String: Any])
writes.append(body)
let collection = try XCTUnwrap(body["collection"] as? String)
let key = try XCTUnwrap(body["rkey"] as? String)
return try self.json(["uri": "at://\(self.did)/\(collection)/\(key)", "cid": "new-\(key)"])
default: return try fixture.response(request)
}
}
_ = try await fixture.service.signIn(
handle: "reader.bsky.social", appPassword: "dedicated-app-password", authFactorToken: nil)
let original = try await fixture.service.loadProfileEdit(accountID: did)
XCTAssertEqual(original.draft.links.map(\.title), ["First", "Second"])
var changes = original.draft
changes.displayName = "New name"
changes.bio = "New bio"
changes.avatar = .replace(Data([1, 2, 3]))
let first = changes.links[0]
changes.links = [changes.links[1], .init(id: first.id, title: "Renamed", address: first.address)]
let saved = try await fixture.service.saveProfileEdit(changes, original: original, accountID: did)
XCTAssertEqual(saved.account.displayName, "New name")
XCTAssertTrue(saved.account.avatarURL?.absoluteString.contains("cid=bafk-image") == true)
XCTAssertEqual(writes.count, 2)
let linkWrite = try XCTUnwrap(writes.first)
let linkKey = try XCTUnwrap(linkWrite["rkey"] as? String)
XCTAssertEqual(linkKey.count, 13)
let editedLink = try XCTUnwrap(linkWrite["record"] as? [String: Any])
XCTAssertEqual(editedLink["title"] as? String, "Renamed")
XCTAssertNotNil(editedLink["icon"])
let profileWrite = try XCTUnwrap(writes.last)
XCTAssertEqual(profileWrite["swapRecord"] as? String, "profile-cid")
let profile = try XCTUnwrap(profileWrite["record"] as? [String: Any])
XCTAssertEqual(profile["displayName"] as? String, "New name")
XCTAssertEqual(profile["description"] as? String, "New bio")
XCTAssertEqual(profile["pronouns"] as? String, "they/them")
XCTAssertEqual(profile["futureField"] as? [String: Int], ["version": 2])
XCTAssertEqual((profile["avatar"] as? [String: Any])?["$type"] as? String, "blob")
let refs = try XCTUnwrap(profile["links"] as? [[String: String]])
XCTAssertEqual(refs.map { $0["cid"] }, ["second-cid", "new-\(linkKey)"])
}
func testChangedProfileRejectsSaveBeforeWriting() async throws {
let fixture = try SecureSessionUploadFixture()
defer { fixture.cleanUp() }
StubURLProtocol.clearRequests()
StubURLProtocol.handler = { request in
if let url = request.url, url.lastPathComponent == "com.atproto.repo.getRecord" {
return try self.recordResponse(url)
}
return try fixture.response(request)
}
_ = try await fixture.service.signIn(
handle: "reader.bsky.social", appPassword: "dedicated-app-password", authFactorToken: nil)
let current = try await fixture.service.loadProfileEdit(accountID: did)
let stale = EditableProfile(accountID: did, recordCID: "older", avatarURL: current.avatarURL, draft: current.draft)
var draft = current.draft
draft.links.removeFirst()
do {
_ = try await fixture.service.saveProfileEdit(draft, original: stale, accountID: did)
XCTFail("Expected a profile conflict")
} catch ProfileEditError.changedOnServer {}
XCTAssertFalse(StubURLProtocol.recordedRequests.contains { $0.url?.lastPathComponent == "com.atproto.repo.putRecord" })
}
func testLinkValidationKeepsUnchangedLegacyLinks() {
let legacy = ProfileEdit.Link(title: "Legacy", address: "http://example.com")
let original = ProfileEdit(displayName: "", bio: "", links: [legacy])
var draft = original
draft.links.insert(.init(title: "", address: "https://example.org"), at: 0)
XCTAssertNil(draft.validationError(comparedTo: original))
draft.links[0].address = "https://example.org/a b"
XCTAssertNotNil(draft.validationError(comparedTo: original))
}
private func recordResponse(_ url: URL) throws -> (Int, Data) {
let components = URLComponents(url: url, resolvingAgainstBaseURL: false)
let key = components?.queryItems?.first { $0.name == "rkey" }?.value ?? ""
if key == "self" {
return try json([
"uri": "at://\(did)/\(profileCollection)/self", "cid": "profile-cid",
"value": [
"$type": profileCollection, "displayName": "Reader", "description": "Old bio",
"futureField": ["version": 2], "pronouns": "they/them",
"links": ["first", "second"].map { ["uri": "at://\(did)/\(linkCollection)/\($0)", "cid": "\($0)-cid"] }
]
])
}
return try json([
"uri": "at://\(did)/\(linkCollection)/\(key)", "cid": "\(key)-cid",
"value": [
"$type": linkCollection, "url": "https://\(key).example", "title": key.capitalized,
"createdAt": "2026-10-01T00:00:00Z", "icon": ["ref": ["$link": "test-icon"]]
]
])
}
private func json(_ body: [String: Any]) throws -> (Int, Data) {
(200, try JSONSerialization.data(withJSONObject: body))
}
}