Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

26.4 KB
Raw Blame
#include "../main/ui/habitat/supertab.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static ht_supertabs_t model;   // 54 KB: never on the stack, as on the board

static ht_lead_in_t lead(const char *id, const char *state, int order)
{
    return (ht_lead_in_t){.id=id, .project_id="p", .project=id, .engine="claude", .owner="", .local="",
                          .state=state, .order=order, .since=-1};
}
static void frame(ht_supertab_t *t, const char *id, int n, const char *const *states, const int *orders)
{
    assert(ht_supertab_init(t, id, "", id, n));
    for (int i = 0; i < n; i++) {
        char lid[16]; snprintf(lid, sizeof lid, "L%d", i);
        ht_lead_in_t in = lead(lid, states[i], orders ? orders[i] : -1);
        assert(ht_supertab_lead_add(t, &in));
    }
}

static void order_and_limits(void)
{
    static ht_supertab_t t;
    ht_supertabs_reset(&model);
    const char ids[2][ID_MAX] = {"s1", "s2"};
    ht_supertabs_ids(&model, ids, 2, NULL, 0);
    const char *st[4] = {"done", "work", "ask", "stuck"};
    int orders[4] = {2, 0, 2, 1};
    frame(&t, "s1", 4, st, orders);
    ht_supertabs_put(&model, &t, 0, NULL, 0);
    const ht_supertab_t *r = ht_supertabs_find(&model, "s1");
    assert(r && r->lead_n == 4);
    // By order, ties stable: L1(0) L3(1) L0(2) L2(2).
    assert(!strcmp(r->leads[0].id, "L1") && !strcmp(r->leads[1].id, "L3"));
    assert(!strcmp(r->leads[2].id, "L0") && !strcmp(r->leads[3].id, "L2"));
    assert(r->leads[0].state == HT_LEAD_WORK && r->leads[1].state == HT_LEAD_STUCK);
    // A daemon without `order`: frame order.
    frame(&t, "s2", 4, st, NULL);
    ht_supertabs_put(&model, &t, 0, NULL, 0);
    r = ht_supertabs_find(&model, "s2");
    for (int i = 0; i < 4; i++) { char lid[4]; snprintf(lid, sizeof lid, "L%d", i); assert(!strcmp(r->leads[i].id, lid)); }

    // 13 in, 12 kept.
    assert(ht_supertab_init(&t, "s1", "", "s1", 13));
    for (int i = 0; i < 13; i++) {
        char lid[8]; snprintf(lid, sizeof lid, "L%d", i);
        ht_lead_in_t in = lead(lid, "work", i);
        assert(ht_supertab_lead_add(&t, &in) == (i < HT_SUPERTAB_LEADS_MAX));
    }
    assert(t.lead_n == 12 && t.count == 13);
    // A name over 48 bytes is cut on a UTF-8 boundary: 47 ASCII bytes then "é" (2 bytes) leaves 47.
    char name[64]; memset(name, 'a', 47); strcpy(name + 47, "\xc3\xa9tail");
    assert(ht_supertab_init(&t, "s1", "", name, 1));
    assert(strlen(t.name) == 47 && t.name[46] == 'a');
    memset(name, 'b', 46); strcpy(name + 46, "\xc3\xa9tail");   // fits exactly at 48
    assert(ht_supertab_init(&t, "s1", "", name, 1) && strlen(t.name) == 48);
    // An id of ID_MAX bytes or more is rejected, for the Swarm and for a Lead; a Lead without one too.
    char long_id[ID_MAX + 1]; memset(long_id, 'x', ID_MAX); long_id[ID_MAX] = 0;
    assert(!ht_supertab_init(&t, long_id, "", "n", 1));
    assert(!ht_supertab_init(&t, "s1", long_id, "n", 1));
    long_id[ID_MAX - 1] = 0; assert(ht_supertab_init(&t, long_id, "", "n", 1));
    assert(ht_supertab_init(&t, "s1", "", "n", 3));
    ht_lead_in_t bad = lead(long_id, "work", 0); long_id[ID_MAX - 1] = 'x';
    assert(!ht_supertab_lead_add(&t, &bad));
    bad = lead("", "work", 0); assert(!ht_supertab_lead_add(&t, &bad));
    bad = lead(NULL, "work", 0); assert(!ht_supertab_lead_add(&t, &bad));
    bad = lead("L0", "work", 0); assert(ht_supertab_lead_add(&t, &bad) && !ht_supertab_lead_add(&t, &bad));
    assert(ht_supertab_init(&t, "s1", "", "n", 3));
    bad = lead("L9", NULL, 0); bad.project = NULL; bad.local = NULL;   // optional fields may be absent
    assert(ht_supertab_lead_add(&t, &bad) && t.lead_n == 1);
    // Unknown state reads as done; `since` absent stays absent.
    assert(t.leads[0].state == HT_LEAD_DONE && t.leads[0].since_s < 0 && !t.leads[0].project[0]);
    bad = lead("L8", "thinking", 0); bad.since = 134;
    assert(ht_supertab_lead_add(&t, &bad) && t.leads[1].state == HT_LEAD_DONE && t.leads[1].since_s == 134);

    // A put for an id not in the last list is dropped; a list without a Swarm drops its record.
    frame(&t, "s3", 4, st, NULL);
    assert(ht_supertabs_put(&model, &t, 0, NULL, 0) == 0 && !ht_supertabs_find(&model, "s3"));
    const char one[1][ID_MAX] = {"s2"};
    ht_supertabs_ids(&model, one, 1, NULL, 0);
    assert(!ht_supertabs_find(&model, "s1") && ht_supertabs_find(&model, "s2") && model.n == 1);
    // 16 Swarms at most.
    char many[17][ID_MAX];
    for (int i = 0; i < 17; i++) snprintf(many[i], ID_MAX, "m%d", i);
    ht_supertabs_ids(&model, (const char (*)[ID_MAX])many, 17, NULL, 0);
    assert(model.id_n == HT_SUPERTABS_MAX && !ht_supertabs_find(&model, "s2"));
    puts("supertab: order (stable, frame order without it), caps, UTF-8 cut, ids, unknown state PASS");
}

