Files
splinter/tests/test_theme.c
T
lotherk 614988aa68 junction plugin, light/dark themes, goal completion
- plugins/junction: evaluates the X-Junction-Mcp header and /status; the
  sidebar widget, the "junction" status segment, junction_status and
  /junction show uptime, MCP-server health, queue and request counts.
  The url/header come from the plugin block, the mcp junction server or
  SPLINTER_JUNCTION_URL/TOKEN; the key is never printed.
- theme: two flat themes. light is a white page with black text, dark the
  same inverted; chat, sidebar and input share the page, the status line is
  the one separate surface. Each CDE palette keeps its own rules.
- curses: the sidebar uses the theme's own text on its own background
  (white/black) instead of white on a darkened surface; the one column
  between chat and sidebar is painted, so a white theme has no black seam.
- focus: the goal goes into the first prompt (config, resume, planner), and
  every goal-carrying text asks the model to end with GOAL_COMPLETE. The
  marker is read from the conversation and the session log and recorded as
  goal_state=succeeded in the session meta; /goal and the sidebar say
  [done], reminders stop, a new goal opens a new one.
- tests: test_junction (fake server, header identity, no colon in widget
  continuation lines, no key in the output), test_focus (announcement,
  completion in the compactor and from the tick, persisted state),
  test_theme (flat themes, list order).

make check: all tests passed.
2026-11-17 10:10:47 +00:00

145 lines
5.2 KiB
C

