import Foundation import Testing @testable import SprachmittlerEngines @Suite("Documented prompts") struct PromptGoldenTests { private func fixture(_ name: String) throws -> String { let base = try #require(Bundle.module.resourceURL) let url = base.appendingPathComponent("Fixtures/\(name)") var text = try String(contentsOf: url, encoding: .utf8) // Fixtures have one final line terminator; the production prompt has none. Remove exactly // that one terminator so an accidental extra newline still fails the comparison. if text.hasSuffix("\n") { text.removeLast() } return text } @Test func defaultPromptMatchesTheGoldenFile() throws { let expected = try fixture("default-prompt.txt") .replacingOccurrences(of: "{target_lang}", with: "German") .replacingOccurrences(of: "{source_text}", with: "Hello.") #expect(HyMT3Prompt.defaultTranslation(target: HyMT3Languages.german, source: "Hello.") == expected) } @Test func delimitersPromptMatchesTheGoldenFile() throws { let expected = try fixture("delimiters-prompt.txt") .replacingOccurrences(of: "{target_lang}", with: "German") .replacingOccurrences(of: "{source_text}", with: "Use {{limit}}.") #expect( HyMT3Prompt.delimiters(target: HyMT3Languages.german, source: "Use {{limit}}.") == expected ) } @Test func structuredData1MatchesTheGoldenFile() throws { let expected = try fixture("structured-data-1-prompt.txt") .replacingOccurrences(of: "{format_type}", with: "JSON") .replacingOccurrences(of: "{target_lang}", with: "German") .replacingOccurrences(of: "{source_text}", with: #"{"name":"value"}"#) #expect( HyMT3Prompt.structuredData1( format: .json, target: HyMT3Languages.german, source: #"{"name":"value"}"# ) == expected ) } @Test func prefixIsStableForTheSameTarget() { let first = HyMT3Prompt.defaultTranslation(target: HyMT3Languages.german, source: "One.") let second = HyMT3Prompt.defaultTranslation(target: HyMT3Languages.german, source: "Two.") let prefix = "Translate the following text into German. Note that you should " + "**only output the translated result without any additional explanation**:\n\n" #expect(first.hasPrefix(prefix)) #expect(second.hasPrefix(prefix)) #expect(first != second) } @Test func variantsResolveToDocumentedTemplates() { #expect( HyMT3Prompt.text(for: .structuredData1(format: .markdown), target: HyMT3Languages.german, source: "x") .contains("### Task") ) #expect( HyMT3Prompt.text(for: .delimiters, target: HyMT3Languages.german, source: "x") .contains("retain the exact same number of delimiters") ) } }