static int events(const ht_lead_event_t *ev, int n, ht_lead_event_kind_t kind, const char *lead_id)
{
    int c = 0;
    for (int i = 0; i < n; i++) if (ev[i].kind == kind && !strcmp(ev[i].lead, lead_id) && !strcmp(ev[i].tab, "s1")) c++;
    return c;
}

static void transitions(void)
{
    static ht_supertab_t t;
    ht_lead_event_t ev[32];
    ht_supertabs_reset(&model);
    const char ids[1][ID_MAX] = {"s1"};
    ht_supertabs_ids(&model, ids, 1, NULL, 0);
    // First seen: ask raises, done/work do not.
    const char *a[3] = {"done", "ask", "work"};
    frame(&t, "s1", 3, a, NULL);
    int n = ht_supertabs_put(&model, &t, 0, ev, 32);
    assert(n == 1 && events(ev, n, HT_LEAD_RAISED_ASK, "L1") == 1);
    // DONE→ASK raises ask once; ASK→ASK raises nothing.
    const char *b[3] = {"ask", "ask", "work"};
    frame(&t, "s1", 3, b, NULL);
    n = ht_supertabs_put(&model, &t, 0, ev, 32);
    assert(n == 1 && events(ev, n, HT_LEAD_RAISED_ASK, "L0") == 1);
    n = ht_supertabs_put(&model, &t, 0, ev, 32);
    assert(n == 0);
    // ASK→STUCK raises stuck; STUCK→WORK clears.
    const char *c[3] = {"stuck", "ask", "work"};
    frame(&t, "s1", 3, c, NULL);
    n = ht_supertabs_put(&model, &t, 0, ev, 32);
    assert(n == 1 && events(ev, n, HT_LEAD_RAISED_STUCK, "L0") == 1);
    const char *d[3] = {"work", "ask", "work"};
    frame(&t, "s1", 3, d, NULL);
    n = ht_supertabs_put(&model, &t, 0, ev, 32);
    assert(n == 1 && events(ev, n, HT_LEAD_CLEARED, "L0") == 1);
    // A Lead gone from the frame clears (L1 was asking); a quiet Lead leaving says nothing.
    const char *e[1] = {"work"};
    frame(&t, "s1", 1, e, NULL);
    n = ht_supertabs_put(&model, &t, 0, ev, 32);
    assert(n == 1 && events(ev, n, HT_LEAD_CLEARED, "L1") == 1);
    // A Swarm leaving the list clears its asking Leads.
    frame(&t, "s1", 2, b, NULL);
    n = ht_supertabs_put(&model, &t, 0, ev, 32);
    assert(n == 2);
    n = ht_supertabs_ids(&model, ids, 0, ev, 32);
    assert(n == 2 && events(ev, n, HT_LEAD_CLEARED, "L0") == 1 && events(ev, n, HT_LEAD_CLEARED, "L1") == 1);
    // The event buffer is never overrun.
    ht_supertabs_ids(&model, ids, 1, NULL, 0);
    frame(&t, "s1", 2, b, NULL);
    assert(ht_supertabs_put(&model, &t, 0, ev, 1) == 1);
    // since is held with the dial clock at receipt.
    assert(ht_supertab_init(&t, "s1", "", "s1", 1));
    ht_lead_in_t w = lead("L0", "work", 0); w.since = 134;
    assert(ht_supertab_lead_add(&t, &w));
    ht_supertabs_put(&model, &t, 5000, NULL, 0);
    const ht_lead_t *l = &ht_supertabs_find(&model, "s1")->leads[0];
    assert(l->since_s == 134 && l->since_at_ms == 5000);
    puts("supertab: raise once, ask->stuck, clear on work/gone/swarm gone, bounded events PASS");
}

