Files
museion/MuseionTests/SessionScopeTests.swift
T
AlexKucera 627ecf515e
CI / ci (push) Canceled after 0s
refactor(session): extract the engine host
Engine lifecycle policy — spawn, handshake, respawn-on-crash, teardown,
and the per-surface residency rules (resident chat engine; one-shot
pass engines spawned per run, torn down at the verdict, ADR-0014) —
moves out of MentorSessionModel into EngineHost behind the
EngineTransport seam. The model keeps session + turn coordination and
stays the only observable the panes see; ACPEngineClient becomes the
seam's production adapter, and a scripted in-process engine
(ScriptedEngine) becomes the second adapter — engine-behaviour tests
(streaming, crash, cancel, restart, per-surface spawn) now run without
child processes, retiring the zsh fake-server rigs from logic tests
(#30 flake family). Exactly one real-process wire smoke per LLM
surface survives (chat two-act + parameterized pass1/pass2): the
held-turn cancel contract, hung-child SIGTERM teardown, handshake,
streamed turn, crash, restart.

One-shot teardown is structural (withOneShot closes at body end, every
exit path); the one-shot slot is claimed before the first suspension so
the at-most-one invariant holds across the spawn/handshake awaits.

Suite: 652 executed / 652 passed / 0 failed (653 argument cases),
counted from the xcresult; swiftformat idempotent, swiftlint clean on
touched files.

Closes #178
2026-11-09 18:26:16 +01:00

95 lines
4.2 KiB
Swift

//
// SessionScopeTests.swift
// MuseionTests
//
// Owner ruling (2026-11-07): the Mentor pane hosts the OPEN PIECE's
// session — the pure seam over the roster (exact pieceTitle match,
// newest-first) pinned here: match, no-match, and title-with-no-roster.
// #178: the defer pin runs IN-PROCESS over a ScriptedEngines rig at the
// EngineTransport seam — no child process.
//
import Foundation
@testable import Museion
import Testing
@Suite("SessionScopeTests")
struct SessionScopeTests {
/// The roster is newest-first (the model's own order), so the FIRST
/// exact pieceTitle match is the newest session for the piece. No
/// match — including near-misses, no open piece, or no roster at all —
/// hosts nothing: nil, the pane's honest empty presentation.
@Test func `hosted session id picks newest exact piece title match`() {
let older = SessionEntry(
sessionId: "s-old", name: "Old", pieceTitle: "Essay X",
createdAt: Date(timeIntervalSinceNow: -600),
)
let newer = SessionEntry(sessionId: "s-new", name: "New", pieceTitle: "Essay X", createdAt: Date())
let other = SessionEntry(sessionId: "s-other", name: "Other", pieceTitle: "Draft Y", createdAt: Date())
let roster = [newer, older, other] // newest-first, as the model keeps it
// Match — the newest session for the piece wins; a different piece
// finds its own.
#expect(SessionRoster.hostedSessionId(forOpenPieceTitle: "Essay X", in: roster) == "s-new")
#expect(SessionRoster.hostedSessionId(forOpenPieceTitle: "Draft Y", in: roster) == "s-other")
// No match — EXACT match only; near-misses never host.
#expect(SessionRoster.hostedSessionId(forOpenPieceTitle: "essay x", in: roster) == nil)
#expect(SessionRoster.hostedSessionId(forOpenPieceTitle: "Unwritten", in: roster) == nil)
// No open piece (draft, external guest, nothing) hosts nothing.
#expect(SessionRoster.hostedSessionId(forOpenPieceTitle: nil, in: roster) == nil)
// A title with no roster behind it hosts nothing.
#expect(SessionRoster.hostedSessionId(forOpenPieceTitle: "Essay X", in: []) == nil)
}
/// Finish-audit: the scope rail is an AUTOMATIC action — when the one
/// replay slot is busy (another session's session/load in flight), the
/// switch DEFERS like a stream (old behavior: resumeSession refused and
/// appended a user-visible "Resume refused" row for an action the writer
/// never took). Pinned over the scripted rig's `loadReplayHold` mode
/// parking the replay; teardown closes the parked engine, so the resume
/// task is deliberately never awaited.
@MainActor
@Test func `scope switch defers behind an in-flight replay without refusing`() async throws {
let rosterDir = acpTempDir("scope-defer")
let engines = ScriptedEngines(mode: "loadReplayHold")
let model = engines.makeMentor(rosterDirectory: rosterDir)
defer { model.shutdown() }
await model.openSession() // empty roster — creates the default session, completes
guard case .ready = model.phase else {
Issue.record("expected ready, got \(model.phase)")
return
}
// Two rostered pieces through the real flow; B's session is active.
guard await model.newSession(name: "On A", pieceTitle: "Piece A") != nil,
await model.newSession(name: "On B", pieceTitle: "Piece B") != nil
else {
Issue.record("session creation refused")
return
}
let entryA = try #require(model.roster.first { $0.pieceTitle == "Piece A" })
let activeBefore = model.activeEntry?.sessionId
// Park A's replay behind the hold — the load sits in flight, the
// slot stays taken, and B remains the pane's surface.
_ = Task { await model.resumeSession(entryA) }
var held = false
for _ in 0 ..< 250 where !held {
held = model.stderrTail.contains { $0.contains("hold-load") }
if !held {
try await Task.sleep(nanoseconds: 20_000_000)
}
}
#expect(held)
await model.scopeHostedSession(toOpenPieceTitle: "Piece A")
let refused = model.rows.contains { row in
if case let .text(text, _, _) = row.content {
return text.contains("Resume refused")
}
return false
}
#expect(!refused)
// The switch applied nowhere yet — deferred, not refused and not
// swapped behind the replay.
#expect(model.activeEntry?.sessionId == activeBefore)
}
}