From 07f5b140922d13519ad7a0d1dad67a9b403be9f1 Mon Sep 17 00:00:00 2001 From: Anders Betts Date: Tue, 22 Sep 2026 12:43:52 +0200 Subject: tui: kundutkast, , explicit Spara och F2-åtgärder MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_tui.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) (limited to 'tests') diff --git a/tests/test_tui.c b/tests/test_tui.c index 84e610e..aa062fc 100644 --- a/tests/test_tui.c +++ b/tests/test_tui.c @@ -1,10 +1,13 @@ /* Unit tests for the TUI widget layer (clients/tui.c). Only the pure parts are exercised here; drawing is smoke-tested over a pty. */ #include +#include #include +#include #include +#include "drafts.h" #include "tui.h" #include "util.h" @@ -712,6 +715,96 @@ static void test_rt_footer(void) CHECK(f[0] == '\0'); } +static void test_actions(void) +{ + char buf[256]; + struct tui_action acts[] = { + { "save", "Spara", KEY_F(9), 1, NULL }, + { "del", "Radera utkast", 0, 0, "inget utkast" }, + { "sec", "Sektion", 0, -1, NULL }, + { "arch", "Arkivera", KEY_F(2), 1, NULL }, + }; + + tui_action_label(&acts[0], buf, sizeof buf); + CHECK(strcmp(buf, "Spara") == 0); + tui_action_label(&acts[1], buf, sizeof buf); + CHECK(strcmp(buf, "Radera utkast (inget utkast)") == 0); + + tui_action_hint(acts, 4, 1, buf, sizeof buf); + CHECK(strstr(buf, "F9 = Spara") != NULL); + CHECK(strstr(buf, "F2 = fler") != NULL); + CHECK(strstr(buf, "Arkivera") == NULL); + + tui_action_hint(acts, 4, 0, buf, sizeof buf); + CHECK(strstr(buf, "F9 = Spara") != NULL); + CHECK(strstr(buf, "F2 = Arkivera F2 = fler") != NULL); + CHECK(strstr(buf, "Radera utkast") == NULL); + + tui_action_hint(NULL, 0, 1, buf, sizeof buf); + CHECK(strcmp(buf, "F2 = fler") == 0); +} + +static void test_drafts(void) +{ + char path[128]; + struct drafts d, r; + + snprintf(path, sizeof path, "/tmp/bokf-drafts-test-%d.json", + (int)getpid()); + unlink(path); + drafts_init_path(&d, path); + CHECK(drafts_count(&d, 1, "customer") == 0); + CHECK(drafts_get(&d, 1, "customer", "ny-1") == NULL); + + drafts_put(&d, 1, "customer", "ny-1", "{\"name\":\"A\"}"); + CHECK(drafts_have(&d, 1, "customer", "ny-1")); + CHECK(drafts_count(&d, 1, "customer") == 1); + CHECK(strcmp(drafts_get(&d, 1, "customer", "ny-1"), + "{\"name\":\"A\"}") == 0); + drafts_put(&d, 1, "customer", "ny-1", "{\"name\":\"B\"}"); + CHECK(strcmp(drafts_get(&d, 1, "customer", "ny-1"), + "{\"name\":\"B\"}") == 0); + drafts_put(&d, 2, "customer", "7", "{\"name\":\"C\"}"); + drafts_put(&d, 1, "employee", "3", "{\"name\":\"D\"}"); + CHECK(drafts_count(&d, 1, "customer") == 1); + CHECK(drafts_count(&d, 2, "customer") == 1); + + drafts_init_path(&r, path); + CHECK(drafts_count(&r, 1, "customer") == 1); + CHECK(strcmp(drafts_get(&r, 1, "customer", "ny-1"), + "{\"name\":\"B\"}") == 0); + CHECK(strcmp(drafts_get(&r, 2, "customer", "7"), + "{\"name\":\"C\"}") == 0); + CHECK(strcmp(drafts_get(&r, 1, "employee", "3"), + "{\"name\":\"D\"}") == 0); + drafts_del(&r, 1, "customer", "ny-1"); + CHECK(drafts_count(&r, 1, "customer") == 0); + drafts_free(&r); + + drafts_init_path(&r, path); + CHECK(drafts_count(&r, 1, "customer") == 0); + CHECK(drafts_count(&r, 2, "customer") == 1); + drafts_free(&r); + drafts_free(&d); + unlink(path); + + FILE *f = fopen(path, "w"); + if (f) { + fputs("inte json", f); + fclose(f); + } + drafts_init_path(&r, path); + CHECK(drafts_count(&r, 1, "customer") == 0); + drafts_free(&r); + unlink(path); + + char *a = drafts_new_id(); + char *b = drafts_new_id(); + CHECK(a && b && strncmp(a, "ny-", 3) == 0 && strcmp(a, b) != 0); + free(a); + free(b); +} + int main(void) { test_disp_width(); @@ -733,6 +826,8 @@ int main(void) test_rt_fields(); test_rt_focus(); test_rt_footer(); + test_actions(); + test_drafts(); printf("test_tui: %d checks, %d failures\n", checks, failures); return failures ? 1 : 0; } -- cgit v1.3