static void lookups_and_recap(void)
{
    static ht_supertab_t t;
    ht_supertabs_reset(&model);
    const char ids[2][ID_MAX] = {"s1", "s2"};
    ht_supertabs_ids(&model, ids, 2, NULL, 0);
    assert(ht_supertab_init(&t, "s1", "desk-7", "openharness", 2));
    ht_lead_in_t a = lead("L0", "work", 0); a.local = "h-local";
    ht_lead_in_t b = lead("L1", "work", 1);
    assert(ht_supertab_lead_add(&t, &a) && ht_supertab_lead_add(&t, &b));
    ht_supertabs_put(&model, &t, 0, NULL, 0);
    assert(ht_supertab_init(&t, "s2", "", "mobile", 0));
    ht_supertabs_put(&model, &t, 0, NULL, 0);
    assert(ht_supertabs_for_desk(&model, "desk-7") == ht_supertabs_find(&model, "s1"));
    assert(!ht_supertabs_for_desk(&model, "") && !ht_supertabs_for_desk(&model, "desk-8"));
    const ht_supertab_t *in = NULL;
    const ht_lead_t *l = ht_supertabs_lead_by_local(&model, "h-local", &in);
    assert(l && !strcmp(l->id, "L0") && in == ht_supertabs_find(&model, "s1"));
    assert(!ht_supertabs_lead_by_local(&model, "", &in) && !ht_supertabs_lead_by_local(&model, NULL, &in));
    // `at` walks the list order, skipping Swarms whose frame has not come.
    assert(ht_supertabs_at(&model, 0) == ht_supertabs_find(&model, "s1"));
    assert(ht_supertabs_at(&model, 1) == ht_supertabs_find(&model, "s2") && !ht_supertabs_at(&model, 2));

    // Recap: only the open Lead's text lands.
    ht_supertabs_recap_open(&model, "s1", "L0", 100);
    assert(model.recap.state == HT_RECAP_LOADING && model.recap.asked_ms == 100 && !model.recap.text[0]);
    assert(!ht_supertabs_recap_put(&model, "s1", "L1", "", "", "other", 1, false));
    assert(model.recap.state == HT_RECAP_LOADING && !model.recap.text[0]);
    char *big = malloc(1601); memset(big, 'r', 1599); strcpy(big + 1499, "\xe2\x80\xa6"); memset(big + 1502, 'r', 98); big[1600] = 0;
    assert(ht_supertabs_recap_put(&model, "s1", "L0", "", "", big, 1792560000u, false));
    assert(model.recap.state == HT_RECAP_LOADED && strlen(model.recap.text) == 1499 && model.recap.at == 1792560000u);
    free(big);
    ht_supertabs_recap_open(&model, "s1", "L1", 200);
    assert(model.recap.state == HT_RECAP_LOADING && !model.recap.text[0]);
    assert(ht_supertabs_recap_put(&model, "s1", "L1", "", "", "", 0, true) && model.recap.state == HT_RECAP_EMPTY);
    ht_supertabs_recap_open(&model, "", "", 300);
    assert(model.recap.state == HT_RECAP_NONE && !ht_supertabs_recap_put(&model, "", "", "", "", "x", 0, false));
    ht_supertabs_recap_open(&model, "s1", "L0", 400);
    ht_supertabs_reset(&model);
    assert(model.n == 0 && model.id_n == 0 && model.recap.state == HT_RECAP_NONE && !model.recap.lead[0]);
    puts("supertab: desk link, local Lead, list order, recap slot (one Lead, UTF-8 cut, none), reset PASS");
}