/* splinter -- CDE theme derivation tests. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "theme.h"
#include "palettes.h"
static int failures;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
failures++; \
} \
} while (0)
static int
luma(const unsigned char *c)
{
return 299 * c[0] + 587 * c[1] + 114 * c[2];
}
int
main(void)
{
struct sp_theme t, t2;
struct sp_buf err;
const char **names;
size_t count, i;
if (sp_buf_init(&err) != 0)
return 1;
/* Xresources first, then every palette of the CDE tree, then the flat two */
CHECK(sp_theme_list(&names, &count) == count);
CHECK(count == sp_palette_count + 3);
CHECK(count >= 43);
CHECK(strcmp(names[0], "Xresources") == 0);
for (i = 2; i < count; i++)
CHECK(strcmp(names[i - 1], names[i]) < 0);
CHECK(strcmp(names[count - 2], "dark") == 0);
CHECK(strcmp(names[count - 1], "light") == 0);
/* the default is the X resource theme, and NULL means the default */
CHECK(sp_theme_load(NULL, &t, &err) == 0);
CHECK(strcmp(t.name, SP_THEME_DEFAULT) == 0);
CHECK(strcmp(t.name, "Xresources") == 0);
/* a CDE palette paints with its colour sets: set 5 is the window */
CHECK(sp_theme_load("Default", &t, &err) == 0);
CHECK(strcmp(t.name, "Default") == 0);
CHECK(memcmp(t.bg, "\xc6\xb2\xa8", 3) == 0);
CHECK(memcmp(t.input_bg, "\x68\x6f\x82", 3) == 0); /* set 4 */
CHECK(t.indexed == 0);
/* names are case-blind: "default" is the CDE Default, too */
CHECK(sp_theme_load("default", &t2, &err) == 0);
CHECK(memcmp(&t, &t2, sizeof(t)) == 0);
CHECK(sp_theme_load("Delphinium", &t2, &err) == 0);
/* in every palette the text stands off its surface (CDE's black/white) */
for (i = 1; i < count - 2; i++) {
CHECK(sp_theme_load(names[i], &t, &err) == 0);
CHECK(abs(luma(t.fg) - luma(t.bg)) >= 100000 ||
luma(t.bg) == luma(t.fg));
CHECK(abs(luma(t.input_fg) - luma(t.input_bg)) >= 100000 ||
luma(t.input_bg) == luma(t.input_fg));
/* every role colour stands off the chat surface */
CHECK(abs(luma(t.accent) - luma(t.bg)) >= 30000);
CHECK(abs(luma(t.agent) - luma(t.bg)) >= 32000);
CHECK(abs(luma(t.think) - luma(t.bg)) >= 28000);
CHECK(abs(luma(t.tool) - luma(t.bg)) >= 34000);
CHECK(abs(luma(t.notice) - luma(t.bg)) >= 36000);
}
/*
* The flat themes: the operator asked for the simple thing. The chat
* and the sidebar share one page colour (white/black), the text is the
* opposite, the input keeps the same page, and only the status line is
* allowed to be a different surface.
*/
CHECK(sp_theme_load("light", &t, &err) == 0);
CHECK(strcmp(t.name, SP_THEME_LIGHT) == 0);
CHECK(memcmp(t.bg, "\xff\xff\xff", 3) == 0);
CHECK(memcmp(t.fg, "\x00\x00\x00", 3) == 0);
CHECK(memcmp(t.input_bg, t.bg, 3) == 0);
CHECK(memcmp(t.input_fg, t.fg, 3) == 0);
CHECK(memcmp(t.status, t.bg, 3) != 0);
CHECK(memcmp(t.status_fg, t.fg, 3) == 0);
CHECK(t.indexed == 0);
CHECK(sp_theme_load("Light", &t2, &err) == 0);
CHECK(strcmp(t2.name, SP_THEME_LIGHT) == 0);
CHECK(sp_theme_load("dark", &t, &err) == 0);
CHECK(strcmp(t.name, SP_THEME_DARK) == 0);
CHECK(memcmp(t.bg, "\x00\x00\x00", 3) == 0);
CHECK(memcmp(t.fg, "\xeb\xeb\xeb", 3) == 0);
CHECK(memcmp(t.input_bg, t.bg, 3) == 0);
CHECK(memcmp(t.input_fg, t.fg, 3) == 0);
CHECK(memcmp(t.status, t.bg, 3) != 0);
/* role text is legible on the page in both directions */
CHECK(luma(t.accent) - luma(t.bg) >= 50000);
CHECK(luma(t.agent) - luma(t.bg) >= 50000);
CHECK(luma(t.tool) - luma(t.bg) >= 50000);
CHECK(sp_theme_load("LIGHT", &t2, &err) == 0);
CHECK(memcmp(t2.bg, "\xff\xff\xff", 3) == 0);
/* X resources: the tight binding wins, 48-bit colours are read */
CHECK(sp_theme_from_xrdb("*.background:\t#6e1e7d\n"
"*.foreground:\t#acdcb4\n*.color12:\t#7f6c6c\n"
"*background:\t#B8B86F6F4B4B\n*foreground:\t#FFFFFFFFFFFF\n"
"URxvt.background: #000000\n", &t) == 0);
CHECK(t.indexed == 0);
CHECK(memcmp(t.bg, "\x6e\x1e\x7d", 3) == 0);
CHECK(memcmp(t.fg, "\xac\xdc\xb4", 3) == 0);
CHECK(sp_theme_from_xrdb("*background: #B8B86F6F4B4B\n", &t) == 0);
CHECK(memcmp(t.bg, "\xb8\x6f\x4b", 3) == 0);
CHECK(sp_theme_from_xrdb("*background: rgb:1d/1f/21\n", &t) == 0);
CHECK(memcmp(t.bg, "\x1d\x1f\x21", 3) == 0);
/* no resources: the terminal's own colours, by index */
CHECK(sp_theme_from_xrdb(NULL, &t) == 0);
CHECK(t.indexed == 1);
CHECK(t.idx_agent == 10 && t.idx_think == 11);
CHECK(t.idx_tool == 14 && t.idx_notice == 13);
CHECK(sp_theme_from_xrdb("URxvt.background: #000000\n", &t) == 0);
CHECK(t.indexed == 1);
/* an unknown theme is an error, never a silent fallback */
sp_buf_clear(&err);
CHECK(sp_theme_load("NoSuchPalette", &t, &err) == -1);
CHECK(strstr(err.p, "unknown theme") != NULL);
/* colour mapping stays inside the terminal's palette */
CHECK(sp_color_rgb(0, 0, 0, 256) >= 16);
CHECK(sp_color_rgb(0, 0, 0, 256) <= 255);
CHECK(sp_color_rgb(255, 255, 255, 256) >= 16);
CHECK(sp_color_rgb(255, 255, 255, 256) <= 255);
CHECK(sp_color_rgb(0, 0, 0, 8) >= 0 && sp_color_rgb(0, 0, 0, 8) < 8);
CHECK(sp_color_rgb(255, 255, 255, 16) >= 0);
CHECK(sp_color_rgb(255, 255, 255, 16) < 16);
sp_buf_free(&err);
if (failures == 0)
printf("test_theme: ok (%zu palettes)\n", count);
return failures == 0 ? 0 : 1;
}