Files
splinter/tests/test_tgmd.c
T
lotherk cc9747b1a7 model choice sticks, endpoints are probed, connect is tested, A2A takes files
/model writes its choice to <config>.d/90-model.conf, so the next start uses
it; picking main deletes the file and the configuration speaks again. With a
plugin transport nothing is written, because that program picks the model.
/model probe asks every endpoint /v1/models and offers what it really serves
as buttons; the callback carries an index, because a GGUF path does not fit
in Telegram's 64 bytes. The TUI gets the same command: the list, or
/model <id|name>, with the same persistence.

/connect runs one tiny turn against the provider it just saved (30 s) and
says whether endpoint, model and key answer. The message with the buttons
goes out before the wait starts, so the save is visible immediately.

A2A no longer refuses binary parts: file{bytes} and raw with a non-text media
type are written to the session's inbox (0600, 8 MiB cap) and the prompt says
[file <name> at <path> (mime, n bytes)], so a text model can hand the file to
a tool. The client saves what a remote agent sends the same way
(SPLINTER_A2A_INBOX). SubscribeToTask stays refused: the task store has no
live events, and a one-shot replay would pretend to be a subscription.

Markdown gains <https://...> autolinks and images (as links, since Telegram
has no inline image without sendPhoto).
2026-11-10 19:33:38 +00:00

211 lines
5.7 KiB
C