// The round dial's face follows the chat the desktop has open (design 2026-10-28-round-dial-swarm-chat).
static void chats(void)
{
    static ht_supertab_t t;
    ht_supertabs_reset(&model);
    const char ids[1][ID_MAX] = {"s1"};
    ht_supertabs_ids(&model, ids, 1, NULL, 0);
    assert(ht_supertab_init(&t, "s1", "", "openharness", 3));
    // lead keeps chat, chat_id, chat_name
    ht_lead_in_t g = lead("L0", "work", 0); g.chat = "group"; g.chat_id = "g1"; g.chat_name = "Fix login";
    ht_lead_in_t pm = lead("L1", "done", 1); pm.chat = "pm"; pm.chat_id = "pm1"; pm.chat_name = "PM";
    // a lead without chat fields reads as the Lead chat
    ht_lead_in_t old = lead("L2", "ask", 2);
    assert(ht_supertab_lead_add(&t, &g) && ht_supertab_lead_add(&t, &pm) && ht_supertab_lead_add(&t, &old));
    assert(t.leads[0].chat == HT_CHAT_GROUP && !strcmp(t.leads[0].chat_id, "g1") && !strcmp(t.leads[0].chat_name, "Fix login"));
    assert(t.leads[1].chat == HT_CHAT_PM && !strcmp(t.leads[1].chat_id, "pm1") && !strcmp(t.leads[1].chat_name, "PM"));
    assert(t.leads[2].chat == HT_CHAT_LEAD && !t.leads[2].chat_id[0] && !t.leads[2].chat_name[0]);
    // A long group name is cut to 48 bytes on a UTF-8 boundary; an unknown kind, or a chat id that cannot be held, is the
    // Lead chat with no id, and the Lead is kept.
    static ht_supertab_t u;
    assert(ht_supertab_init(&u, "s1", "", "openharness", 3));
    char name[64]; memset(name, 'n', 47); strcpy(name + 47, "\xc3\xa9tail");
    char longer[ID_MAX + 1]; memset(longer, 'x', ID_MAX); longer[ID_MAX] = 0;
    ht_lead_in_t cut = lead("L0", "work", 0); cut.chat = "group"; cut.chat_id = "g2"; cut.chat_name = name;
    ht_lead_in_t odd = lead("L1", "work", 1); odd.chat = "dm"; odd.chat_id = "c1"; odd.chat_name = "x";
    ht_lead_in_t wide = lead("L2", "work", 2); wide.chat = "group"; wide.chat_id = longer; wide.chat_name = "x";
    assert(ht_supertab_lead_add(&u, &cut) && ht_supertab_lead_add(&u, &odd) && ht_supertab_lead_add(&u, &wide));
    assert(strlen(u.leads[0].chat_name) == 47 && u.leads[0].chat == HT_CHAT_GROUP);
    assert(u.leads[1].chat == HT_CHAT_LEAD && !u.leads[1].chat_id[0] && !u.leads[1].chat_name[0]);
    assert(u.leads[2].chat == HT_CHAT_LEAD && !u.leads[2].chat_id[0]);
    ht_supertabs_put(&model, &t, 0, NULL, 0);

    // drops a recap for another chat: the face is the group's, so the Lead chat's recap (or none named) is not its.
    ht_supertabs_recap_open(&model, "s1", "L0", 100);
    assert(!ht_supertabs_recap_put(&model, "s1", "L0", "lead-web", "", "the Lead", 1, false));
    assert(!ht_supertabs_recap_put(&model, "s1", "L0", "", "", "no chat named", 1, false));
    assert(model.recap.state == HT_RECAP_LOADING && !model.recap.text[0]);
    assert(ht_supertabs_recap_put(&model, "s1", "L0", "g1", "", "Tests green.", 2, false));
    assert(model.recap.state == HT_RECAP_LOADED && !strcmp(model.recap.chat_id, "g1") && !strcmp(model.recap.text, "Tests green."));
    // An old daemon's Lead (no chat) takes the recap that names none, as before.
    ht_supertabs_recap_open(&model, "s1", "L2", 200);
    assert(ht_supertabs_recap_put(&model, "s1", "L2", "", "", "Shipped.", 3, false) && !model.recap.chat_id[0]);
    // Opening the Lead already on the face keeps its recap until a new one arrives; one still on its way is asked anew.
    ht_supertabs_recap_open(&model, "s1", "L0", 250);
    assert(ht_supertabs_recap_put(&model, "s1", "L0", "g1", "", "Tests green.", 2, false));
    ht_supertabs_recap_open(&model, "s1", "L0", 260);
    assert(model.recap.state == HT_RECAP_LOADED && !strcmp(model.recap.text, "Tests green.") && model.recap.asked_ms == 260);
    assert(ht_supertabs_recap_put(&model, "s1", "L0", "g1", "", "Deployed.", 4, false) && !strcmp(model.recap.text, "Deployed."));
    ht_supertabs_recap_open(&model, "s1", "L1", 270); ht_supertabs_recap_open(&model, "s1", "L1", 280);   // L1 never had one
    assert(model.recap.state == HT_RECAP_LOADING && model.recap.asked_ms == 280);
    // The face's chat moved on: what it holds is another chat's, and is not kept.
    ht_supertabs_recap_open(&model, "s1", "L0", 290);
    assert(ht_supertabs_recap_put(&model, "s1", "L0", "g1", "", "Tests green.", 2, false));
    t.leads[0].chat_id[0] = 'h'; ht_supertabs_put(&model, &t, 0, NULL, 0);
    ht_supertabs_recap_open(&model, "s1", "L0", 295);
    assert(model.recap.state == HT_RECAP_LOADING && !model.recap.text[0] && !model.recap.chat_id[0]);
    // Opening another Lead forgets the chat the last recap was for.
    ht_supertabs_recap_open(&model, "s1", "L1", 300);
    assert(!model.recap.chat_id[0]);
    puts("supertab: chat kind/id/name, old Leads as the Lead chat, recap only for the face's chat, the face's own reopen keeps it PASS");
}

