Files
oai-swift/oAITests/MCPServicePDFTests.swift
rune 76b58e6fdc Rename app from oAI to Confab
"oAI" reads as easily confused with OpenAI, both visually and in
casual conversation. Renamed to "Confab" throughout: Xcode
target/scheme/bundle ID (com.oai.Confab), Info.plist and Help Book
identity, all user-facing UI text, internal Log subsystem and color
identifiers, localization catalogs (6 languages, including a proper
reworded/retranslated Intel-deprecation notice), Help Book HTML
content, and docs (README/DEVELOPMENT/PRIVACY/SECURITY).

Deliberately cosmetic-only: the on-disk data folder
(~/Library/Application Support/oAI/), database/backup filenames,
Keychain service identifiers, and EncryptionService's key-derivation
inputs are all left untouched so existing conversations, settings,
and stored API keys survive the update with zero migration and no
re-entering credentials. Verified live: a real signed build
successfully decrypted a stored API key and loaded an existing
conversation database after the bundle ID change.

Also includes a small already-completed, previously uncommitted
model-release-date feature (ModelInfo/OpenRouterModels/
OpenRouterProvider/ModelInfoView) that happened to share several
files with this rename.

Gitignored on this branch and updated on disk but not part of this
commit: CLAUDE.md, RELEASE_NOTES.md, and the build*.sh scripts.
2026-08-02 14:58:14 +02:00

69 lines
2.5 KiB
Swift

//
// MCPServicePDFTests.swift
// oAITests
//
// SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
// Copyright (C) 2026 Rune Olsen
import Testing
import Foundation
import CoreGraphics
import AppKit
@testable import Confab
@Suite("MCPService PDF text extraction")
struct MCPServicePDFTests {
/// Builds a real, well-formed single-page PDF (via CoreGraphics) containing the given text,
/// writes it to a temp file, and returns the path. Using CoreGraphics rather than hand-rolled
/// PDF bytes avoids flakiness from malformed xref tables etc.
private func makeTestPDF(text: String) throws -> String {
let path = FileManager.default.temporaryDirectory
.appendingPathComponent(UUID().uuidString)
.appendingPathExtension("pdf")
let pdfData = NSMutableData()
let consumer = CGDataConsumer(data: pdfData as CFMutableData)!
var mediaBox = CGRect(x: 0, y: 0, width: 200, height: 200)
let context = CGContext(consumer: consumer, mediaBox: &mediaBox, nil)!
context.beginPDFPage(nil)
let attributedString = NSAttributedString(string: text, attributes: [.font: NSFont.systemFont(ofSize: 24)])
let line = CTLineCreateWithAttributedString(attributedString)
context.textPosition = CGPoint(x: 10, y: 100)
CTLineDraw(line, context)
context.endPDFPage()
context.closePDF()
try (pdfData as Data).write(to: path)
return path.path
}
@Test("Extracts text from a real PDF's text layer")
func extractsTextFromRealPDF() throws {
let path = try makeTestPDF(text: "Hello World")
defer { try? FileManager.default.removeItem(atPath: path) }
let extracted = MCPService.shared.extractPDFText(atPath: path)
#expect(extracted?.contains("Hello World") == true)
}
@Test("Returns nil for a nonexistent path")
func returnsNilForMissingFile() {
let extracted = MCPService.shared.extractPDFText(atPath: "/tmp/definitely-does-not-exist-\(UUID().uuidString).pdf")
#expect(extracted == nil)
}
@Test("Returns nil for a file that isn't a valid PDF")
func returnsNilForGarbageBytes() throws {
let path = FileManager.default.temporaryDirectory
.appendingPathComponent(UUID().uuidString)
.appendingPathExtension("pdf")
try Data("not a real pdf".utf8).write(to: path)
defer { try? FileManager.default.removeItem(at: path) }
let extracted = MCPService.shared.extractPDFText(atPath: path.path)
#expect(extracted == nil)
}
}