new features
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import XCTest
|
||||
import AppKit
|
||||
import CoreText
|
||||
import PDFKit
|
||||
import SQLite3
|
||||
@testable import AIStudio
|
||||
|
||||
@@ -672,6 +674,108 @@ final class StdioMCPTests: XCTestCase {
|
||||
}
|
||||
|
||||
final class DesktopRuntimeTests: XCTestCase {
|
||||
func testScreenshotPreviewAndImageRequests() throws {
|
||||
let file = FileManager.default.temporaryDirectory.appending(path: "\(UUID()).png")
|
||||
defer { try? FileManager.default.removeItem(at: file) }
|
||||
let bitmap = try XCTUnwrap(CGContext(data: nil, width: 640, height: 480,
|
||||
bitsPerComponent: 8, bytesPerRow: 0, space: CGColorSpaceCreateDeviceRGB(),
|
||||
bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue))
|
||||
bitmap.setFillColor(CGColor.white)
|
||||
bitmap.fill(CGRect(x: 0, y: 0, width: 640, height: 480))
|
||||
bitmap.textPosition = CGPoint(x: 40, y: 400)
|
||||
CTLineDraw(CTLineCreateWithAttributedString(NSAttributedString(string: "Screenshot attachment", attributes: [
|
||||
.font: NSFont.systemFont(ofSize: 32), .foregroundColor: NSColor.black
|
||||
])), bitmap)
|
||||
let image = try XCTUnwrap(bitmap.makeImage())
|
||||
let original = try XCTUnwrap(NSBitmapImageRep(cgImage: image).representation(using: .png, properties: [:]))
|
||||
try original.write(to: file)
|
||||
|
||||
let preview = try ChatFilePreview.read(file)
|
||||
let png = try XCTUnwrap(preview.imagePNG)
|
||||
let previewImage = try XCTUnwrap(NSBitmapImageRep(data: png))
|
||||
XCTAssertEqual(previewImage.pixelsWide, 640)
|
||||
XCTAssertEqual(previewImage.pixelsHigh, 480)
|
||||
XCTAssertTrue(preview.text.contains("Screenshot attachment"))
|
||||
|
||||
let messages = [
|
||||
Message(role: .system, content: "Answer clearly."),
|
||||
Message(role: .user, content: "Describe this screenshot.", imagePNG: png)
|
||||
]
|
||||
let prepared = try StudioPromptRequest.prepare(messages, template: nil, options: .init(), parameters: .init())
|
||||
let request = InferenceRequest(model: "test", messages: prepared.messages, thinking: .off, parameters: prepared.parameters)
|
||||
func object(_ value: some Encodable) throws -> [String: Any] {
|
||||
try XCTUnwrap(JSONSerialization.jsonObject(with: JSONEncoder().encode(value)) as? [String: Any])
|
||||
}
|
||||
|
||||
let ollama = try XCTUnwrap(object(Ollama.ChatBody(request))["messages"] as? [[String: Any]])
|
||||
XCTAssertNil(ollama[0]["images"])
|
||||
XCTAssertEqual(ollama[1]["images"] as? [String], [png.base64EncodedString()])
|
||||
|
||||
let compatible = try XCTUnwrap(object(CompatibleEndpoint.Body(request))["messages"] as? [[String: Any]])
|
||||
XCTAssertEqual(compatible[0]["content"] as? String, "Answer clearly.")
|
||||
let parts = try XCTUnwrap(compatible[1]["content"] as? [[String: Any]])
|
||||
let url = "data:image/png;base64," + png.base64EncodedString()
|
||||
XCTAssertEqual(parts[0]["text"] as? String, "Describe this screenshot.")
|
||||
XCTAssertEqual((parts[1]["image_url"] as? [String: String])?["url"], url)
|
||||
|
||||
let plugin = try XCTUnwrap(object(CompatibleEndpoint.PluginChatBody(request, plugins: ["mcp/test"]))["input"] as? [[String: Any]])
|
||||
XCTAssertEqual(plugin[0]["content"] as? String, "Describe this screenshot.")
|
||||
XCTAssertEqual(plugin[1]["data_url"] as? String, url)
|
||||
}
|
||||
|
||||
func testPDFAttachmentKeepsLatePagesAndRecognizesScans() throws {
|
||||
let file = FileManager.default.temporaryDirectory.appending(path: "\(UUID()).PDF")
|
||||
defer { try? FileManager.default.removeItem(at: file) }
|
||||
var bounds = CGRect(x: 0, y: 0, width: 612, height: 792)
|
||||
let pdf = try XCTUnwrap(CGContext(file as CFURL, mediaBox: &bounds, nil))
|
||||
|
||||
func draw(_ text: String, in context: CGContext, at position: CGPoint) {
|
||||
let line = CTLineCreateWithAttributedString(NSAttributedString(string: text, attributes: [
|
||||
.font: NSFont.systemFont(ofSize: 24), .foregroundColor: NSColor.black
|
||||
]))
|
||||
context.textPosition = position
|
||||
CTLineDraw(line, context)
|
||||
}
|
||||
|
||||
for _ in 0..<10 {
|
||||
pdf.beginPDFPage(nil)
|
||||
for row in 0..<35 {
|
||||
draw("Embedded document text", in: pdf, at: CGPoint(x: 24, y: 760 - row * 20))
|
||||
}
|
||||
pdf.endPDFPage()
|
||||
}
|
||||
let scan = try XCTUnwrap(CGContext(data: nil, width: 612, height: 792,
|
||||
bitsPerComponent: 8, bytesPerRow: 0, space: CGColorSpaceCreateDeviceRGB(),
|
||||
bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue))
|
||||
scan.setFillColor(CGColor.white)
|
||||
scan.fill(bounds)
|
||||
draw("Final page reference", in: scan, at: CGPoint(x: 24, y: 700))
|
||||
pdf.beginPDFPage(nil)
|
||||
pdf.draw(try XCTUnwrap(scan.makeImage()), in: bounds)
|
||||
pdf.endPDFPage()
|
||||
pdf.closePDF()
|
||||
|
||||
let document = try XCTUnwrap(PDFDocument(url: file))
|
||||
XCTAssertEqual(document.page(at: 10)?.string?.trimmingCharacters(in: .whitespacesAndNewlines) ?? "", "")
|
||||
let preview = try ChatFilePreview.read(file)
|
||||
XCTAssertTrue(preview.isPDF)
|
||||
XCTAssertGreaterThan(preview.text.count, 8000)
|
||||
XCTAssertTrue(preview.text.contains("Page 11\nFinal page reference"))
|
||||
XCTAssertFalse(preview.text.contains("[Preview truncated]"))
|
||||
|
||||
let attachment = DocumentCitation(number: 1, title: preview.name, excerpt: preview.text,
|
||||
documentID: nil, chunkID: nil, requiresFullText: true)
|
||||
let user = Message(role: .user, content: "Read the PDF", documentCitations: [attachment])
|
||||
var options = AgentPromptSettings()
|
||||
options.useDocuments = false
|
||||
let request = try StudioPromptRequest.prepare([user], template: nil, options: options, parameters: .init())
|
||||
XCTAssertEqual(request.citations, [attachment])
|
||||
XCTAssertTrue(request.messages.last?.content.hasSuffix(preview.text) == true)
|
||||
XCTAssertThrowsError(try PromptBudget.fit([user], limit: 512, reserve: 128)) { error in
|
||||
XCTAssertTrue(error.localizedDescription.contains("full attachment text"))
|
||||
}
|
||||
}
|
||||
|
||||
func testTextPreviewRespectsUTF8BoundaryAndRejectsBinary() throws {
|
||||
let directory = FileManager.default.temporaryDirectory.appending(path: UUID().uuidString)
|
||||
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
|
||||
|
||||
Reference in New Issue
Block a user