Files
museion/MuseionTests/PassRunTests.swift
T
AlexKucera 78f6bb2dba
CI / ci (push) Canceled after 0s
test(mentor): pin runPass guard matrix (#24)
The pass rails landed in 683ddf8 left guards #1-3 and the pass bar's
Cancel rail without direct coverage. Three new tests over the fake ACP
path: a second runPass while one streams refuses "already running" and
cancelPass ends the held turn (state stays First Draft), a pass
mid-mentor-reply refuses before the snapshot lands, and a pass against
a never-opened engine refuses "isn't ready". Every guard in
PostsModel.runPass now has a flip-verified pin (299 tests green).

Finish-audit fanout over the delta: the three whole-tree enumerator
walks became fileExists one-liners at the deterministic path (same
discrimination, suite convention); two Info findings deferred with
landing spots in the session log.

Closes #24
2026-10-20 12:44:56 +02:00

207 lines
8.1 KiB
Swift

//
// PassRunTests.swift
// MuseionTests
//
// #24 (S3.1): the pass orchestration — PostsModel.runPass over the real
// client + fake ACP server (ACPClientTests' fake, PostsModelTests' temp-home
// model). Pre-pass snapshot before the engine sees the text, Pass State
// advancing only on a completed turn, order-free.
//
import Foundation
@testable import Museion
import Testing
/// Throwaway Home + model + mentor over the fake engine (makeModelHome +
/// makeFakeACP, one rig).
@MainActor
// swiftlint:disable:next large_tuple
func makePassRig(mode: String = "full") throws -> (PostsModel, URL, MentorSessionModel) {
let (model, home) = try makeModelHome()
let fake = makeFakeACP(at: acpTempDir("passrun"), mode: mode)
let mentor = MentorSessionModel(binaryPath: fake)
return (model, home, mentor)
}
@MainActor
private func textRows(of model: MentorSessionModel) -> [String] {
model.rows.compactMap {
if case let .text(text) = $0.content {
text
} else {
nil
}
}
}
@Test @MainActor func `runPass snapshots streams and advances pass state`() async throws {
let (model, home, mentor) = try makePassRig()
await mentor.openSession()
let post = try PostStore.writeNew(body: "# Pass Piece\n\nThe body under pass.", home: home)
model.select(post.id, home: home)
// Wait for the bind rescan to surface the piece — the pass label reads
// the library's title for it (the PostsModelTests poll pattern).
for _ in 0 ..< 250 where !model.posts.contains(where: { $0.id == post.id }) {
try await Task.sleep(nanoseconds: 20_000_000)
}
await model.runPass(.pass1, home: home, mentor: mentor)
let piece = home.appendingPathComponent("projects/posts/\(post.id)")
let snapshot = try String(contentsOf: piece.appendingPathComponent("passes/pass-1.md"), encoding: .utf8)
#expect(snapshot.contains("The body under pass."))
let onDisk = try String(contentsOf: piece.appendingPathComponent("post.md"), encoding: .utf8)
#expect(onDisk.contains("pass_state: Pass-1 Done"))
// The label renders as the user row; the wire stub never lands.
#expect(textRows(of: mentor).contains("Pass 1 · \(post.title)"))
#expect(!textRows(of: mentor).contains { $0.contains("Critique the piece") })
#expect(model.runningPass == nil)
#expect(model.lastError == nil)
// The library regroups after the (async) rescan.
for _ in 0 ..< 250 where model.posts.first(where: { $0.id == post.id })?.passState != .pass1Done {
try await Task.sleep(nanoseconds: 20_000_000)
}
#expect(model.posts.first(where: { $0.id == post.id })?.passState == .pass1Done)
mentor.shutdown()
}
@Test @MainActor func `cancelled pass does not advance`() async throws {
let (model, home, mentor) = try makePassRig(mode: "cancelOnPrompt")
await mentor.openSession()
let post = try PostStore.writeNew(body: "# Cancel Me\n\nStill first draft.", home: home)
model.select(post.id, home: home)
await model.runPass(.pass1, home: home, mentor: mentor)
let piece = home.appendingPathComponent("projects/posts/\(post.id)")
// Pre-pass is unconditional — the snapshot landed anyway.
let snapshot = try String(contentsOf: piece.appendingPathComponent("passes/pass-1.md"), encoding: .utf8)
#expect(snapshot.contains("Still first draft."))
let onDisk = try String(contentsOf: piece.appendingPathComponent("post.md"), encoding: .utf8)
#expect(onDisk.contains("pass_state: First Draft"))
#expect(model.runningPass == nil)
mentor.shutdown()
}
@Test @MainActor func `refused pass does not advance`() async throws {
let (model, home, mentor) = try makePassRig(mode: "refuseOnPrompt")
await mentor.openSession()
let post = try PostStore.writeNew(body: "# Refused\n\nAlso first draft.", home: home)
model.select(post.id, home: home)
await model.runPass(.pass1, home: home, mentor: mentor)
let onDisk = try String(
contentsOf: home.appendingPathComponent("projects/posts/\(post.id)/post.md"), encoding: .utf8,
)
#expect(onDisk.contains("pass_state: First Draft"))
#expect(model.runningPass == nil)
mentor.shutdown()
}
@Test @MainActor func `runPass requires an open saved post`() async throws {
let (model, home, mentor) = try makePassRig()
await mentor.openSession()
model.newPost() // draft staged — no folder exists
await model.runPass(.pass1, home: home, mentor: mentor)
#expect(model.lastError?.contains("post") == true)
// Nothing landed on disk — the draft never even materialized a folder.
#expect(!FileManager.default.fileExists(atPath: home.appendingPathComponent("projects/posts").path))
mentor.shutdown()
}
@Test @MainActor func `second pass while one runs refuses and cancel ends it`() async throws {
let (model, home, mentor) = try makePassRig(mode: "hangOnPrompt")
await mentor.openSession()
let post = try PostStore.writeNew(body: "# Held\n\nMid-pass.", home: home)
model.select(post.id, home: home)
let first = Task { await model.runPass(.pass1, home: home, mentor: mentor) }
for _ in 0 ..< 250 where model.runningPass == nil {
try await Task.sleep(nanoseconds: 20_000_000)
}
#expect(model.runningPass == .pass1)
await model.runPass(.pass1, home: home, mentor: mentor) // one pass at a time
#expect(model.lastError?.contains("already running") == true)
model.cancelPass(mentor: mentor) // the pass bar's Cancel rail
mentor.shutdown() // the hung fake ignores cancel; teardown frees the turn
_ = await first.value
#expect(model.runningPass == nil)
let onDisk = try String(
contentsOf: home.appendingPathComponent("projects/posts/\(post.id)/post.md"), encoding: .utf8,
)
#expect(onDisk.contains("pass_state: First Draft"))
}
@Test @MainActor func `pass refuses while the mentor is mid-reply`() async throws {
let (model, home, mentor) = try makePassRig(mode: "hangOnPrompt")
await mentor.openSession()
let post = try PostStore.writeNew(body: "# Busy\n\nMentor chatting.", home: home)
model.select(post.id, home: home)
let chat = Task { await mentor.sendPrompt("just chatting") }
for _ in 0 ..< 250 where !mentor.isStreaming {
try await Task.sleep(nanoseconds: 20_000_000)
}
#expect(mentor.isStreaming)
await model.runPass(.pass1, home: home, mentor: mentor)
#expect(model.lastError?.contains("mid-reply") == true)
// The guard fired before the snapshot — nothing on disk.
#expect(!FileManager.default.fileExists(
atPath: home.appendingPathComponent("projects/posts/\(post.id)/passes").path,
))
mentor.shutdown()
_ = await chat.value
}
@Test @MainActor func `pass refuses when the engine is not ready`() async throws {
let (model, home, mentor) = try makePassRig() // never opened — engine idle
let post = try PostStore.writeNew(body: "# Cold\n\nNo engine yet.", home: home)
model.select(post.id, home: home)
await model.runPass(.pass1, home: home, mentor: mentor)
#expect(model.lastError?.contains("isn't ready") == true)
#expect(!FileManager.default.fileExists(
atPath: home.appendingPathComponent("projects/posts/\(post.id)/passes").path,
))
}
@Test @MainActor func `dirty session flushes before the snapshot`() async throws {
let (model, home, mentor) = try makePassRig()
await mentor.openSession()
let post = try PostStore.writeNew(body: "# Flush First\n\nOriginal.", home: home)
model.select(post.id, home: home)
model.session?.edit("# Flush First\n\nEdited on the way.") // inside the debounce window
await model.runPass(.pass1, home: home, mentor: mentor)
let piece = home.appendingPathComponent("projects/posts/\(post.id)")
let snapshot = try String(contentsOf: piece.appendingPathComponent("passes/pass-1.md"), encoding: .utf8)
#expect(snapshot.contains("Edited on the way."))
let onDisk = try String(contentsOf: piece.appendingPathComponent("post.md"), encoding: .utf8)
#expect(onDisk.contains("Edited on the way."))
mentor.shutdown()
}
@Test @MainActor func `pass runs in any order from first draft`() async throws {
let (model, home, mentor) = try makePassRig()
await mentor.openSession()
let post = try PostStore.writeNew(body: "# Free Order\n\nStraight to two.", home: home)
model.select(post.id, home: home)
await model.runPass(.pass2, home: home, mentor: mentor)
let onDisk = try String(
contentsOf: home.appendingPathComponent("projects/posts/\(post.id)/post.md"), encoding: .utf8,
)
#expect(onDisk.contains("pass_state: Pass-2 Done"))
mentor.shutdown()
}