Sessions are now the unit of the product instead of an implicit capture run. - Nothing is deleted by default: the fixed 10-session cap is gone. An age limit (Never / 7 / 14 / 30 / 90 / 180 / 365 days) is chosen in Settings > Sessions and applied on launch, after archiving, and by "Clean up now". Sent PDFs are never touched. - Each session carries review details: lane, deployed version, commit, reviewer (pre-filled with the Mac user) and a free note. They persist in session.json and are inherited by the next session, so a review run is typed once. - PDF file names are Redline-<yyyyMMdd-HHmm>-<LANE>-<SHORTID>.pdf, and every page carries a two-line reference footer (tool, build time, session / lane, deployed build, commit, reviewer, note). The PDF document info carries the same values in Author and Keywords. - Verification: 159 tests, a headless panel self-test, and a LaunchServices-driven end-to-end run that types and clicks the real panel.
359 lines
14 KiB
Swift
359 lines
14 KiB
Swift
import CoreGraphics
|
|
import Foundation
|
|
import ImageIO
|
|
import Testing
|
|
import ShotdeckCore
|
|
|
|
// Session lifecycle + retention: "Start new session" keeps the current screenshots as
|
|
// a past session; every session is kept by default, and an age limit chosen in
|
|
// Settings is the only thing that ever removes one.
|
|
|
|
// MARK: - 1. closeCurrentSession keeps the captures as a past session
|
|
|
|
@Test
|
|
func closeCurrentSessionKeepsCapturesAsAPastSessionAndStartsFresh() async throws {
|
|
let (root, paths) = try makeIsolatedPaths()
|
|
defer { try? FileManager.default.removeItem(at: root) }
|
|
|
|
let store = try SpoolStore(paths: paths)
|
|
let c1 = try await store.append(
|
|
pngData: try makePNGData(width: 8, height: 8, red: 1, green: 0, blue: 0),
|
|
pixelWidth: 8, pixelHeight: 8, scale: 1.0, capturedAt: Date())
|
|
let c2 = try await store.append(
|
|
pngData: try makePNGData(width: 8, height: 8, red: 0, green: 1, blue: 0),
|
|
pixelWidth: 8, pixelHeight: 8, scale: 1.0, capturedAt: Date())
|
|
|
|
let closedID = try await store.currentSession().id
|
|
let archived = try #require(try await store.closeCurrentSession())
|
|
|
|
#expect(archived.id == closedID)
|
|
#expect(archived.state == .archived)
|
|
#expect(archived.pdfFileName == nil) // never sent — shown as "Not sent"
|
|
#expect(archived.captures.map(\.id) == [c1.id, c2.id])
|
|
|
|
// Nothing is deleted: both PNGs live on under archive/<id>/.
|
|
let archiveDir = paths.archiveDirectory(closedID)
|
|
for capture in archived.captures {
|
|
#expect(FileManager.default.fileExists(
|
|
atPath: archiveDir.appendingPathComponent(capture.fileName).path))
|
|
}
|
|
#expect(!FileManager.default.fileExists(atPath: paths.sessionDirectory(closedID).path))
|
|
|
|
let fresh = try await store.currentSession()
|
|
#expect(fresh.id != closedID)
|
|
#expect(fresh.state == .open)
|
|
#expect(fresh.isEmpty)
|
|
|
|
let past = try await store.archivedSessions()
|
|
#expect(past.map(\.id) == [closedID])
|
|
#expect(past.first?.captures.count == 2)
|
|
}
|
|
|
|
// MARK: - 2. closeCurrentSession on an empty open session just starts fresh
|
|
|
|
@Test
|
|
func closeCurrentSessionOnAnEmptyOpenSessionStartsFreshWithoutArchiving() async throws {
|
|
let (root, paths) = try makeIsolatedPaths()
|
|
defer { try? FileManager.default.removeItem(at: root) }
|
|
|
|
let store = try SpoolStore(paths: paths)
|
|
let before = try await store.currentSession().id
|
|
|
|
let archived = try await store.closeCurrentSession()
|
|
#expect(archived == nil)
|
|
|
|
let after = try await store.currentSession()
|
|
#expect(after.id != before)
|
|
#expect(after.isEmpty)
|
|
let past = try await store.archivedSessions()
|
|
#expect(past.isEmpty)
|
|
}
|
|
|
|
// MARK: - 3. sent and unsent sessions are distinguishable in the past list
|
|
|
|
@Test
|
|
func pastSessionsDistinguishSentFromUnsentByPDFFileName() async throws {
|
|
let (root, paths) = try makeIsolatedPaths()
|
|
defer { try? FileManager.default.removeItem(at: root) }
|
|
|
|
let store = try SpoolStore(paths: paths)
|
|
_ = try await store.append(
|
|
pngData: try makePNGData(width: 6, height: 6, red: 1, green: 0, blue: 0),
|
|
pixelWidth: 6, pixelHeight: 6, scale: 1.0, capturedAt: Date())
|
|
let sent = try await store.archiveCurrent(pdfFileName: "Redline-20261011-120000.pdf")
|
|
|
|
_ = try await store.append(
|
|
pngData: try makePNGData(width: 6, height: 6, red: 0, green: 0, blue: 1),
|
|
pixelWidth: 6, pixelHeight: 6, scale: 1.0, capturedAt: Date())
|
|
let closed = try #require(try await store.closeCurrentSession())
|
|
|
|
#expect(sent.pdfFileName == "Redline-20261011-120000.pdf")
|
|
#expect(closed.pdfFileName == nil)
|
|
|
|
let past = try await store.archivedSessions()
|
|
#expect(past.map(\.id) == [closed.id, sent.id]) // newest first
|
|
#expect(past.map(\.pdfFileName) == [nil, "Redline-20261011-120000.pdf"])
|
|
}
|
|
|
|
// MARK: - 4. nothing is deleted by default
|
|
|
|
@Test
|
|
func defaultPolicyKeepsEverySession() async throws {
|
|
let (root, paths) = try makeIsolatedPaths()
|
|
defer { try? FileManager.default.removeItem(at: root) }
|
|
|
|
let store = try SpoolStore(paths: paths)
|
|
#expect(await store.configuredRetention() == .never)
|
|
|
|
var closedIDs: [UUID] = []
|
|
for index in 0..<12 {
|
|
_ = try await store.append(
|
|
pngData: try makePNGData(width: 4, height: 4, red: 0.1, green: 0.1, blue: 0.1),
|
|
pixelWidth: 4, pixelHeight: 4, scale: 1.0,
|
|
capturedAt: Date(timeIntervalSince1970: 1_700_000_000 + Double(index)))
|
|
let closed = try #require(try await store.closeCurrentSession())
|
|
closedIDs.append(closed.id)
|
|
}
|
|
|
|
// All 12 sessions live on in the single archive/ folder; none was pruned.
|
|
let fm = FileManager.default
|
|
let entries = try fm.contentsOfDirectory(at: paths.archive, includingPropertiesForKeys: nil)
|
|
let sessionDirs = entries.filter { UUID(uuidString: $0.lastPathComponent) != nil }
|
|
#expect(sessionDirs.count == closedIDs.count)
|
|
|
|
let kept = try await store.archivedSessions().map(\.id)
|
|
#expect(kept == closedIDs.reversed())
|
|
|
|
for id in closedIDs {
|
|
#expect(fm.fileExists(atPath: paths.archiveDirectory(id).appendingPathComponent("session.json").path))
|
|
}
|
|
|
|
// A prune under the default policy removes nothing at all.
|
|
#expect(try await store.pruneExpiredSessions().isEmpty)
|
|
#expect(try await store.expiredSessionIDs().isEmpty)
|
|
}
|
|
|
|
// MARK: - 5. an age limit removes exactly the expired sessions
|
|
|
|
@Test
|
|
func ageLimitRemovesOnlySessionsOlderThanTheCutoff() async throws {
|
|
let (root, paths) = try makeIsolatedPaths()
|
|
defer { try? FileManager.default.removeItem(at: root) }
|
|
|
|
let now = Date(timeIntervalSince1970: 1_700_000_000) // fixed "now" for the test
|
|
let day: TimeInterval = 86_400
|
|
let oldIDs = try [
|
|
try writeArchivedSession(createdAt: now.addingTimeInterval(-40 * day), in: paths),
|
|
try writeArchivedSession(createdAt: now.addingTimeInterval(-31 * day), in: paths),
|
|
]
|
|
let recentIDs = try [
|
|
try writeArchivedSession(createdAt: now.addingTimeInterval(-29 * day), in: paths),
|
|
try writeArchivedSession(createdAt: now.addingTimeInterval(-1 * day), in: paths),
|
|
]
|
|
|
|
let store = try SpoolStore(paths: paths, retention: .days(30))
|
|
|
|
// Asking what would happen deletes nothing.
|
|
#expect(Set(try await store.expiredSessionIDs(now: now)) == Set(oldIDs))
|
|
#expect(try await store.archivedSessions().count == 4)
|
|
|
|
let removed = try await store.pruneExpiredSessions(now: now)
|
|
#expect(Set(removed) == Set(oldIDs))
|
|
|
|
let fm = FileManager.default
|
|
for id in oldIDs {
|
|
#expect(!fm.fileExists(atPath: paths.archiveDirectory(id).path))
|
|
}
|
|
for id in recentIDs {
|
|
#expect(fm.fileExists(atPath: paths.archiveDirectory(id).appendingPathComponent("session.json").path))
|
|
}
|
|
|
|
// The open session is never a candidate, and a second pass is a no-op.
|
|
#expect(try await store.currentSession().state == .open)
|
|
#expect(try await store.pruneExpiredSessions(now: now).isEmpty)
|
|
}
|
|
|
|
// MARK: - 6. the archive path applies the configured age limit
|
|
|
|
@Test
|
|
func theArchivePathAppliesTheAgeLimit() async throws {
|
|
let (root, paths) = try makeIsolatedPaths()
|
|
defer { try? FileManager.default.removeItem(at: root) }
|
|
|
|
let day: TimeInterval = 86_400
|
|
let expiredID = try writeArchivedSession(
|
|
createdAt: Date().addingTimeInterval(-10 * day), in: paths)
|
|
|
|
let store = try SpoolStore(paths: paths, retention: .days(7))
|
|
_ = try await store.append(
|
|
pngData: try makePNGData(width: 4, height: 4, red: 0.3, green: 0.3, blue: 0.3),
|
|
pixelWidth: 4, pixelHeight: 4, scale: 1.0, capturedAt: Date())
|
|
let archived = try await store.archiveCurrent(pdfFileName: "Redline-20261031-1200-A1B2C3D4.pdf")
|
|
|
|
let kept = try await store.archivedSessions()
|
|
#expect(kept.map(\.id) == [archived.id])
|
|
#expect(!FileManager.default.fileExists(atPath: paths.archiveDirectory(expiredID).path))
|
|
}
|
|
|
|
// MARK: - 7. review context is persisted, survives a relaunch and is inherited
|
|
|
|
@Test
|
|
func reviewContextPersistsAndIsInheritedByTheNextSession() async throws {
|
|
let (root, paths) = try makeIsolatedPaths()
|
|
defer { try? FileManager.default.removeItem(at: root) }
|
|
|
|
let store = try SpoolStore(paths: paths)
|
|
let context = ReviewContext(
|
|
lane: "dev03", deployedVersion: "v1.4.2 (41)", commit: "a1b2c3d4",
|
|
reviewer: "Ben Hippler", note: "checkout flow")
|
|
|
|
let updated = try await store.setContext(context)
|
|
#expect(updated.context == context)
|
|
|
|
// A second store on the same directory is what a relaunch sees.
|
|
let reopened = try SpoolStore(paths: paths)
|
|
#expect(try await reopened.currentSession().context == context)
|
|
|
|
_ = try await store.append(
|
|
pngData: try makePNGData(width: 4, height: 4, red: 0.4, green: 0.4, blue: 0.4),
|
|
pixelWidth: 4, pixelHeight: 4, scale: 1.0, capturedAt: Date())
|
|
let archived = try #require(try await store.closeCurrentSession())
|
|
#expect(archived.context == context)
|
|
|
|
// The fresh session keeps the same review details.
|
|
#expect(try await store.currentSession().context == context)
|
|
}
|
|
|
|
// MARK: - 8. a manifest written before contexts existed still loads
|
|
|
|
@Test
|
|
func legacyManifestWithoutContextDecodesAsEmptyContext() async throws {
|
|
let (root, paths) = try makeIsolatedPaths()
|
|
defer { try? FileManager.default.removeItem(at: root) }
|
|
|
|
let id = UUID()
|
|
let dir = paths.archiveDirectory(id)
|
|
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
|
|
let legacyJSON = """
|
|
{
|
|
"id": "\(id.uuidString)",
|
|
"createdAt": "2026-10-11T12:00:00Z",
|
|
"state": "archived",
|
|
"captures": [],
|
|
"pdfFileName": "Redline-20261011-120000.pdf"
|
|
}
|
|
"""
|
|
try Data(legacyJSON.utf8).write(to: dir.appendingPathComponent("session.json"))
|
|
|
|
let store = try SpoolStore(paths: paths)
|
|
let sessions = try await store.archivedSessions()
|
|
#expect(sessions.map(\.id) == [id])
|
|
#expect(sessions.first?.context == .empty)
|
|
#expect(sessions.first?.pdfFileName == "Redline-20261011-120000.pdf")
|
|
}
|
|
|
|
// MARK: - 7. a stale open session is never returned as current (truth guard)
|
|
|
|
@Test
|
|
func aStaleOpenSessionIsNeverReturnedAsCurrent() async throws {
|
|
let (root, paths) = try makeIsolatedPaths()
|
|
defer { try? FileManager.default.removeItem(at: root) }
|
|
|
|
let store = try SpoolStore(paths: paths)
|
|
_ = try await store.append(
|
|
pngData: try makePNGData(width: 6, height: 6, red: 0.7, green: 0.7, blue: 0.1),
|
|
pixelWidth: 6, pixelHeight: 6, scale: 1.0, capturedAt: Date())
|
|
let archived = try await store.archiveCurrent(pdfFileName: "Redline-20261011-120200.pdf")
|
|
|
|
// Simulate the mid-run failure the truth guard exists for: the archived session
|
|
// moved away and the replacement open session's folder is gone (e.g. the fresh
|
|
// session could not be created / survive). The store must mint a real fresh
|
|
// session — never report the already-archived one as open.
|
|
let fresh = try await store.currentSession()
|
|
try FileManager.default.removeItem(at: paths.sessionDirectory(fresh.id))
|
|
|
|
let recovered = try await store.currentSession()
|
|
#expect(recovered.id != fresh.id)
|
|
#expect(recovered.id != archived.id)
|
|
#expect(recovered.state == .open)
|
|
#expect(recovered.isEmpty)
|
|
let past = try await store.archivedSessions()
|
|
#expect(past.map(\.id) == [archived.id])
|
|
}
|
|
|
|
// MARK: - Fixtures
|
|
|
|
/// Writes an archived session folder with the given creation date, exactly the shape
|
|
/// production writes (a manifest plus one PNG), so retention can be exercised without
|
|
/// waiting days.
|
|
private func writeArchivedSession(createdAt: Date, in paths: AppSupportPaths) throws -> UUID {
|
|
let id = UUID()
|
|
let dir = paths.archiveDirectory(id)
|
|
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
|
|
let pngName = "001-A1B2C3D4.png"
|
|
try makePNGData(width: 4, height: 4, red: 0.5, green: 0.5, blue: 0.5)
|
|
.write(to: dir.appendingPathComponent(pngName))
|
|
let capture = Capture(
|
|
id: UUID(), sequence: 1, fileName: pngName,
|
|
pixelWidth: 4, pixelHeight: 4, scale: 1.0, capturedAt: createdAt)
|
|
let session = CaptureSession(
|
|
id: id, createdAt: createdAt, state: .archived, captures: [capture],
|
|
pdfFileName: "Redline-\(DubaiTime.fileMinuteStamp(createdAt)).pdf")
|
|
let encoder = JSONEncoder()
|
|
encoder.dateEncodingStrategy = .iso8601
|
|
try encoder.encode(session).write(to: dir.appendingPathComponent("session.json"))
|
|
return id
|
|
}
|
|
|
|
private enum FixtureError: Error {
|
|
case pngGenerationFailed
|
|
}
|
|
|
|
private func makeIsolatedPaths() throws -> (root: URL, paths: AppSupportPaths) {
|
|
let root = FileManager.default.temporaryDirectory
|
|
.appendingPathComponent("shotdeck-retention-\(UUID().uuidString)", isDirectory: true)
|
|
let paths = try AppSupportPaths(
|
|
root: root,
|
|
outbox: root.appendingPathComponent("outbox", isDirectory: true),
|
|
watchFolder: root.appendingPathComponent("watch", isDirectory: true)
|
|
)
|
|
return (root, paths)
|
|
}
|
|
|
|
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 data = NSMutableData()
|
|
guard let destination = CGImageDestinationCreateWithData(
|
|
data, "public.png" as CFString, 1, nil
|
|
) else {
|
|
throw FixtureError.pngGenerationFailed
|
|
}
|
|
CGImageDestinationAddImage(destination, image, nil)
|
|
guard CGImageDestinationFinalize(destination) else {
|
|
throw FixtureError.pngGenerationFailed
|
|
}
|
|
return data as Data
|
|
}
|