// A Lead in a thread (spec 2026-11-03-dial-thread-face): its root, title and replies, and a recap that must be the thread's.
static void threads(void)
{
    static ht_supertab_t t;
    ht_supertabs_reset(&model);
    const char ids[1][ID_MAX] = {"s1"};
    ht_supertabs_ids(&model, ids, 1, NULL, 0);
    assert(ht_supertab_init(&t, "s1", "", "openharness", 2));
    char title[200]; memset(title, 't', 97); strcpy(title + 97, "\xc3\xa9x");   // the buffer holds 97 bytes: a letter straddling it is dropped
    ht_lead_in_t a = lead("L0", "work", 0); a.chat = "pm"; a.chat_id = "pm1"; a.thread = "m9"; a.thread_title = "Fix the login bug"; a.thread_replies = 3;
    ht_lead_in_t b = lead("L1", "work", 1); b.chat = "pm"; b.chat_id = "pm1"; b.thread = "m8"; b.thread_title = title; b.thread_replies = -1;
    ht_lead_in_t old = lead("L2", "work", 2); old.thread_replies = -1;   // no thread fields: the main chat
    assert(ht_supertab_lead_add(&t, &a) && ht_supertab_lead_add(&t, &b) && ht_supertab_lead_add(&t, &old));
    // "a lead keeps its thread root, title and replies"
    assert(!strcmp(t.leads[0].thread_root, "m9") && !strcmp(t.leads[0].thread_title, "Fix the login bug") && t.leads[0].thread_replies == 3);
    assert(!strcmp(t.leads[1].thread_root, "m8") && strlen(t.leads[1].thread_title) == 97 && t.leads[1].thread_replies == -1);
    assert(!t.leads[2].thread_root[0] && !t.leads[2].thread_title[0] && t.leads[2].thread_replies == -1);
    ht_supertabs_put(&model, &t, 0, NULL, 0);

    // "a recap for another thread than the face's is refused"
    ht_supertabs_recap_open(&model, "s1", "L0", 100);
    assert(!ht_supertabs_recap_put(&model, "s1", "L0", "pm1", "", "main chat", 1, false));
    assert(!ht_supertabs_recap_put(&model, "s1", "L0", "pm1", "m7", "another thread", 1, false));
    assert(model.recap.state == HT_RECAP_LOADING && !model.recap.text[0]);
    assert(ht_supertabs_recap_put(&model, "s1", "L0", "pm1", "m9", "Taken.", 2, false));
    assert(model.recap.state == HT_RECAP_LOADED && !strcmp(model.recap.thread, "m9"));
    // and the main chat's face refuses a thread's recap
    ht_supertabs_recap_open(&model, "s1", "L2", 110);
    assert(!ht_supertabs_recap_put(&model, "s1", "L2", "", "m9", "thread", 1, false));
    assert(ht_supertabs_recap_put(&model, "s1", "L2", "", NULL, "main", 1, false));

    // "leaving the thread re-opens the main chat's recap": the held thread recap is kept for the thread, not the main chat.
    ht_supertabs_recap_open(&model, "s1", "L0", 200);
    assert(ht_supertabs_recap_put(&model, "s1", "L0", "pm1", "m9", "Taken.", 2, false));
    ht_supertabs_recap_open(&model, "s1", "L0", 210);
    assert(model.recap.state == HT_RECAP_LOADED && !strcmp(model.recap.text, "Taken.") && model.recap.asked_ms == 210);   // same thread: kept
    assert(!ht_supertabs_thread_leave(&model, "s1", "L2") && !ht_supertabs_thread_leave(&model, "s1", "nope"));
    assert(ht_supertabs_thread_leave(&model, "s1", "L0"));
    const ht_lead_t *l0 = &ht_supertabs_find(&model, "s1")->leads[0];
    assert(!l0->thread_root[0] && !l0->thread_title[0] && l0->thread_replies == -1);
    ht_supertabs_recap_open(&model, "s1", "L0", 300);
    assert(model.recap.state == HT_RECAP_LOADING && !model.recap.text[0] && !model.recap.thread[0] && model.recap.asked_ms == 300);
    assert(!ht_supertabs_recap_put(&model, "s1", "L0", "pm1", "m9", "late thread recap", 3, false));
    assert(ht_supertabs_recap_put(&model, "s1", "L0", "pm1", "", "Merged #2704.", 4, false) && !strcmp(model.recap.text, "Merged #2704."));
    puts("supertab: thread root/title/replies, a recap only for the face's thread, leaving it asks the main chat again PASS");
}

