prompts_test.gleam

 1// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
 2//
 3// SPDX-License-Identifier: AGPL-3.0-or-later
 4
 5import gleam/string
 6import gleeunit/should
 7import prompts
 8
 9// --- system tests ---
10
11pub fn system_returns_prompt_test() {
12  let result = prompts.system()
13  should.be_true(result |> contains("text transformation tool"))
14  should.be_true(result |> contains("<raw>"))
15}
16
17// --- build_user_message tests ---
18
19pub fn build_user_message_with_directions_test() {
20  let result = prompts.build_user_message("hello", "make it loud")
21  should.be_true(result |> contains("<raw>\nhello\n</raw>"))
22  should.be_true(
23    result |> contains("<directions>\nmake it loud\n</directions>"),
24  )
25  should.be_true(result |> contains("Transform the text in <raw>"))
26}
27
28pub fn build_user_message_without_directions_test() {
29  let result = prompts.build_user_message("hello", "")
30  should.be_true(result |> contains("<raw>\nhello\n</raw>"))
31  should.be_false(result |> contains("<directions>"))
32  should.be_true(result |> contains("Transform the text in <raw>"))
33}
34
35pub fn build_user_message_trims_directions_test() {
36  let result = prompts.build_user_message("hello", "   ")
37  should.be_false(result |> contains("<directions>"))
38}
39
40// --- extract_code_block tests ---
41
42pub fn extract_simple_code_block_test() {
43  let input = "```\nhello world\n```"
44  prompts.extract_code_block(input)
45  |> should.equal("hello world")
46}
47
48pub fn extract_code_block_with_language_test() {
49  let input = "```json\n{\"key\": \"value\"}\n```"
50  prompts.extract_code_block(input)
51  |> should.equal("{\"key\": \"value\"}")
52}
53
54pub fn extract_code_block_with_surrounding_text_test() {
55  let input = "Here's the result:\n```\ntransformed\n```\nHope that helps!"
56  prompts.extract_code_block(input)
57  |> should.equal("transformed")
58}
59
60pub fn extract_returns_full_text_when_no_code_block_test() {
61  let input = "Just plain text without any code blocks."
62  prompts.extract_code_block(input)
63  |> should.equal(input)
64}
65
66pub fn extract_uses_first_code_block_test() {
67  let input = "```\nfirst\n```\n\n```\nsecond\n```"
68  prompts.extract_code_block(input)
69  |> should.equal("first")
70}
71
72pub fn extract_handles_empty_code_block_test() {
73  let input = "```\n```"
74  prompts.extract_code_block(input)
75  |> should.equal("")
76}
77
78pub fn extract_handles_unclosed_code_block_test() {
79  let input = "```\nunclosed content here"
80  prompts.extract_code_block(input)
81  |> should.equal(input)
82}
83
84fn contains(haystack: String, needle: String) -> Bool {
85  string.contains(haystack, needle)
86}