Skip to content
Merged
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
70 changes: 44 additions & 26 deletions Tests/KeystoneTests/Tests/Services/EditorSettingsServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ private class TestableEditorSettingsService: EditorSettingsService {
}

override func api(for blog: Blog) -> WordPressComRestApi? {
return mockApi
mockApi
}

override var apiForDefaultAccount: WordPressComRestApi? {
return mockApi
mockApi
}
}

Expand Down Expand Up @@ -98,8 +98,13 @@ class EditorSettingsServiceTest: CoreDataTestCase {
let blogs = addBlogsToAccount(count: numberOfBlogs)
let response = bulkResponse(with: .gutenberg, count: numberOfBlogs)

service.migrateGlobalSettingToRemote(isGutenbergEnabled: true)
remoteApi.successBlockPassedIn?(response as AnyObject, HTTPURLResponse())
blogs.forEach { $0.mobileEditor = .aztec }
contextManager.saveContextAndWait(mainContext)

waitForEditorUpdate(to: .gutenberg, blogs: blogs) {
service.migrateGlobalSettingToRemote(isGutenbergEnabled: true)
remoteApi.successBlockPassedIn?(response as AnyObject, HTTPURLResponse())
}
Comment on lines +104 to +107

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finding from Claude:

This call site uses the migrateGlobalSetting(to:response:) helper suggested at line 139.

Not blocking: apply this together with the other two suggestions in one batch ("Add suggestion to batch"). Each one depends on the others, so applying only some of them breaks the build.

Suggested change
waitForEditorUpdate(to: .gutenberg, blogs: blogs) {
service.migrateGlobalSettingToRemote(isGutenbergEnabled: true)
remoteApi.successBlockPassedIn?(response as AnyObject, HTTPURLResponse())
}
migrateGlobalSetting(to: .gutenberg, response: response)


blogs.forEach {
XCTAssertTrue($0.isGutenbergEnabled)
Expand All @@ -118,38 +123,47 @@ class EditorSettingsServiceTest: CoreDataTestCase {

contextManager.saveContextAndWait(mainContext)

let editorsUpdated = expectation(description: "Editors updated to Aztec")
waitForEditorUpdate(to: .aztec, blogs: blogs) {
service.migrateGlobalSettingToRemote(isGutenbergEnabled: false)
remoteApi.successBlockPassedIn?(response as AnyObject, HTTPURLResponse())
}
Comment on lines +126 to +129

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finding from Claude:

This call site uses the migrateGlobalSetting(to:response:) helper suggested at line 139.

Not blocking: apply this together with the other two suggestions in one batch ("Add suggestion to batch"). Each one depends on the others, so applying only some of them breaks the build.

Suggested change
waitForEditorUpdate(to: .aztec, blogs: blogs) {
service.migrateGlobalSettingToRemote(isGutenbergEnabled: false)
remoteApi.successBlockPassedIn?(response as AnyObject, HTTPURLResponse())
}
migrateGlobalSetting(to: .aztec, response: response)


blogs.forEach { blog in
XCTAssertFalse(blog.isGutenbergEnabled)
XCTAssertEqual(blog.editor, .aztec)
}
}
}

extension EditorSettingsServiceTest {
private func waitForEditorUpdate(to editor: MobileEditor, blogs: [Blog], update: () -> Void) {
let editorsUpdated = expectation(description: "Editors updated to \(editor.rawValue.capitalized)")
var didFulfill = false
let observer = NotificationCenter.default.addObserver(
forName: .NSManagedObjectContextObjectsDidChange,
object: mainContext,
queue: nil
) { _ in
if blogs.allSatisfy({ $0.editor == .aztec && !$0.isGutenbergEnabled }) {
editorsUpdated.fulfill()
guard !didFulfill, blogs.allSatisfy({ $0.editor == editor }) else {
return
}
didFulfill = true
editorsUpdated.fulfill()
}
defer { NotificationCenter.default.removeObserver(observer) }

service.migrateGlobalSettingToRemote(isGutenbergEnabled: false)
remoteApi.successBlockPassedIn?(response as AnyObject, HTTPURLResponse())

update()
wait(for: [editorsUpdated], timeout: 2)
NotificationCenter.default.removeObserver(observer)
blogs.forEach { blog in
XCTAssertFalse(blog.isGutenbergEnabled)
XCTAssertEqual(blog.editor, .aztec)
}
}
Comment on lines +139 to 157

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finding from Claude:

migrateGlobalSettingToRemote already takes an onSuccess closure (EditorSettingsService.swift:66). It runs on the main thread after the save, and the sites are already updated by then. So the notification observer and the didFulfill guard aren't needed.

Checked locally with all three suggestions applied:

  • 200 runs of EditorSettingsServiceTest with every CPU core busy: 0 failures.
  • Service changed to ignore the server response: both tests still fail. They fail at the assertions in about 0.04 s, without waiting out the 2 s timeout.
  • swift-format leaves the file unchanged, and SwiftLint reports nothing.

Not blocking: apply this together with the other two suggestions in one batch ("Add suggestion to batch"). Each one depends on the others, so applying only some of them breaks the build.

Suggested change
private func waitForEditorUpdate(to editor: MobileEditor, blogs: [Blog], update: () -> Void) {
let editorsUpdated = expectation(description: "Editors updated to \(editor.rawValue.capitalized)")
var didFulfill = false
let observer = NotificationCenter.default.addObserver(
forName: .NSManagedObjectContextObjectsDidChange,
object: mainContext,
queue: nil
) { _ in
if blogs.allSatisfy({ $0.editor == .aztec && !$0.isGutenbergEnabled }) {
editorsUpdated.fulfill()
guard !didFulfill, blogs.allSatisfy({ $0.editor == editor }) else {
return
}
didFulfill = true
editorsUpdated.fulfill()
}
defer { NotificationCenter.default.removeObserver(observer) }
service.migrateGlobalSettingToRemote(isGutenbergEnabled: false)
remoteApi.successBlockPassedIn?(response as AnyObject, HTTPURLResponse())
update()
wait(for: [editorsUpdated], timeout: 2)
NotificationCenter.default.removeObserver(observer)
blogs.forEach { blog in
XCTAssertFalse(blog.isGutenbergEnabled)
XCTAssertEqual(blog.editor, .aztec)
}
}
private func migrateGlobalSetting(to editor: MobileEditor, response: [String: String]) {
let migrated = expectation(description: "Editors updated to \(editor.rawValue.capitalized)")
service.migrateGlobalSettingToRemote(isGutenbergEnabled: editor == .gutenberg) {
migrated.fulfill()
}
remoteApi.successBlockPassedIn?(response as AnyObject, HTTPURLResponse())
wait(for: [migrated], timeout: 2)
}

}

extension EditorSettingsServiceTest {
func addBlogsToAccount(count: Int) -> [Blog] {
let blogs = makeTestBlogs(count: count)
account.addBlogs(Set(blogs))
return blogs
}

func makeTestBlogs(count: Int) -> [Blog] {
return (1...count).map(makeTestBlog)
(1...count).map(makeTestBlog)
}

func makeTestBlog(withID id: Int = 1) -> Blog {
Expand All @@ -160,25 +174,29 @@ extension EditorSettingsServiceTest {
}

func bulkResponse(with editor: MobileEditor, count: Int) -> [String: String] {
return (1...count).reduce(into: [String: String]()) {
$0["\($1)"] = editor.rawValue
}
(1...count)
.reduce(into: [String: String]()) {
$0["\($1)"] = editor.rawValue
}
}

func responseWith(mobileEditor: String) -> AnyObject {
return [
[
"editor_mobile": mobileEditor,
"editor_web": "classic",
"editor_web": "classic"
] as AnyObject
}

func sync(with blog: Blog) {
let expec = expectation(description: "success")
expec.assertForOverFulfill = true

service.syncEditorSettings(for: blog, success: {
expec.fulfill()
}) { error in
service.syncEditorSettings(
for: blog,
success: {
expec.fulfill()
}
) { error in
XCTFail("This call should succeed. Error: \(error)")
expec.fulfill()
}
Expand Down