// // SummaryGenerationTests.swift // MuseionTests // // ## Scenario: the AI drafts a first-person summary // From #208 // // Given an open saved piece and an idle one-shot rail // When the writer asks for an AI summary // Then a fresh one-shot engine on the museion profile receives the // piece's Title and body (flushed to disk first) with a prompt that // asks for a first-person teaser, and the reply text comes back to // the caller — nothing is written to disk, the roster is untouched, // and the transcript it ran in is transient. // import Foundation @testable import Museion import Testing @Suite("SummaryGenerationTests") struct SummaryGenerationTests { // MARK: The pure prompt (SummaryPrompt) @Test func `prompt carries the title and body and asks for first person`() { let prompt = SummaryPrompt.prompt(title: "T", body: "B") #expect(prompt.contains("T")) #expect(prompt.contains("B")) #expect(prompt.lowercased().contains("first person")) #expect(prompt.lowercased().contains("entice")) } @Test func `cleaned trims drops a leading label and surrounding quotes`() { #expect(SummaryPrompt.cleaned(" Summary: Why I left. ") == "Why I left.") #expect(SummaryPrompt.cleaned("\"Why I left.\"") == "Why I left.") #expect(SummaryPrompt.cleaned("Line one.\nLine two.") == "Line one. Line two.") #expect(SummaryPrompt.cleaned(" ") == "") // A reply that merely opens with the word keeps it. #expect(SummaryPrompt.cleaned("Summary opens the piece.") == "Summary opens the piece.") // Prose bracketed by quoted words is a teaser, not a wrapper — the // edge quotes are the writer's and stay. let bracketed = "\"Why did you leave?\" I asked. She said \"never.\"" #expect(SummaryPrompt.cleaned(bracketed) == bracketed) // Label and wrapper together. #expect(SummaryPrompt.cleaned("Summary: \"Why I left.\"") == "Why I left.") } // MARK: The model half (PostsModel.summarize over the scripted rig) /// A saved `# Titled` piece opened in the model, waited into the /// library scan (the label and the prompt read the library's title). @MainActor private func openPiece(_ rig: ScriptedRig) async throws -> String { let id = try plantedPiece(rig.home) rig.model.select(id) for _ in 0 ..< 250 where !rig.model.posts.contains(where: { $0.id == id }) { try await Task.sleep(nanoseconds: 20_000_000) } return id } /// How many `museion` (chat-profile) engines spawned — the resident /// plus every one-shot (BlindReadTests' countable). @MainActor private func museionSpawns(_ rig: ScriptedRig) -> Int { rig.engines.spawnedProfiles.filter { $0 == "museion" }.count } @Test @MainActor func `summarize runs a fresh chat-profile engine and returns the cleaned reply`() async throws { let rig = try makeScriptedRig(mode: "summaryOnPrompt") defer { rig.mentor.shutdown() } await rig.mentor.openSession() let rosterBefore = rig.mentor.roster.count _ = try await openPiece(rig) let summary = await rig.model.summarize(mentor: rig.mentor) #expect(summary == "I left the city because the noise never stopped.") // A fresh CHAT-profile engine drafted (the resident is the only // other `museion` spawn; pass profiles never ran). #expect(museionSpawns(rig) == 2) #expect(!rig.engines.spawnedProfiles.contains { $0.contains("museion-pass") }) // The prompt carried the piece's body inline (the engines' prompt log). let asked = rig.engines.engines.flatMap(\.promptLog).map(\.text).joined() #expect(asked.contains("Body.")) // The transient label renders; the roster never grew; nothing was // written — the piece on disk carries no summary line. #expect(textRows(of: rig.mentor).contains("Summary · Titled")) #expect(rig.mentor.roster.count == rosterBefore) let onDisk = try String( contentsOf: rig.home.appendingPathComponent("projects/posts/\(rig.model.selectedPostID!)/post.md"), encoding: .utf8, ) #expect(!onDisk.contains("summary:")) #expect(rig.model.lastError == nil) } @Test @MainActor func `summarize refuses when no piece is open`() async throws { let rig = try makeScriptedRig(mode: "summaryOnPrompt") defer { rig.mentor.shutdown() } await rig.mentor.openSession() let summary = await rig.model.summarize(mentor: rig.mentor) #expect(summary == nil) #expect(rig.model.lastError?.contains("Open a saved post") == true) // Nothing ran beyond the resident engine. #expect(museionSpawns(rig) == 1) } @Test @MainActor func `summarize refuses while the one-shot rail is busy`() async throws { let rig = try makeScriptedRig(mode: "hangOnPrompt") defer { rig.mentor.shutdown() } await rig.mentor.openSession() _ = try await openPiece(rig) let run = Task { await rig.model.runPass(.pass1, mentor: rig.mentor) } for _ in 0 ..< 250 where rig.model.runningPass == nil { try await Task.sleep(nanoseconds: 20_000_000) } #expect(rig.model.runningPass == .pass1) // Exclusivity — the summary refuses while the pass owns the rail. let summary = await rig.model.summarize(mentor: rig.mentor) #expect(summary == nil) #expect(rig.model.lastError?.contains("already running") == true) rig.model.cancelPass(mentor: rig.mentor) // the pass bar's Cancel rail for _ in 0 ..< 250 where rig.model.runningPass != nil { try await Task.sleep(nanoseconds: 20_000_000) } #expect(rig.model.runningPass == nil) rig.mentor.shutdown() // frees the held turn if the rail regressed — bounds the await _ = await run.value } @Test @MainActor func `a cancelled summary turn returns nil and frees the rail`() async throws { // A hung engine must not hold the sheet's spinner forever: the pass // bar's Cancel (cancelPass) ends the summary turn like a Blind Read's. let rig = try makeScriptedRig(mode: "hangOnPrompt") defer { rig.mentor.shutdown() } await rig.mentor.openSession() _ = try await openPiece(rig) let run = Task { await rig.model.summarize(mentor: rig.mentor) } for _ in 0 ..< 250 where !rig.model.summarizing { try await Task.sleep(nanoseconds: 20_000_000) } #expect(rig.model.summarizing) // the flag the pass bar's Cancel keys on // Reverse exclusivity: a Pass refuses while the summary owns the // rail — and the refusal NAMES the summary (the guard, not the // host's engine lock downstream). await rig.model.runPass(.pass1, mentor: rig.mentor) #expect(rig.model.lastError == "A summary is already running.") #expect(rig.model.runningPass == nil) rig.model.cancelPass(mentor: rig.mentor) // the pass bar's Cancel rail let summary = await run.value #expect(summary == nil) for _ in 0 ..< 250 where rig.model.summarizing { try await Task.sleep(nanoseconds: 20_000_000) } #expect(!rig.model.summarizing) #expect(!rig.mentor.isStreaming) // the rail is free again } @Test @MainActor func `a refused or empty turn returns nil`() async throws { let rig = try makeScriptedRig(mode: "refuseOnPrompt") defer { rig.mentor.shutdown() } await rig.mentor.openSession() _ = try await openPiece(rig) let summary = await rig.model.summarize(mentor: rig.mentor) #expect(summary == nil) #expect(rig.model.lastError?.contains("didn't return a summary") == true) } }