/*
* splinter -- model markdown as Telegram HTML.
*
* The chat used to show "**fett**", "|---|" and "<b>" as characters. These
* cases pin what has to become a tag, what has to stay text (snake_case, a bare
* '<'), and how a long answer is split so that every piece parses on its own.
*/
#include <stdio.h>
#include <string.h>
#include "splinter.h"
#include "tgmd.h"
static int failures;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
failures++; \
} \
} while (0)
static void
check_render(const char *md, const char *want)
{
struct sp_buf b;
if (sp_buf_init(&b) != 0) {
fprintf(stderr, "FAIL init\n");
failures++;
return;
}
if (sp_tgmd_render(md, &b) != 0) {
fprintf(stderr, "FAIL render of:\n%s\n", md);
failures++;
} else if (b.p == NULL || strcmp(b.p, want) != 0) {
fprintf(stderr, "FAIL %s:%d:\n--- in ---\n%s--- want ---\n%s"
"--- got ---\n%s\n", __FILE__, __LINE__, md, want,
b.p != NULL ? b.p : "(null)");
failures++;
}
sp_buf_free(&b);
}
/* tag names opened and closed in 's', in order: 1 when every tag balances */
static int
balanced(const char *s)
{
char stack[32][16];
size_t depth = 0;
size_t i;
for (i = 0; s[i] != '\0'; i++) {
char name[16];
size_t n = 0, j;
if (s[i] != '<')
continue;
j = i + 1;
if (s[j] == '/') {
j++;
while (s[j] != '\0' && s[j] != '>')
name[n++] = s[j++];
name[n] = '\0';
if (depth == 0 || strcmp(stack[depth - 1], name) != 0)
return 0;
depth--;
i = j;
continue;
}
while (s[j] != '\0' && s[j] != '>' && s[j] != ' ')
name[n++] = s[j++];
name[n] = '\0';
if (n == 0)
continue;
if (depth == sizeof(stack) / sizeof(stack[0]))
return 0;
snprintf(stack[depth++], sizeof(stack[0]), "%s", name);
i = j;
}
return depth == 0;
}
/* the text between tags, with the entities the renderer writes undone */
static int
plain_into(struct sp_buf *out, const char *s)
{
size_t i;
for (i = 0; s[i] != '\0'; i++) {
if (s[i] == '<') {
const char *gt = strchr(s + i, '>');
if (gt == NULL)
return -1;
i = (size_t)(gt - s);
continue;
}
if (strncmp(s + i, "&amp;", 5) == 0) {
if (sp_buf_puts(out, "&") != 0)
return -1;
i += 4;
} else if (strncmp(s + i, "&lt;", 4) == 0) {
if (sp_buf_puts(out, "<") != 0)
return -1;
i += 3;
} else if (strncmp(s + i, "&gt;", 4) == 0) {
if (sp_buf_puts(out, ">") != 0)
return -1;
i += 3;
} else if (sp_buf_add(out, s + i, 1) != 0) {
return -1;
}
}
return 0;
}
static void
check_chunks(void)
{
struct sp_buf md, html, piece, all, back;
struct sp_tgmd_cursor cur;
size_t i;
int n, pieces = 0;
CHECK(sp_buf_init(&md) == 0);
CHECK(sp_buf_init(&html) == 0);
CHECK(sp_buf_init(&piece) == 0);
CHECK(sp_buf_init(&all) == 0);
CHECK(sp_buf_init(&back) == 0);
for (i = 0; i < 120; i++)
CHECK(sp_buf_printf(&md,
"**Zeile %zu** mit `code` und einem <tag> & mehr Text, "
"damit die Antwort lang genug fuer mehrere Nachrichten ist.\n",
i) == 0);
CHECK(sp_tgmd_render(md.p, &html) == 0);
CHECK(sp_tgmd_cursor_init(&cur, html.p) == 0);
while ((n = sp_tgmd_next(&cur, 1000, &piece)) > 0) {
pieces++;
CHECK(piece.len > 0);
/* a piece is what Telegram gets: it has to fit and to parse */
CHECK(piece.len < 4096);
CHECK(balanced(piece.p));
CHECK(plain_into(&all, piece.p) == 0);
}
CHECK(n == 0);
CHECK(pieces > 1);
CHECK(plain_into(&back, html.p) == 0);
CHECK(all.p != NULL && back.p != NULL && strcmp(all.p, back.p) == 0);
sp_tgmd_cursor_free(&cur);
sp_buf_free(&md);
sp_buf_free(&html);
sp_buf_free(&piece);
sp_buf_free(&all);
sp_buf_free(&back);
}
int
main(void)
{
/* inline: the shapes a model actually writes */
check_render("**fett**", "<b>fett</b>");
check_render("__fett__", "<b>fett</b>");
check_render("*kursiv*", "<i>kursiv</i>");
check_render("_kursiv_", "<i>kursiv</i>");
check_render("~~weg~~", "<s>weg</s>");
check_render("ein `code` hier", "ein <code>code</code> hier");
check_render("**fett mit *kursiv***",
"<b>fett mit <i>kursiv</i></b>");
check_render("[Titel](https://example.org/a?x=1&y=2)",
"<a href=\"https://example.org/a?x=1&amp;y=2\">Titel</a>");
check_render("<https://example.org/x>",
"<a href=\"https://example.org/x\">https://example.org/x</a>");
check_render("![Bild](https://example.org/b.png)",
"<a href=\"https://example.org/b.png\">Bild</a>");
/* what must stay text */
check_render("snake_case_name", "snake_case_name");
check_render("2 * 3 * 4 = 24", "2 * 3 * 4 = 24");
check_render("a < b & c > d", "a &lt; b &amp; c &gt; d");
check_render("<script>x</script>", "&lt;script&gt;x&lt;/script&gt;");
check_render("[kein link](javascript:alert(1))",
"[kein link](javascript:alert(1))");
check_render("* nicht fett", "\xe2\x80\xa2 nicht fett");
/* blocks */
check_render("## Titel\nText\n", "<b>Titel</b>\nText\n");
check_render("> zitat\n> mehr\n",
"<blockquote>zitat\nmehr\n</blockquote>\n");
check_render("---\n", "\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
"\xe2\x94\x80\n");
check_render("- eins\n- zwei\n",
"\xe2\x80\xa2 eins\n\xe2\x80\xa2 zwei\n");
check_render("```c\nif (a < b)\n\treturn **x**;\n```\n",
"<pre><code class=\"language-c\">if (a &lt; b)\n\treturn **x**;"
"</code></pre>\n");
check_render("```\nplain & simple\n```\n",
"<pre><code>plain &amp; simple</code></pre>\n");
/* a table: columns in <pre>, not a row of pipes in a proportional font */
check_render("| a | b |\n|---|---|\n| 1 | 22 |\n",
"<pre>| a | b |\n|---|----|\n| 1 | 22 |\n</pre>\n");
check_chunks();
if (failures == 0)
printf("test_tgmd: ok\n");
return failures == 0 ? 0 : 1;
}