// The reader's own Buddy in each project (spec 2026-10-29-round-dial-pm-buddy): `buddies` while the Swarm has them on,
// the Buddy block with its avatar's 15 half bits and colour, and a face that may be its chat.
static void buddies(void)
{
    static ht_supertab_t t;
    assert(ht_supertab_init(&t, "s1", "", "openharness", 3));
    ht_lead_in_t b = lead("L0", "done", 0); b.chat = "buddy"; b.chat_id = "b1"; b.chat_name = "diego's Buddy"; b.buddies = true;
    b.buddy = (ht_buddy_in_t){.present = true, .chat_id = "b1", .name = "diego's Buddy", .engine = "codex", .color = "#d6a79a",
                                .cells = 25513, .live = true};
    // Buddies on, no Buddy in this project; and an older daemon, which sends neither.
    ht_lead_in_t none = lead("L1", "done", 1); none.buddies = true;
    ht_lead_in_t old = lead("L2", "done", 2);
    assert(ht_supertab_lead_add(&t, &b) && ht_supertab_lead_add(&t, &none) && ht_supertab_lead_add(&t, &old));
    const ht_lead_t *l = &t.leads[0];
    assert(l->buddies && l->buddy.has && l->buddy.live && l->buddy.cells == 25513 && l->buddy.color == 0xd6a79a);
    assert(!strcmp(l->buddy.chat_id, "b1") && !strcmp(l->buddy.name, "diego's Buddy") && !strcmp(l->buddy.engine, "codex"));
    assert(l->chat == HT_CHAT_BUDDY && !strcmp(l->chat_id, "b1") && !strcmp(l->chat_name, "diego's Buddy"));
    assert(t.leads[1].buddies && !t.leads[1].buddy.has);
    assert(!t.leads[2].buddies && !t.leads[2].buddy.has && t.leads[2].chat == HT_CHAT_LEAD);
    // A down Buddy is still drawn, not live.
    assert(ht_supertab_init(&t, "s1", "", "openharness", 1));
    b.buddy.live = false; assert(ht_supertab_lead_add(&t, &b) && t.leads[0].buddy.has && !t.leads[0].buddy.live);
    // A block this dial cannot draw is dropped and the Lead kept: a colour that is not #rrggbb, cells past 15 bits or
    // negative, no chat id or one too long to hold. Buddies stay on.
    char longer[ID_MAX + 1]; memset(longer, 'b', ID_MAX); longer[ID_MAX] = 0;
    const char *colors[] = {"d6a79a", "#d6a79", "#d6a79a0", "#d6a7zz", "", NULL};
    for (int i = 0; i < 6; i++) {
        ht_lead_in_t bad = b; bad.buddy.color = colors[i];
        assert(ht_supertab_init(&t, "s1", "", "n", 1) && ht_supertab_lead_add(&t, &bad));
        assert(t.leads[0].buddies && !t.leads[0].buddy.has);
    }
    const long cells[] = {32768, -1};
    for (int i = 0; i < 2; i++) {
        ht_lead_in_t bad = b; bad.buddy.cells = cells[i];
        assert(ht_supertab_init(&t, "s1", "", "n", 1) && ht_supertab_lead_add(&t, &bad) && !t.leads[0].buddy.has);
    }
    const char *ids[] = {"", NULL, longer};
    for (int i = 0; i < 3; i++) {
        ht_lead_in_t bad = b; bad.buddy.chat_id = ids[i];
        assert(ht_supertab_init(&t, "s1", "", "n", 1) && ht_supertab_lead_add(&t, &bad) && !t.leads[0].buddy.has);
    }
    // The upper case and the edges hold: 0 and 32767 cells, #FFFFFF.
    ht_lead_in_t edge = b; edge.buddy.color = "#FFFFFF"; edge.buddy.cells = 32767;
    assert(ht_supertab_init(&t, "s1", "", "n", 1) && ht_supertab_lead_add(&t, &edge));
    assert(t.leads[0].buddy.has && t.leads[0].buddy.color == 0xffffff && t.leads[0].buddy.cells == 32767);
    edge.buddy.cells = 0;
    assert(ht_supertab_init(&t, "s1", "", "n", 1) && ht_supertab_lead_add(&t, &edge) && t.leads[0].buddy.has && !t.leads[0].buddy.cells);
    // A Buddy block without `buddies` (never from the daemon) is not drawn.
    ht_lead_in_t off = b; off.buddies = false;
    assert(ht_supertab_init(&t, "s1", "", "n", 1) && ht_supertab_lead_add(&t, &off) && !t.leads[0].buddy.has);
    // A Buddy face whose chat id cannot be held is the Lead chat, as any other.
    ht_lead_in_t wide = b; wide.chat_id = longer;
    assert(ht_supertab_init(&t, "s1", "", "n", 1) && ht_supertab_lead_add(&t, &wide) && t.leads[0].chat == HT_CHAT_LEAD);
    puts("supertab: Buddies on, the Buddy block and its avatar, a Buddy face, bad blocks dropped with the Lead kept PASS");
}

