Docs native S2a: a refused rename never strands the title field
The field latched 'done' before committing, so when commitRename() refused (a title over 1,024 bytes, metadata not saved on the device, write access lost mid-rename) no later focus loss committed, and the field could stay open, even for someone who had become a reader (Fable on #169, finding 2). DocsRenameSession holds the rules: Return commits, and a refusal keeps the field with the error shown for another try; focus loss commits, and a refusal puts the title back; Esc puts it back. Only an accepted commit or a revert finishes it. A reader never gets the field (showsField), and write access ending mid-rename reverts it. The field has an accessibility label, Rename document (finding 10). DocsRenameTests covers each path and the model's two testable refusals; the latch-first mutation turns the refused-Return test red.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+144
−15
native/apple/Sources/DocsKit/DocsDocumentHeader.swift+46 −11native/apple/Sources/DocsKit/DocsWorkspace.swift+8 −3native/apple/Tests/DocsKitTests/DocsRenameTests.swift+89 −0native/apple/Tests/DocsSnapshots/WindowSnapshots.swift+1 −1
diff --git a/native/apple/Sources/DocsKit/DocsDocumentHeader.swift b/native/apple/Sources/DocsKit/DocsDocumentHeader.swift| index 9274b4e..5f5a8e3 100644 |
| --- a/native/apple/Sources/DocsKit/DocsDocumentHeader.swift |
| +++ b/native/apple/Sources/DocsKit/DocsDocumentHeader.swift |
| @@ -23,7 +23,8 @@ enum DocsInspectorState { |
| let team: String? |
| @Binding var renaming: Bool |
| let beginRename: () -> Void |
| - let finishRename: () -> Void |
| + /// Commits the rename; false when the model refused it. |
| + let finishRename: () -> Bool |
| var body: some View { |
| // spacing-allow: the subtitle sits on the title's descender, as a |
| @@ -38,11 +39,14 @@ enum DocsInspectorState { |
| .accessibilityIdentifier("docs.document.header") |
| } |
| + /// A reader never gets an editable field, whatever `renaming` says. |
| + static func showsField(renaming: Bool, access: DocsAccess) -> Bool { renaming && access == .writable } |
| + |
| /// Esc: the title as it was. |
| private func cancelRename() { model.renameInput = nil; renaming = false } |
| @ViewBuilder private var title: some View { |
| - if renaming { |
| + if Self.showsField(renaming: renaming, access: model.documentAccess) { |
| // An AppKit field: SwiftUI's focus does not reach into a toolbar |
| // item's hosting view, so a TextField there appears without the |
| // keyboard and never sees Esc. This one takes focus as it |
| @@ -51,6 +55,8 @@ enum DocsInspectorState { |
| placeholder: String(localized: "docs.list.create.placeholder", bundle: DocsResources.bundle), |
| changed: { model.renameInput = $0 }, commit: finishRename, cancel: cancelRename) |
| .frame(minWidth: 180, maxWidth: 480) |
| + // Write access ending mid-rename puts the title back. |
| + .onChange(of: model.documentAccess) { _, access in if access != .writable { cancelRename() } } |
| } else { |
| let canWrite = model.detail?.can_write == true |
| Text(verbatim: model.detail?.title ?? " ") |
| @@ -236,6 +242,38 @@ struct DocsPillStyle: ButtonStyle { |
| } |
| #endif |
| +/// One rename's commit rules (Fable on #169, finding 2). It is finished |
| +/// only by a commit the model accepts, or by putting the title back: |
| +/// - Return: commit; refused, the field stays with the error shown, and a |
| +/// later Return or focus loss tries again. |
| +/// - Focus loss: commit; refused, the title goes back as it was (the error |
| +/// stays shown), so no field is left open behind the person's back. |
| +/// - Esc: the title as it was. |
| +@MainActor final class DocsRenameSession { |
| + enum Outcome: Equatable { case committed, keepEditing, reverted, finished } |
| + private let commit: () -> Bool |
| + private let cancel: () -> Void |
| + private(set) var isFinished = false |
| + init(commit: @escaping () -> Bool, cancel: @escaping () -> Void) { self.commit = commit; self.cancel = cancel } |
| + func returnPressed() -> Outcome { |
| + guard !isFinished else { return .finished } |
| + if commit() { isFinished = true; return .committed } |
| + return .keepEditing |
| + } |
| + func focusLost() -> Outcome { |
| + guard !isFinished else { return .finished } |
| + isFinished = true |
| + if commit() { return .committed } |
| + cancel() |
| + return .reverted |
| + } |
| + func escape() { |
| + guard !isFinished else { return } |
| + isFinished = true |
| + cancel() |
| + } |
| +} |
| + |
| #if os(macOS) |
| /// The title's rename field (spec §1, "Rename in place"): Return or focus |
| /// loss commits through `model.commitRename()`, Esc reverts. |
| @@ -243,7 +281,7 @@ struct DocsRenameField: NSViewRepresentable { |
| let text: String |
| let placeholder: String |
| let changed: (String) -> Void |
| - let commit: () -> Void |
| + let commit: () -> Bool |
| let cancel: () -> Void |
| func makeCoordinator() -> Coordinator { Coordinator(self) } |
| func makeNSView(context: Context) -> NSTextField { |
| @@ -257,26 +295,23 @@ struct DocsRenameField: NSViewRepresentable { |
| field.cell?.isScrollable = true |
| field.delegate = context.coordinator |
| field.setAccessibilityIdentifier("docs.document.title") |
| + field.setAccessibilityLabel(String(localized: "docs.document.rename", bundle: DocsResources.bundle)) |
| return field |
| } |
| func updateNSView(_ field: NSTextField, context: Context) { context.coordinator.parent = self } |
| @MainActor final class Coordinator: NSObject, NSTextFieldDelegate { |
| var parent: DocsRenameField |
| - private var done = false |
| + private lazy var session = DocsRenameSession(commit: { [unowned self] in self.parent.commit() }, cancel: { [unowned self] in self.parent.cancel() }) |
| init(_ parent: DocsRenameField) { self.parent = parent } |
| func controlTextDidChange(_ note: Notification) { |
| if let field = note.object as? NSTextField { parent.changed(field.stringValue) } |
| } |
| func control(_ control: NSControl, textView: NSTextView, doCommandBy selector: Selector) -> Bool { |
| - if selector == #selector(NSResponder.cancelOperation(_:)) { done = true; parent.cancel(); return true } |
| - if selector == #selector(NSResponder.insertNewline(_:)) { parent.commit(); return true } |
| + if selector == #selector(NSResponder.cancelOperation(_:)) { session.escape(); return true } |
| + if selector == #selector(NSResponder.insertNewline(_:)) { _ = session.returnPressed(); return true } |
| return false |
| } |
| - func controlTextDidEndEditing(_ note: Notification) { |
| - guard !done else { return } |
| - done = true |
| - parent.commit() |
| - } |
| + func controlTextDidEndEditing(_ note: Notification) { _ = session.focusLost() } |
| } |
| /// Takes the keyboard as soon as it is in a window, with the title selected. |
| final class FocusingField: NSTextField { |
diff --git a/native/apple/Sources/DocsKit/DocsWorkspace.swift b/native/apple/Sources/DocsKit/DocsWorkspace.swift| index 57bd9e9..f0f2c06 100644 |
| --- a/native/apple/Sources/DocsKit/DocsWorkspace.swift |
| +++ b/native/apple/Sources/DocsKit/DocsWorkspace.swift |
| @@ -200,12 +200,17 @@ import SwiftUI |
| #endif |
| } |
| private func beginRename() { |
| + // Only a writer gets an editable title (spec §1, rename). |
| + guard model.documentAccess == .writable else { return } |
| model.renameInput = model.detail?.title ?? "" |
| renaming = true; titleFocused = true |
| } |
| - private func finishRename() { |
| - guard renaming else { return } |
| - if model.commitRename() { renaming = false } |
| + /// Commits the title; false when the model refused it (too long, not |
| + /// saved on the device, no longer writable), and the field stays open. |
| + @discardableResult private func finishRename() -> Bool { |
| + guard renaming else { return true } |
| + if model.commitRename() { renaming = false; return true } |
| + return false |
| } |
| #if os(iOS) |
| private var documentTitle: some View { |
diff --git a/native/apple/Tests/DocsKitTests/DocsRenameTests.swift b/native/apple/Tests/DocsKitTests/DocsRenameTests.swift| new file mode 100644 |
| index 0000000..4ac8df7 |
| --- /dev/null |
| +++ b/native/apple/Tests/DocsKitTests/DocsRenameTests.swift |
| @@ -0,0 +1,89 @@ |
| +import Foundation |
| +import RastrilloApple |
| +import RastrilloNative |
| +import RastrilloTestSupport |
| +import XCTest |
| +@testable import DocsKit |
| + |
| +/// Rename in place (spec §1; Fable on #169, finding 2): a refused commit |
| +/// never leaves a field that later focus losses cannot commit, and a reader |
| +/// never gets an editable title. |
| +@MainActor final class DocsRenameTests: XCTestCase { |
| + func testAnAcceptedReturnFinishesTheRename() { |
| + var commits = 0, cancels = 0 |
| + let session = DocsRenameSession(commit: { commits += 1; return true }, cancel: { cancels += 1 }) |
| + XCTAssertEqual(session.returnPressed(), .committed) |
| + XCTAssertEqual(session.focusLost(), .finished, "the field going away afterwards commits nothing more") |
| + XCTAssertEqual(commits, 1); XCTAssertEqual(cancels, 0) |
| + } |
| + |
| + /// Refused on Return: still editing, and the next Return or focus loss |
| + /// tries again rather than being swallowed. |
| + func testARefusedReturnKeepsEditingAndTheNextAttemptCommits() { |
| + var accept = false, commits = 0 |
| + let session = DocsRenameSession(commit: { commits += 1; return accept }, cancel: {}) |
| + XCTAssertEqual(session.returnPressed(), .keepEditing) |
| + XCTAssertFalse(session.isFinished) |
| + accept = true |
| + XCTAssertEqual(session.focusLost(), .committed, "a later focus loss commits") |
| + XCTAssertEqual(commits, 2) |
| + } |
| + |
| + /// Refused on focus loss: the title goes back as it was. |
| + func testARefusedFocusLossReverts() { |
| + var cancels = 0 |
| + let session = DocsRenameSession(commit: { false }, cancel: { cancels += 1 }) |
| + XCTAssertEqual(session.returnPressed(), .keepEditing) |
| + XCTAssertEqual(session.focusLost(), .reverted) |
| + XCTAssertEqual(cancels, 1) |
| + XCTAssertTrue(session.isFinished) |
| + } |
| + |
| + func testEscapePutsTheTitleBack() { |
| + var commits = 0, cancels = 0 |
| + let session = DocsRenameSession(commit: { commits += 1; return true }, cancel: { cancels += 1 }) |
| + session.escape() |
| + XCTAssertEqual(session.focusLost(), .finished) |
| + XCTAssertEqual(commits, 0); XCTAssertEqual(cancels, 1) |
| + } |
| + |
| + func testAReaderNeverGetsAnEditableTitle() { |
| + XCTAssertTrue(DocsTitleArea.showsField(renaming: true, access: .writable)) |
| + XCTAssertFalse(DocsTitleArea.showsField(renaming: true, access: .readOnly)) |
| + XCTAssertFalse(DocsTitleArea.showsField(renaming: true, access: .unavailable)) |
| + XCTAssertFalse(DocsTitleArea.showsField(renaming: false, access: .writable)) |
| + } |
| + |
| + // The model's refusals, each of which the session above keeps open or reverts. |
| + |
| + private func open() async throws -> (DocsModel, AccessServer) { |
| + let server = AccessServer() |
| + try await server.inner.seed([.object(["type": .string("paragraph"), "children": .array([ |
| + .object(["type": .string("text"), "runs": .array([.object(["insert": .string("Hello"), "marks": .object([:])])])])])])]) |
| + let connectivity = NativeConnectivity(source: ScriptedPathSource(.satisfied)) |
| + let account = DocsOfflineAccount(store: EphemeralDocsStore(), accountID: UUID().uuidString, connectivity: connectivity, transport: { server }) |
| + let model = DocsModel(transport: { server }, connectivity: connectivity, offlineAccount: account) |
| + await model.open("doc") |
| + return (model, server) |
| + } |
| + |
| + func testATitleOverTheLimitIsRefusedWithTheErrorShown() async throws { |
| + let (model, _) = try await open() |
| + model.renameInput = String(repeating: "é", count: 600) // 1,200 bytes |
| + XCTAssertFalse(model.commitRename()) |
| + XCTAssertEqual(model.errorKey, "docs.error.generic", "the callout says so") |
| + XCTAssertNotNil(model.renameInput, "the typed title is kept for another try") |
| + await model.stop() |
| + } |
| + |
| + func testARenameAfterWriteAccessEndsIsRefused() async throws { |
| + let (model, server) = try await open() |
| + model.renameInput = "New title" |
| + await server.answer(200, canWrite: false) |
| + await model.revalidateAccess() |
| + XCTAssertEqual(model.documentAccess, .readOnly) |
| + XCTAssertFalse(model.commitRename()) |
| + XCTAssertNotEqual(model.detail?.title, "New title") |
| + await model.stop() |
| + } |
| +} |
diff --git a/native/apple/Tests/DocsSnapshots/WindowSnapshots.swift b/native/apple/Tests/DocsSnapshots/WindowSnapshots.swift| index 094b3c7..b2b3a84 100644 |
| --- a/native/apple/Tests/DocsSnapshots/WindowSnapshots.swift |
| +++ b/native/apple/Tests/DocsSnapshots/WindowSnapshots.swift |
| @@ -90,7 +90,7 @@ final class DocsWindowSnapshots: XCTestCase { |
| HStack(spacing: 8) { |
| // Where the window's close, minimise and zoom buttons sit. |
| Color.clear.frame(width: 78) |
| - DocsTitleArea(model: model, team: "Tito", renaming: $renaming, beginRename: {}, finishRename: {}) |
| + DocsTitleArea(model: model, team: "Tito", renaming: $renaming, beginRename: {}, finishRename: { true }) |
| Spacer(minLength: 24) |
| DocsHeaderControls(model: model, inspectorOpen: $open, part: .history, share: {}) |
| DocsHeaderControls(model: model, inspectorOpen: $open, part: .format, share: {}) |