Review verdict FAIL raised 7 defects; this addresses 1-5 and 7. 6 is
accepted as-is (see below).
1. Retention failure after a COMPLETED archive no longer throws: a failed
prune is logged loudly with the exact path and otherwise ignored, so a
successful send can never be reported as "The screenshot could not be
saved". The visible symptom of a failed prune is simply more than 10
sessions listed.
2. handleDidShareItems now refreshes the current session AND the
past-submissions list from the store on both the success and the
failure path (previously the menu count and any open window went stale
after every send). Any refresh problem is appended to the status line.
3. "Start new session" and send() now guard against each other
synchronously before their first await (isStartingNewSession flag on
both sides), instead of relying on the SwiftUI .disabled state alone —
a queued Task could otherwise rename the session directory out from
under an in-flight PDF compose.
4. Contract decision, documented in code: session retention prunes the
whole session folder INCLUDING removed/ parked captures. That is the
requested retention policy ("retain the past 10 sessions"), and the
D-11 never-delete note on remove() now says so explicitly.
5. Session ordering tie-break chain extended: manifest createdAt, then the
folder creation date, then the folder content-modification date, then
the id — so retention stays on real creation order even on a volume
that reports no creation date.
7. The PDF button's help text now distinguishes never-sent from
sent-but-no-longer-on-disk (the button was already disabled in both).
6 (empty close leaves the old empty spool/ folder behind) is left exactly
as the pinned SpoolStoreTests contract has it (startNewSession test 16
asserts the folder remains); it is pre-existing behavior, harmless (the
store reconciles and picks the newest candidate), and out of scope here.
Tests: 2 new app-target regression tests (send refreshes past
submissions; start/send refuse to overlap) — 134 tests pass.
209 lines
8.4 KiB
Swift
209 lines
8.4 KiB
Swift
import AppKit
|
|
import CoreGraphics
|
|
import Foundation
|
|
import ImageIO
|
|
import Testing
|
|
import ShotdeckCore
|
|
@testable import Shotdeck
|
|
|
|
/// MMDB-2925, through the REAL app-target seams the menu rows invoke (`AppModel`
|
|
/// actions + `SpoolStore`): "Start new session" keeps the current screenshots as a
|
|
/// past session, the Past submissions state reflects what was sent, and session
|
|
/// retention keeps the newest 10 sessions in the one archive folder. `bootstrap()`
|
|
/// itself is covered by LaunchWiringTests; here `addCapture` refreshes the session
|
|
/// from the store exactly the way `AppModel.captureNow()` does after a real capture.
|
|
@MainActor
|
|
@Test("Menu actions: start new session keeps captures, lists past submissions, retains 10")
|
|
func startNewSessionKeepsCapturesListsPastSubmissionsAndRetainsTen() async throws {
|
|
let fm = FileManager.default
|
|
let root = fm.temporaryDirectory
|
|
.appendingPathComponent("shotdeck-session-workflow-\(UUID().uuidString)", isDirectory: true)
|
|
defer { try? fm.removeItem(at: root) }
|
|
let model = AppDelegate.makeLaunchModel(appSupportRoot: root)
|
|
|
|
// Two captures in the open session, then the menu's "Start new session".
|
|
let c1 = try await addCapture(to: model, red: 1, green: 0, blue: 0)
|
|
let c2 = try await addCapture(to: model, red: 0, green: 1, blue: 0)
|
|
let closedID = model.session.id
|
|
#expect(model.canStartNewSession)
|
|
|
|
await model.startNewSession()
|
|
|
|
// The fresh session is active and the captures are kept as a past session.
|
|
#expect(model.session.id != closedID)
|
|
#expect(model.session.isEmpty)
|
|
#expect(!model.canStartNewSession)
|
|
#expect(model.pastSessions.map(\.id) == [closedID])
|
|
#expect(model.pastSessions.first?.captures.map(\.id) == [c1.id, c2.id])
|
|
#expect(model.pastSessions.first?.pdfFileName == nil) // never sent
|
|
#expect(model.statusLine?.contains("kept as a past session") == true)
|
|
|
|
// Nothing was deleted on close: both PNGs live in the single archive folder.
|
|
let archiveDir = model.paths.archiveDirectory(closedID)
|
|
for capture in [c1, c2] {
|
|
#expect(fm.fileExists(atPath: archiveDir.appendingPathComponent(capture.fileName).path))
|
|
}
|
|
|
|
// "PDF" on a never-sent row: visible missing state, no silent substitute.
|
|
#expect(model.sentPDFURL(for: model.pastSessions[0]) == nil)
|
|
model.revealSentPDF(model.pastSessions[0])
|
|
#expect(model.statusLine == "That session was never sent.")
|
|
}
|
|
|
|
@MainActor
|
|
@Test("Retention: 12 sessions through the menu action leave the newest 10 on disk")
|
|
func twelveSessionsLeaveTheNewestTenInTheOneArchiveFolder() async throws {
|
|
let fm = FileManager.default
|
|
let root = fm.temporaryDirectory
|
|
.appendingPathComponent("shotdeck-session-workflow-\(UUID().uuidString)", isDirectory: true)
|
|
defer { try? fm.removeItem(at: root) }
|
|
let model = AppDelegate.makeLaunchModel(appSupportRoot: root)
|
|
|
|
var closedIDs: [UUID] = []
|
|
for _ in 0..<12 {
|
|
_ = try await addCapture(to: model, red: 0.2, green: 0.2, blue: 0.2)
|
|
closedIDs.append(model.session.id)
|
|
await model.startNewSession()
|
|
}
|
|
|
|
#expect(model.pastSessions.count == SpoolStore.defaultRetainedSessionCount)
|
|
#expect(model.pastSessions.map(\.id)
|
|
== Array(closedIDs.suffix(SpoolStore.defaultRetainedSessionCount).reversed()))
|
|
|
|
let entries = try fm.contentsOfDirectory(at: model.paths.archive, includingPropertiesForKeys: nil)
|
|
let sessionDirs = entries.filter { UUID(uuidString: $0.lastPathComponent) != nil }
|
|
#expect(sessionDirs.count == SpoolStore.defaultRetainedSessionCount)
|
|
for id in closedIDs.prefix(2) {
|
|
#expect(!fm.fileExists(atPath: model.paths.archiveDirectory(id).path))
|
|
}
|
|
}
|
|
|
|
@MainActor
|
|
@Test("Menu action on an empty session reports it started without inventing a past session")
|
|
func startNewSessionOnEmptySessionCreatesNoPastSession() async throws {
|
|
let fm = FileManager.default
|
|
let root = fm.temporaryDirectory
|
|
.appendingPathComponent("shotdeck-session-workflow-\(UUID().uuidString)", isDirectory: true)
|
|
defer { try? fm.removeItem(at: root) }
|
|
let model = AppDelegate.makeLaunchModel(appSupportRoot: root)
|
|
|
|
#expect(model.session.isEmpty)
|
|
await model.startNewSession()
|
|
|
|
#expect(model.session.isEmpty)
|
|
#expect(model.pastSessions.isEmpty)
|
|
#expect(model.statusLine == "New session started.")
|
|
}
|
|
|
|
@MainActor
|
|
@Test("Send seam refreshes past submissions after archiving (menu count stays true)")
|
|
func successfulSendRefreshesPastSubmissions() async throws {
|
|
let fm = FileManager.default
|
|
let root = fm.temporaryDirectory
|
|
.appendingPathComponent("shotdeck-session-workflow-\(UUID().uuidString)", isDirectory: true)
|
|
defer { try? fm.removeItem(at: root) }
|
|
let model = AppDelegate.makeLaunchModel(appSupportRoot: root)
|
|
|
|
_ = try await addCapture(to: model, red: 0.4, green: 0.4, blue: 0.4)
|
|
await model.handleDidShareItems(fileName: "Redline-20261011-120000.pdf", pageCount: 1)
|
|
|
|
#expect(model.statusLine == "Sent — 1 page.")
|
|
#expect(model.session.isEmpty)
|
|
#expect(model.pastSessions.count == 1)
|
|
#expect(model.pastSessions.first?.pdfFileName == "Redline-20261011-120000.pdf")
|
|
#expect(model.pastSessions.first?.captures.count == 1)
|
|
}
|
|
|
|
@MainActor
|
|
@Test("Start new session and send refuse to overlap (no mid-flight session close)")
|
|
func sessionStartAndSendRefuseToOverlap() async throws {
|
|
let fm = FileManager.default
|
|
let root = fm.temporaryDirectory
|
|
.appendingPathComponent("shotdeck-session-workflow-\(UUID().uuidString)", isDirectory: true)
|
|
defer { try? fm.removeItem(at: root) }
|
|
let model = AppDelegate.makeLaunchModel(appSupportRoot: root)
|
|
|
|
let capture = try await addCapture(to: model, red: 0.1, green: 0.1, blue: 0.1)
|
|
let before = model.session.id
|
|
|
|
// A send in flight: "Start new session" must refuse instead of renaming the
|
|
// session directory out from under the compose.
|
|
model.setSending(true)
|
|
await model.startNewSession()
|
|
#expect(model.session.id == before)
|
|
#expect(model.session.captures.map(\.id) == [capture.id])
|
|
#expect(model.statusLine == "Finish the current send first.")
|
|
model.setSending(false)
|
|
|
|
// A session start in flight: send must refuse before composing anything.
|
|
model.setStartingNewSession(true)
|
|
await model.send(anchor: nil)
|
|
#expect(model.statusLine == "Finish starting the new session first.")
|
|
#expect(model.session.id == before)
|
|
#expect(model.session.captures.map(\.id) == [capture.id])
|
|
model.setStartingNewSession(false)
|
|
|
|
// Both settled: the normal path still works.
|
|
await model.startNewSession()
|
|
#expect(model.session.id != before)
|
|
#expect(model.pastSessions.count == 1)
|
|
#expect(model.pastSessions.first?.captures.map(\.id) == [capture.id])
|
|
}
|
|
|
|
// MARK: - Fixtures
|
|
|
|
/// Appends one generated PNG through the store and refreshes `model.session` from
|
|
/// it — the same refresh `AppModel.captureNow()` performs after a real capture.
|
|
@MainActor
|
|
private func addCapture(
|
|
to model: AppModel,
|
|
red: CGFloat,
|
|
green: CGFloat,
|
|
blue: CGFloat
|
|
) async throws -> Capture {
|
|
let capture = try await model.spool.append(
|
|
pngData: try makePNGData(width: 8, height: 8, red: red, green: green, blue: blue),
|
|
pixelWidth: 8, pixelHeight: 8, scale: 1.0, capturedAt: Date())
|
|
model.replaceSession(try await model.spool.currentSession())
|
|
return capture
|
|
}
|
|
|
|
private enum FixtureError: Error {
|
|
case pngGenerationFailed
|
|
}
|
|
|
|
private func makePNGData(
|
|
width: Int,
|
|
height: Int,
|
|
red: CGFloat,
|
|
green: CGFloat,
|
|
blue: CGFloat
|
|
) throws -> Data {
|
|
let colorSpace = CGColorSpaceCreateDeviceRGB()
|
|
guard let context = CGContext(
|
|
data: nil,
|
|
width: width,
|
|
height: height,
|
|
bitsPerComponent: 8,
|
|
bytesPerRow: width * 4,
|
|
space: colorSpace,
|
|
bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue
|
|
) else {
|
|
throw FixtureError.pngGenerationFailed
|
|
}
|
|
context.setFillColor(red: red, green: green, blue: blue, alpha: 1)
|
|
context.fill(CGRect(x: 0, y: 0, width: width, height: height))
|
|
guard let image = context.makeImage() else {
|
|
throw FixtureError.pngGenerationFailed
|
|
}
|
|
let buffer = NSMutableData()
|
|
guard let destination = CGImageDestinationCreateWithData(buffer, "public.png" as CFString, 1, nil) else {
|
|
throw FixtureError.pngGenerationFailed
|
|
}
|
|
CGImageDestinationAddImage(destination, image, nil)
|
|
guard CGImageDestinationFinalize(destination) else {
|
|
throw FixtureError.pngGenerationFailed
|
|
}
|
|
return buffer as Data
|
|
}
|