// A swipe to another project shows the recap it last had at once, not the resting pet until the new one arrives (Diego's
// dial, 2026-11-05): the last few Leads' recaps are remembered, each only for the chat and thread it was for.
static void recap_memory(void)
{
    static ht_supertab_t t;
    ht_supertabs_reset(&model);
    const char ids[1][ID_MAX] = {"s1"};
    ht_supertabs_ids(&model, ids, 1, NULL, 0);
    assert(ht_supertab_init(&t, "s1", "", "openharness", 3));
    ht_lead_in_t a = lead("L0", "work", 0); a.chat = "group"; a.chat_id = "g1"; a.chat_name = "Fix login";
    ht_lead_in_t b = lead("L1", "done", 1); b.chat = "pm"; b.chat_id = "pm1"; b.chat_name = "PM";
    ht_lead_in_t c = lead("L2", "done", 2); c.chat = "pm"; c.chat_id = "pm2"; c.chat_name = "PM";
    assert(ht_supertab_lead_add(&t, &a) && ht_supertab_lead_add(&t, &b) && ht_supertab_lead_add(&t, &c));
    ht_supertabs_put(&model, &t, 0, NULL, 0);
    ht_supertabs_recap_open(&model, "s1", "L0", 100);
    assert(ht_supertabs_recap_put(&model, "s1", "L0", "g1", "", "Tests green.", 2, false));
    ht_supertabs_recap_open(&model, "s1", "L1", 110);
    assert(model.recap.state == HT_RECAP_LOADING);   // never had one: still asked for
    assert(ht_supertabs_recap_put(&model, "s1", "L1", "pm1", "", "", 0, true) && model.recap.state == HT_RECAP_EMPTY);
    // Back to L0: its recap is on the face at once, and the fresh one still lands.
    ht_supertabs_recap_open(&model, "s1", "L0", 120);
    assert(model.recap.state == HT_RECAP_LOADED && !strcmp(model.recap.text, "Tests green.") && !strcmp(model.recap.chat_id, "g1"));
    assert(model.recap.asked_ms == 120 && model.recap.at == 2);
    assert(ht_supertabs_recap_put(&model, "s1", "L0", "g1", "", "Deployed.", 3, false) && !strcmp(model.recap.text, "Deployed."));
    // L1 had none: it comes back as none.
    ht_supertabs_recap_open(&model, "s1", "L1", 130);
    assert(model.recap.state == HT_RECAP_EMPTY && !model.recap.text[0]);
    // The newest text is the one remembered.
    ht_supertabs_recap_open(&model, "s1", "L0", 140);
    assert(model.recap.state == HT_RECAP_LOADED && !strcmp(model.recap.text, "Deployed."));
    // Its chat moved on: the remembered text was another chat's, so the face waits for the new one.
    ht_supertabs_recap_open(&model, "s1", "L2", 150);
    t.leads[0].chat_id[0] = 'h'; ht_supertabs_put(&model, &t, 0, NULL, 0);
    ht_supertabs_recap_open(&model, "s1", "L0", 160);
    assert(model.recap.state == HT_RECAP_LOADING && !model.recap.text[0] && !model.recap.chat_id[0]);
    // A reset forgets them all.
    ht_supertabs_recap_open(&model, "s1", "L1", 170);
    ht_supertabs_reset(&model);
    ht_supertabs_ids(&model, ids, 1, NULL, 0); ht_supertabs_put(&model, &t, 0, NULL, 0);
    ht_supertabs_recap_open(&model, "s1", "L1", 180);
    assert(model.recap.state == HT_RECAP_LOADING);
    // More Leads than it remembers: the oldest is forgotten, the rest kept.
    ht_supertabs_reset(&model);
    static ht_supertab_t many;
    assert(ht_supertab_init(&many, "s1", "", "openharness", HT_SUPERTAB_LEADS_MAX));
    char lid[HT_SUPERTAB_LEADS_MAX][8], cid[HT_SUPERTAB_LEADS_MAX][8];
    for (int i = 0; i < HT_SUPERTAB_LEADS_MAX; i++) {
        snprintf(lid[i], sizeof lid[i], "M%d", i); snprintf(cid[i], sizeof cid[i], "c%d", i);
        ht_lead_in_t m = lead(lid[i], "done", i); m.chat = "pm"; m.chat_id = cid[i]; m.chat_name = "PM";
        assert(ht_supertab_lead_add(&many, &m));
    }
    ht_supertabs_ids(&model, ids, 1, NULL, 0); ht_supertabs_put(&model, &many, 0, NULL, 0);
    for (int i = 0; i < HT_RECAP_REMEMBERED + 1; i++) {
        ht_supertabs_recap_open(&model, "s1", lid[i], 200 + i);
        assert(ht_supertabs_recap_put(&model, "s1", lid[i], cid[i], "", "said", 5, false));
    }
    ht_supertabs_recap_open(&model, "s1", lid[0], 300);
    assert(model.recap.state == HT_RECAP_LOADING);
    ht_supertabs_recap_open(&model, "s1", lid[2], 310);
    assert(model.recap.state == HT_RECAP_LOADED);
    puts("supertab: the last Leads' recaps come back at once on a swipe, only for their chat, oldest forgotten, reset clears PASS");
}

int main(void)
{
    order_and_limits();
    transitions();
    lookups_and_recap();
    chats();
    threads();
    buddies();
    recap_memory();
    return 0;
}