interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / svc / tests / setup_wizard_test.c

629 lines · 31333 bytes Raw Blame History
1/* setup_wizard_test.c -- host test of the setup inventory (svc/setup_inv.c)
2 * and Allen's setup wizard (svc/setup_wizard.c) against the three machine
3 * fixtures in fixtures/setup: a games PC, an AI PC and the Spark. Every path
4 * of FIRST_RUN_UX.md screens 3 to 9 is walked with keys and the clock: both
5 * intents and their own words, Set it up / Show me each one / Not now, the
6 * three ways to an AI (found here, found nearby, connect my own with a key
7 * test), a failed key, no wire for a key, a restart in the middle, and
8 * Escape everywhere. Run: setup_wizard_test <fixtures dir>. */
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include "setup_inv.h"
13#include "setup_wizard.h"
14#include "setup_store.h"
15#include "disk_file.h"
16
17static int fails;
18#define CHECK(c, ...) \
19 do { \
20 if (!(c)) { \
21 fails++; \
22 printf("FAIL %s:%d ", __FILE__, __LINE__); \
23 printf(__VA_ARGS__); \
24 printf("\n"); \
25 } \
26 } while (0)
27
28static char fx[3][4096];
29static unsigned fxn[3];
30
31static void load(const char *dir, const char *name, int i)
32{
33 char path[512];
34 snprintf(path, sizeof path, "%s/%s", dir, name);
35 FILE *f = fopen(path, "rb");
36 if (!f) {
37 printf("FAIL cannot open %s\n", path);
38 exit(1);
39 }
40 fxn[i] = (unsigned)fread(fx[i], 1, sizeof fx[i], f);
41 fclose(f);
42}
43
44/* every string in the view fits, is plain printable ASCII, and names none of our own parts (spec rule 3) */
45static void view_ok(const sw_wizard *w, const char *where)
46{
47 const sw_view *v = sw_view_of(w);
48 const char *texts[SW_LINES + SW_CHOICES + 5];
49 unsigned n = 0;
50 texts[n++] = v->say, texts[n++] = v->hint, texts[n++] = v->aside, texts[n++] = v->prompt, texts[n++] = v->typed;
51 for (unsigned i = 0; i < v->nline; i++)
52 texts[n++] = v->line[i];
53 for (unsigned i = 0; i < v->nchoice; i++)
54 texts[n++] = v->choice[i];
55 static const char *const banned[] = {"Aienux", "Interplane", "Store", "contract", "kernel", "!", "blazing",
56 "seamless", "magic", 0};
57 for (unsigned i = 0; i < n; i++) {
58 for (const char *p = texts[i]; *p; p++)
59 CHECK(*p >= ' ' && *p < 127, "%s: unprintable byte in \"%s\"", where, texts[i]);
60 for (unsigned b = 0; banned[b]; b++)
61 CHECK(!strstr(texts[i], banned[b]), "%s: \"%s\" says %s", where, texts[i], banned[b]);
62 }
63 CHECK(strlen(v->say) < SW_SAY - 1, "%s: say may be cut: %s", where, v->say);
64 for (unsigned i = 0; i < v->nline; i++)
65 CHECK(strlen(v->line[i]) < SW_LINE - 1, "%s: line may be cut: %s", where, v->line[i]);
66 for (unsigned i = 0; i < v->nchoice; i++)
67 CHECK(strlen(v->choice[i]) < SW_CHOICE - 1, "%s: choice may be cut: %s", where, v->choice[i]);
68 CHECK(!v->nchoice || (v->lit >= 0 && (unsigned)v->lit < v->nchoice), "%s: lit %d of %u", where, v->lit, v->nchoice);
69}
70
71static int has_line(const sw_wizard *w, const char *s)
72{
73 const sw_view *v = sw_view_of(w);
74 for (unsigned i = 0; i < v->nline; i++)
75 if (strstr(v->line[i], s))
76 return 1;
77 return 0;
78}
79
80static int has_choice(const sw_wizard *w, const char *s)
81{
82 const sw_view *v = sw_view_of(w);
83 for (unsigned i = 0; i < v->nchoice; i++)
84 if (!strcmp(v->choice[i], s))
85 return 1;
86 return 0;
87}
88
89/* the log so far, one line each, appended to log */
90static char log_text[16384];
91static void drain(sw_wizard *w)
92{
93 char e[SW_LINE];
94 while (sw_event(w, e, sizeof e)) {
95 strncat(log_text, e, sizeof log_text - strlen(log_text) - 2);
96 strncat(log_text, "\n", sizeof log_text - strlen(log_text) - 1);
97 }
98}
99
100static uint32_t now;
101static void key(sw_wizard *w, int k, char c)
102{
103 sw_key(w, k, c, now);
104 view_ok(w, sw_step_name(sw_step_of(w)));
105 drain(w);
106}
107static void enter(sw_wizard *w) { key(w, SW_K_ENTER, 0); }
108static void esc(sw_wizard *w) { key(w, SW_K_ESC, 0); }
109static void type(sw_wizard *w, const char *s)
110{
111 while (*s)
112 key(w, SW_K_CHAR, *s++);
113}
114static void wait_ms(sw_wizard *w, uint32_t ms)
115{
116 for (uint32_t t = 0; t < ms; t += 100) {
117 now += 100;
118 sw_tick(w, now);
119 view_ok(w, sw_step_name(sw_step_of(w)));
120 drain(w);
121 }
122}
123static void until(sw_wizard *w, int step, uint32_t max_ms, const char *what)
124{
125 for (uint32_t t = 0; t < max_ms && sw_step_of(w) != step; t += 100) {
126 now += 100;
127 sw_tick(w, now);
128 drain(w);
129 }
130 CHECK(sw_step_of(w) == step, "%s: at %s, wanted %s", what, sw_step_name(sw_step_of(w)), sw_step_name(step));
131 view_ok(w, what);
132}
133
134static void test_inventory(void)
135{
136 si_inv v;
137 si_init(&v);
138 unsigned n = si_parse(&v, fx[0], fxn[0]);
139 CHECK(n == 13, "games pc items %u", n);
140 CHECK(v.host && v.ngpu == 1 && v.gpu[0].vendor == 0x10de && v.gpu[0].device == 0x2786, "games gpu");
141 CHECK(v.gpu[0].vram_mib == 12282 && !strcmp(v.gpu[0].driver, "NVIDIA 560.94"), "games gpu memory/driver");
142 CHECK(v.ndisplay == 1 && v.display[0].hz == 60 && v.display[0].max_hz == 165 && v.display[0].vrr, "games display");
143 CHECK(v.cpu.fast == 8 && v.cpu.threads == 16 && v.ram_mib == 32768 && v.ndisk == 2 && v.nusb == 3, "games cpu etc");
144 CHECK(v.npeer == 1 && v.peer[0].kind == SI_PEER_AIENOS && v.peer[0].params_b10 == 270, "games peer");
145 CHECK(!strcmp(v.gateway, "192.168.1.1") && v.skipped == 0, "games gateway, skipped %u", v.skipped);
146 char out[8][96];
147 unsigned k = si_summary(&v, out, 8);
148 CHECK(k == 7, "games summary %u lines", k);
149 CHECK(!strcmp(out[0], "Screen: ASUS VG27AQ, 2560 by 1440, 60 times a second (it can do 165)"), "%s", out[0]);
150 CHECK(!strcmp(out[1], "Graphics: NVIDIA GeForce RTX 4070, 12 GB of its own memory"), "%s", out[1]);
151 CHECK(!strcmp(out[2], "Processor: AMD Ryzen 7 7800X3D, 8 cores"), "%s", out[2]);
152 CHECK(!strcmp(out[3], "Memory: 32 GB"), "%s", out[3]);
153 CHECK(!strcmp(out[4], "Storage: 931 GB fast drive; 1.9 TB hard disk"), "%s", out[4]);
154 CHECK(!strcmp(out[5], "Nearby: 1 other AienOS computer (spark), and your router"), "%s", out[5]);
155 CHECK(!strcmp(out[6], "Also: 3 USB devices"), "%s", out[6]);
156
157 si_init(&v);
158 n = si_parse(&v, fx[1], fxn[1]);
159 CHECK(n == 12 && v.ngpu == 2 && v.nmodel == 2, "ai pc items %u gpus %u models %u", n, v.ngpu, v.nmodel);
160 CHECK(si_best_gpu(&v) == 0 && v.gpu[1].kind == SI_GPU_INTEGRATED, "ai pc best gpu");
161 CHECK(v.model[0].src == SI_SRC_OLLAMA && !v.model[0].running && v.model[0].mib == 4700, "ollama model");
162 CHECK(v.model[1].src == SI_SRC_LMSTUDIO && v.model[1].running && !strcmp(v.model[1].where, "http://127.0.0.1:1234"),
163 "lmstudio server");
164 k = si_summary(&v, out, 8);
165 CHECK(!strcmp(out[1], "Graphics: NVIDIA GeForce RTX 4090, 24 GB of its own memory, and 1 more"), "%s", out[1]);
166 CHECK(!strcmp(out[2], "Processor: Intel Core i9-13900K, 24 cores (8 fast, 16 efficient)"), "%s",
167 out[2]);
168
169 si_init(&v);
170 n = si_parse(&v, fx[2], fxn[2]);
171 CHECK(n == 10 && v.gpu[0].kind == SI_GPU_UNIFIED && v.gpu[0].vram_mib == 124607, "spark items %u", n);
172 CHECK(v.nmodel == 1 && v.model[0].running && v.model[0].mib == 29460, "spark server");
173 k = si_summary(&v, out, 8);
174 CHECK(!strcmp(out[1], "Graphics: NVIDIA GB10, 122 GB of shared memory"), "%s", out[1]);
175
176 /* what a newer launcher or a broken one may hand in */
177 static const char odd[] = "# comment\n\n gpu vendor=1002 name=\"AMD Radeon\" future=1\nhologram depth=3\n"
178 "display name=\"never closed\nram mib=99999999999999\nusb noequals name=\"Pad\"\n"
179 "model params_b10=10\nmachine name=\"x\" from=host\r\n";
180 si_init(&v);
181 n = si_parse(&v, odd, sizeof odd - 1);
182 CHECK(n == 4, "odd items taken %u", n);
183 CHECK(v.ngpu == 1 && v.gpu[0].vendor == 0x1002 && v.ndisplay == 0 && v.nmodel == 0, "odd kept");
184 CHECK(v.ram_mib == 0xffffffffu && v.nusb == 1 && !strcmp(v.usb[0], "Pad"), "odd values");
185 CHECK(v.skipped == 5, "odd skipped %u", v.skipped);
186
187 /* what the kernel itself finds, with no host */
188 si_init(&v);
189 si_add_gpu(&v, 0x1234, 0x1111, 0x030000);
190 si_add_display(&v, 1280, 800);
191 si_add_cpus(&v, 2, "QEMU Virtual CPU");
192 si_add_ram(&v, 1024ull << 20);
193 si_add_disk(&v, "QEMU HARDDISK", 64ull << 20, SI_DISK_VIRTUAL);
194 v.npci = 7;
195 CHECK(si_best_gpu(&v) == -1, "a virtual display runs no model");
196 k = si_summary(&v, out, 8);
197 CHECK(!strcmp(out[0], "Screen: this screen, 1280 by 800"), "%s", out[0]);
198 CHECK(!strcmp(out[1], "Graphics: only a simple virtual display"), "%s", out[1]);
199 CHECK(!strcmp(out[2], "Processor: 2 cores") && !strcmp(out[3], "Memory: 1 GB"), "%s / %s", out[2], out[3]);
200 CHECK(!strcmp(out[k - 1], "Also: 7 parts on the main board"), "%s", out[k - 1]);
201}
202
203static si_inv games, ai, spark, empty;
204
205static void parse_all(void)
206{
207 si_init(&games);
208 si_parse(&games, fx[0], fxn[0]);
209 si_init(&ai);
210 si_parse(&ai, fx[1], fxn[1]);
211 si_init(&spark);
212 si_parse(&spark, fx[2], fxn[2]);
213 si_init(&empty);
214}
215
216/* games PC: games, Set it up, the Spark's model nearby used after 10 s */
217static void test_games(void)
218{
219 static sw_wizard w;
220 char rec[512];
221 now = 1000;
222 log_text[0] = 0;
223 sw_start(&w, &games, 1, 0, 0, now);
224 drain(&w);
225 CHECK(!strcmp(w.v.say, "Hello. I'm Allen. I'm going to look after this computer with you."), "%s", w.v.say);
226 until(&w, SW_INTENT, 6100, "hello moves on");
227 CHECK(w.v.nchoice == 3 && w.v.lit == 0 && !strcmp(w.v.choice[0], "Play games smoothly"), "intent choices");
228 key(&w, SW_K_RIGHT, 0);
229 CHECK(w.v.lit == 1 && strstr(w.v.hint, "run an AI"), "right lights the next: %s", w.v.hint);
230 key(&w, SW_K_LEFT, 0);
231 enter(&w);
232 CHECK(sw_step_of(&w) == SW_SCAN && !strcmp(w.v.say, "Getting to know this computer."), "scan %s", w.v.say);
233 CHECK(w.v.nline == 7, "scan lines %u", w.v.nline);
234 until(&w, SW_CONSENT, 10000, "scan moves on");
235 CHECK(w.v.nline == 5 && !strcmp(w.v.line[1], "Set your screen to 165 times a second"), "consent: %s", w.v.line[1]);
236 CHECK(has_line(&w, "fast processor cores") && has_line(&w, "Build drivers") && !has_line(&w, "Run the AI"),
237 "games consent list");
238 enter(&w);
239 CHECK(sw_step_of(&w) == SW_GAMES, "games path, at %s", sw_step_name(sw_step_of(&w)));
240 CHECK(!strcmp(w.v.line[0], "Screen: 60 times a second now, it can do 165. My graphics driver will set it."), "%s",
241 w.v.line[0]);
242 CHECK(!strcmp(w.v.line[1], "Graphics: NVIDIA GeForce RTX 4070, driven by NVIDIA 560.94"), "%s", w.v.line[1]);
243 CHECK(has_line(&w, "I'll measure them the first time a game runs"), "frames are not invented");
244 for (unsigned i = 0; i < w.v.nline; i++)
245 if (strstr(w.v.line[i], "Frames"))
246 CHECK(!strpbrk(w.v.line[i], "0123456789"), "a frame rate with no measurement: %s", w.v.line[i]);
247 enter(&w);
248 CHECK(sw_step_of(&w) == SW_LLM, "llm");
249 CHECK(!strcmp(w.v.say, "I build drivers with an AI. I found The 27B model nearby. I'll use it."), "%s", w.v.say);
250 CHECK(has_choice(&w, "Use it") && has_choice(&w, "Choose another") && has_line(&w, "On spark, nearby"), "one found");
251 wait_ms(&w, 9800);
252 CHECK(sw_step_of(&w) == SW_LLM, "not before 10 s");
253 until(&w, SW_READY, 400, "auto-picked");
254 CHECK(has_line(&w, "Games will use your screen at 165 times a second") &&
255 has_line(&w, "Your AI is qwen3.8-27b-fp8, on a computer nearby.") && has_line(&w, "build drivers"),
256 "ready lines");
257 unsigned n = sw_llm_conf(&w, rec, sizeof rec);
258 CHECK(n && !strcmp(rec, "kind openai\nbase_url http://127.0.0.1:18091\nmodel qwen3.8-27b-fp8\nkey_env none\n"),
259 "llm.conf: %s", rec);
260 sw_consent(&w, rec, sizeof rec);
261 CHECK(!strcmp(rec, "aienos-consent v1\ndrivers-own yes\nscreen-refresh yes\ncpu-games yes\ndrivers-build yes\n"),
262 "consent: %s", rec);
263 CHECK(sw_record(&w, rec, sizeof rec) && strstr(rec, "\nkind openai\nbase_url http://127.0.0.1:18091\n") &&
264 strstr(rec, "\naienos-consent v1\ndrivers-own yes\n"),
265 "the disk record carries llm.conf and consent: %s", rec);
266 enter(&w);
267 CHECK(sw_step_of(&w) == SW_DONE && w.v.nchoice == 0 && w.v.nline == 0, "done clears the panel");
268 n = sw_save(&w, rec, sizeof rec);
269 CHECK(n && sw_saved_done(rec, n), "a finished setup is saved as done: %s", rec);
270 CHECK(strstr(log_text, "intent games\n") && strstr(log_text, "consent all\n") &&
271 strstr(log_text, "llm auto-picked after 10 s\n") && strstr(log_text, "llm picked peer qwen3.8-27b-fp8\n") &&
272 strstr(log_text, "step done\n"),
273 "games log:\n%s", log_text);
274}
275
276/* AI PC: the AI by its number, Show me each one, two models here (the running one first) */
277static void test_ai(void)
278{
279 static sw_wizard w;
280 char rec[512];
281 now = 1000;
282 log_text[0] = 0;
283 sw_start(&w, &ai, 1, 0, 0, now);
284 enter(&w); /* hello: Enter moves on */
285 CHECK(sw_step_of(&w) == SW_INTENT, "enter skips hello");
286 key(&w, SW_K_CHAR, '2');
287 CHECK(sw_step_of(&w) == SW_SCAN, "2 picks the AI");
288 enter(&w);
289 CHECK(sw_step_of(&w) == SW_CONSENT && has_line(&w, "Run the AI on your NVIDIA GeForce RTX 4090"), "ai consent");
290 CHECK(!has_line(&w, "screen to"), "no screen change for an AI");
291 key(&w, SW_K_DOWN, 0);
292 enter(&w);
293 CHECK(sw_step_of(&w) == SW_EACH && has_line(&w, "Use my own drivers"), "each 1");
294 enter(&w); /* yes: drivers */
295 key(&w, SW_K_CHAR, '2'); /* skip: ai on the gpu */
296 CHECK(has_line(&w, "Build drivers"), "each 3");
297 enter(&w); /* yes: build */
298 CHECK(sw_step_of(&w) == SW_AI, "ai path, at %s", sw_step_name(sw_step_of(&w)));
299 CHECK(!strcmp(w.v.line[0], "NVIDIA GeForce RTX 4090 has 24 GB of its own memory."), "%s", w.v.line[0]);
300 CHECK(!strcmp(w.v.line[1], "The 27B model needs 29 GB for its weights: it fits if I share main memory, slower."),
301 "%s", w.v.line[1]);
302 CHECK(!strcmp(w.v.line[3], "A small 4B model needs 8 GB for its weights: it fits on the graphics card."), "%s",
303 w.v.line[3]);
304 CHECK(has_line(&w, "not measured yet"), "working memory not invented");
305 enter(&w);
306 CHECK(sw_step_of(&w) == SW_LLM && !strcmp(w.v.say, "I found 2 AI models. The first one fits best."), "%s",
307 w.v.say);
308 CHECK(!strcmp(w.v.choice[0], "qwen2.5-7b-instruct") && !strcmp(w.v.choice[1], "llama3.1:8b") &&
309 has_choice(&w, "Connect my own"),
310 "running first, then installed, then connect");
311 CHECK(!strcmp(w.v.hint, "Already running on this computer"), "%s", w.v.hint);
312 key(&w, SW_K_TAB, 0);
313 CHECK(!strcmp(w.v.hint, "Installed with Ollama on this computer"), "%s", w.v.hint);
314 wait_ms(&w, 12000);
315 CHECK(sw_step_of(&w) == SW_LLM, "two found: nothing picked by itself");
316 key(&w, SW_K_CHAR, '1');
317 CHECK(sw_step_of(&w) == SW_READY && has_line(&w, "Your AI is qwen2.5-7b-instruct, on this computer."), "picked");
318 sw_llm_conf(&w, rec, sizeof rec);
319 CHECK(!strcmp(rec, "kind openai\nbase_url http://127.0.0.1:1234\nmodel qwen2.5-7b-instruct\nkey_env none\n"), "%s",
320 rec);
321 sw_consent(&w, rec, sizeof rec);
322 CHECK(!strcmp(rec, "aienos-consent v1\ndrivers-own yes\nai-gpu no\ndrivers-build yes\n"), "consent: %s", rec);
323 CHECK(strstr(log_text, "consent yes drivers-own\nconsent skip ai-gpu\nconsent yes drivers-build\n") != 0,
324 "each log:\n%s", log_text);
325 until(&w, SW_DONE, 15100, "ready moves on by itself");
326
327 /* an installed model nothing serves yet: chosen, but nothing to call */
328 sw_start(&w, &ai, 1, 0, 0, now);
329 w.want = SW_WANT_ASKED | SW_WANT_AI;
330 sw_key(&w, SW_K_ESC, 0, now);
331 until(&w, SW_INTENT, 100, "escape on hello");
332}
333
334/* the Spark: their own words, Not now, choose another, connect another service, the key test */
335static void test_spark(void)
336{
337 static sw_wizard w;
338 char rec[512];
339 const char *kind = "", *url = "", *model = "", *k = "";
340 now = 1000;
341 log_text[0] = 0;
342 sw_start(&w, &spark, 1, 0, 0, now);
343 until(&w, SW_INTENT, 6100, "hello");
344 type(&w, "Run my own AI and play games");
345 CHECK(sw_step_of(&w) == SW_OTHER && !strcmp(w.v.typed, "Run my own AI and play games"), "typing on the question");
346 enter(&w);
347 CHECK(sw_step_of(&w) == SW_OTHER_CHECK && !strcmp(w.v.say, "You want smoother games and an AI here. I'll set up both."),
348 "%s", w.v.say);
349 key(&w, SW_K_RIGHT, 0);
350 enter(&w); /* not quite */
351 CHECK(sw_step_of(&w) == SW_OTHER && w.v.ntyped == 0, "not quite: the bar again");
352 type(&w, "write letters quietly");
353 enter(&w);
354 CHECK(!strcmp(w.v.say, "You'd like this: write letters quietly. I'll keep everything else simple."), "%s", w.v.say);
355 key(&w, SW_K_ESC, 0);
356 CHECK(sw_step_of(&w) == SW_INTENT, "escape goes back to the question");
357 type(&w, "both gaming and an ai model");
358 enter(&w);
359 enter(&w); /* that's right */
360 CHECK(strstr(log_text, "intent games+ai\n") != 0, "both intents");
361 until(&w, SW_CONSENT, 10000, "scan");
362 key(&w, SW_K_CHAR, '3'); /* not now */
363 CHECK(sw_step_of(&w) == SW_GAMES, "not now still shows the path, at %s", sw_step_name(sw_step_of(&w)));
364 CHECK(!strcmp(w.v.line[0], "Screen: 60 times a second, the most it can do."), "%s", w.v.line[0]);
365 CHECK(has_line(&w, "the 10 fast cores stay free for the game, the rest on the 10 efficient ones."), "cores");
366 enter(&w);
367 CHECK(sw_step_of(&w) == SW_AI && !strcmp(w.v.line[0], "NVIDIA GB10 shares 122 GB of memory with the processor."),
368 "%s", w.v.line[0]);
369 CHECK(!strcmp(w.v.line[1], "The 27B model needs 29 GB for its weights: it fits."), "%s", w.v.line[1]);
370 enter(&w);
371 CHECK(!strcmp(w.v.say, "I found The 27B model on this computer. I'll use it."), "%s", w.v.say);
372 key(&w, SW_K_CHAR, '2'); /* choose another */
373 CHECK(sw_step_of(&w) == SW_LLM && !strcmp(w.v.say, "Here's what I found.") && has_choice(&w, "Connect my own"),
374 "the full list: %s", w.v.say);
375 wait_ms(&w, 11000);
376 CHECK(sw_step_of(&w) == SW_LLM, "the list is never auto-picked");
377 key(&w, SW_K_CHAR, '2');
378 CHECK(sw_step_of(&w) == SW_SERVICE, "connect");
379 key(&w, SW_K_CHAR, '3');
380 CHECK(sw_step_of(&w) == SW_URL, "another service asks its address");
381 type(&w, "localhost:18091");
382 enter(&w);
383 CHECK(sw_step_of(&w) == SW_URL && has_line(&w, "http:// or https://"), "a bad address is said plainly");
384 for (int i = 0; i < 15; i++)
385 key(&w, SW_K_BACK, 0);
386 type(&w, "http://127.0.0.1:18091");
387 enter(&w);
388 CHECK(sw_step_of(&w) == SW_KEY && strstr(w.v.say, "or press Enter if it needs none"), "key: %s", w.v.say);
389 type(&w, "sk-test-123");
390 CHECK(!strcmp(w.v.typed, "***********") && !strstr(w.v.say, "sk-test"), "the key shows as dots");
391 enter(&w);
392 CHECK(sw_step_of(&w) == SW_TESTING, "testing");
393 CHECK(sw_test_wanted(&w, &kind, &url, &model, &k) == 1, "a test is wanted");
394 CHECK(!strcmp(kind, "openai") && !strcmp(url, "http://127.0.0.1:18091") && !strcmp(model, "auto") &&
395 !strcmp(k, "sk-test-123"),
396 "test request");
397 CHECK(sw_test_wanted(&w, &kind, &url, &model, &k) == 0, "asked once");
398 sw_test_done(&w, 1, 0, now);
399 drain(&w);
400 CHECK(sw_step_of(&w) == SW_READY && has_line(&w, "Your AI is your own service, through your key. It works."),
401 "ready after the test");
402 CHECK(has_line(&w, "Nothing was changed"), "not now is said at the end");
403 int clean = 1;
404 for (unsigned i = 0; i < sizeof w.key; i++)
405 clean &= w.key[i] == 0;
406 CHECK(clean && w.nkey == 0, "the key is wiped after the test");
407 sw_llm_conf(&w, rec, sizeof rec);
408 CHECK(!strcmp(rec, "kind openai\nbase_url http://127.0.0.1:18091\nmodel auto\nkey_env AIENOS_LLM_KEY\n"), "%s",
409 rec);
410 sw_save(&w, rec, sizeof rec);
411 CHECK(!strstr(rec, "sk-test") && !strstr(rec, "letters"), "no key and no wish in the saved record: %s", rec);
412 CHECK(!strstr(log_text, "sk-test") && !strstr(log_text, "127.0.0.1") && !strstr(log_text, "letters"),
413 "nothing typed reaches the log:\n%s", log_text);
414}
415
416/* a key that fails, Claude without a key, Escape out of the key screens */
417static void test_key_fails(void)
418{
419 static sw_wizard w;
420 const char *kind = "", *url = "", *model = "", *k = "";
421 now = 1000;
422 sw_start(&w, &spark, 1, 0, 0, now);
423 enter(&w);
424 key(&w, SW_K_CHAR, '2');
425 enter(&w);
426 enter(&w);
427 enter(&w);
428 CHECK(sw_step_of(&w) == SW_LLM, "at the pick, at %s", sw_step_name(sw_step_of(&w)));
429 key(&w, SW_K_CHAR, '2');
430 key(&w, SW_K_CHAR, '2');
431 key(&w, SW_K_CHAR, '1'); /* Claude */
432 CHECK(sw_step_of(&w) == SW_KEY && strstr(w.v.say, "Type your key for Claude."), "%s", w.v.say);
433 enter(&w);
434 CHECK(sw_step_of(&w) == SW_KEY && has_line(&w, "I need the key"), "Claude needs a key");
435 type(&w, "bad-key");
436 enter(&w);
437 CHECK(sw_test_wanted(&w, &kind, &url, &model, &k) && !strcmp(kind, "anthropic") &&
438 !strcmp(url, "https://api.anthropic.com"),
439 "claude test");
440 sw_test_done(&w, 0, "key not valid", now);
441 CHECK(sw_step_of(&w) == SW_TEST_FAILED && !strcmp(w.v.say, "That didn't work. The service said: key not valid. Try again?"),
442 "%s", w.v.say);
443 CHECK(w.nkey == 0 && !w.key[0], "a failed key is wiped too");
444 enter(&w);
445 CHECK(sw_step_of(&w) == SW_KEY && w.v.ntyped == 0, "try again: an empty key");
446 type(&w, "x");
447 esc(&w);
448 CHECK(sw_step_of(&w) == SW_SERVICE && w.nkey == 0, "escape wipes the key");
449 key(&w, SW_K_CHAR, '2');
450 type(&w, "y");
451 enter(&w);
452 CHECK(sw_step_of(&w) == SW_TESTING, "chatgpt testing");
453 CHECK(sw_test_wanted(&w, &kind, &url, &model, &k) && !strcmp(url, "https://api.openai.com"), "chatgpt url");
454 wait_ms(&w, 29800);
455 CHECK(sw_step_of(&w) == SW_TESTING, "still waiting");
456 wait_ms(&w, 400);
457 CHECK(sw_step_of(&w) == SW_TEST_FAILED && strstr(w.v.say, "no answer came back"), "a silent service: %s", w.v.say);
458 key(&w, SW_K_CHAR, '2');
459 CHECK(sw_step_of(&w) == SW_LLM && has_choice(&w, "Connect my own"), "choose another goes to the list");
460}
461
462/* no wire for a key (native, no launcher), nothing found anywhere */
463static void test_alone(void)
464{
465 static sw_wizard w;
466 char rec[256];
467 now = 1000;
468 log_text[0] = 0;
469 sw_start(&w, &empty, 0, 0, 0, now);
470 enter(&w);
471 esc(&w);
472 CHECK(sw_step_of(&w) == SW_SCAN && strstr(w.v.say, "That's fine. I'll set up the basics first."), "%s", w.v.say);
473 CHECK(has_line(&w, "I couldn't find much yet"), "nothing found is said");
474 enter(&w);
475 CHECK(sw_step_of(&w) == SW_CONSENT && w.v.nline == 3, "consent with no intent: %u", w.v.nline);
476 enter(&w);
477 CHECK(sw_step_of(&w) == SW_LLM && strstr(w.v.say, "I didn't find an AI model"), "%s", w.v.say);
478 CHECK(!strcmp(w.v.hint, "The small 4B model, an 8 GB download"), "fetch the size that fits: %s", w.v.hint);
479 key(&w, SW_K_CHAR, '2'); /* connect my own, with no wire */
480 CHECK(sw_step_of(&w) == SW_LLM && strstr(w.v.say, "I can't keep a key safely here yet") &&
481 !has_choice(&w, "Connect my own"),
482 "no wire: %s", w.v.say);
483 enter(&w);
484 CHECK(sw_step_of(&w) == SW_READY && has_line(&w, "I'll get the small 4B model when this computer can download it."),
485 "fetch");
486 CHECK(sw_llm_conf(&w, rec, sizeof rec) == 0, "nothing to call yet");
487 CHECK(strstr(log_text, "connect refused: no wire to keep a key\n") != 0, "log:\n%s", log_text);
488
489 /* Escape on every question */
490 sw_start(&w, &games, 1, 0, 0, now);
491 esc(&w);
492 esc(&w);
493 esc(&w);
494 esc(&w);
495 CHECK(sw_step_of(&w) == SW_LLM, "escape through, at %s", sw_step_name(sw_step_of(&w)));
496 esc(&w);
497 CHECK(sw_step_of(&w) == SW_READY && has_line(&w, "No AI yet") && has_line(&w, "Nothing was changed"), "ready");
498 sw_consent(&w, rec, sizeof rec);
499 CHECK(!strcmp(rec, "aienos-consent v1\ndrivers-own no\ndrivers-build no\n"), "%s", rec);
500 esc(&w);
501 CHECK(sw_step_of(&w) == SW_DONE, "done");
502 CHECK(sw_key(&w, SW_K_ENTER, 0, now) == 0, "done takes no keys");
503}
504
505/* a restart in the middle carries on where it stopped */
506static void test_resume(void)
507{
508 static sw_wizard w, again;
509 char rec[512];
510 now = 1000;
511 sw_start(&w, &games, 1, 0, 0, now);
512 enter(&w);
513 enter(&w);
514 enter(&w);
515 CHECK(sw_step_of(&w) == SW_CONSENT, "at consent");
516 /* the disk record before an AI is picked: the step and the consent, no llm.conf yet */
517 unsigned r = sw_record(&w, rec, sizeof rec);
518 CHECK(r && strstr(rec, "aienos-setup v1\nstep 5\n") && strstr(rec, "aienos-consent v1\n") && !strstr(rec, "base_url"),
519 "record before a pick: %s", rec);
520 CHECK(sw_record(&w, rec, 40) == 0 && rec[0] == 0, "a short buffer gets no record");
521 unsigned n = sw_save(&w, rec, sizeof rec);
522 CHECK(n && !sw_saved_done(rec, n), "saved mid-way: %s", rec);
523 log_text[0] = 0;
524 sw_start(&again, &games, 1, rec, n, now);
525 drain(&again);
526 CHECK(!strcmp(again.v.say, "Welcome back. Let's carry on where we stopped."), "%s", again.v.say);
527 CHECK(strstr(log_text, "resumed at consent\n") != 0, "log:\n%s", log_text);
528 until(&again, SW_CONSENT, 4100, "back at consent");
529 CHECK(again.v.nline == 5 && has_line(&again, "165 times a second"), "the same list after a restart");
530 /* inside the key screens a restart goes back to the pick */
531 sw_start(&w, &spark, 1, 0, 0, now);
532 enter(&w);
533 key(&w, SW_K_CHAR, '2');
534 enter(&w);
535 enter(&w);
536 enter(&w);
537 key(&w, SW_K_CHAR, '2');
538 key(&w, SW_K_CHAR, '2');
539 key(&w, SW_K_CHAR, '1');
540 type(&w, "secret");
541 n = sw_save(&w, rec, sizeof rec);
542 CHECK(!strstr(rec, "secret"), "the key is not saved");
543 sw_start(&again, &spark, 1, rec, n, now);
544 until(&again, SW_LLM, 4100, "a restart in the key screen goes back to the pick");
545 /* a broken record is a first start */
546 sw_start(&again, &games, 1, "aienos-setup v1\nstep x\n", 22, now);
547 CHECK(!strcmp(again.v.say, "Hello. I'm Allen. I'm going to look after this computer with you."), "broken record");
548 CHECK(sw_save(&again, rec, 20) == 0 && rec[0] == 0, "a short buffer gets nothing, not half a record");
549}
550
551/* the records on disk: two copies, a power cut in a save, a spoiled copy */
552static void test_store(const char *dir)
553{
554 char path[512], text[CK_SETUP_TEXT + 1];
555 unsigned n;
556 snprintf(path, sizeof path, "%s/setup-store.img", dir);
557 static disk_file f;
558 disk_dev d;
559 uint64_t blocks = (uint64_t)(CK_SETUP_FIRST + CK_SETUP_UNITS) * (CK_ALLEN_MEM_UNIT / 512);
560 CHECK(disk_file_open(&f, &d, path, 512, blocks, 1) == 0, "store image");
561 ck_setup_store st;
562 CHECK(ck_setup_load(&st, &d, text, sizeof text, &n) == 1 && n == 0, "a blank section is a first start");
563 CHECK(ck_setup_save(&st, "aienos-setup v1\nstep 5\n", 23) == 0 && st.gen == 1 && st.newest == 0, "first save in A");
564 CHECK(ck_setup_save(&st, "aienos-setup v1\nstep 9\n", 23) == 0 && st.newest == 1, "second save in B");
565 CHECK(ck_setup_load(&st, &d, text, sizeof text, &n) == 0 && !strcmp(text, "aienos-setup v1\nstep 9\n") && st.gen == 2,
566 "the newest comes back: %s", text);
567 disk_file_arm_cut(&f, 0); /* power lost before the next save (into copy A) reached the disk */
568 CHECK(ck_setup_save(&st, "aienos-setup v1\nstep 16\n", 24) != 0, "a cut save fails");
569 disk_file_disarm(&f);
570 CHECK(ck_setup_load(&st, &d, text, sizeof text, &n) == 0 && !strcmp(text, "aienos-setup v1\nstep 9\n"),
571 "after the cut the last whole save is there: %s", text);
572 uint8_t junk[512];
573 memset(junk, 0xa5, sizeof junk);
574 disk_write(&d, (uint64_t)(CK_SETUP_FIRST + 1) * 8, 1, junk); /* spoil copy B, the newest */
575 CHECK(ck_setup_load(&st, &d, text, sizeof text, &n) == 0 && !strcmp(text, "aienos-setup v1\nstep 5\n"),
576 "a spoiled newest copy: the older one is used: %s", text);
577 /* the fingerprint covers bytes 0..31 and the text only: a byte set after the text must
578 * spoil the copy too (the unit's tail is zero by the format, setup_store.h) */
579 CHECK(ck_setup_save(&st, "aienos-setup v1\nstep 7\n", 23) == 0 && st.newest == 1, "a save into B again");
580 uint8_t blk[512];
581 CHECK(disk_read(&d, (uint64_t)(CK_SETUP_FIRST + 1) * 8, 1, blk) == DISK_OK, "read B back");
582 blk[64 + 23 + 5] = 0x5a;
583 disk_write(&d, (uint64_t)(CK_SETUP_FIRST + 1) * 8, 1, blk);
584 CHECK(ck_setup_load(&st, &d, text, sizeof text, &n) == 0 && !strcmp(text, "aienos-setup v1\nstep 5\n"),
585 "a copy with a byte set after its text is not taken: %s", text);
586 uint8_t last[512];
587 CHECK(ck_setup_save(&st, "aienos-setup v1\nstep 8\n", 23) == 0 && st.newest == 1, "B saved whole again");
588 CHECK(disk_read(&d, (uint64_t)(CK_SETUP_FIRST + 1) * 8 + 7, 1, last) == DISK_OK, "read B's last block");
589 last[511] = 1;
590 disk_write(&d, (uint64_t)(CK_SETUP_FIRST + 1) * 8 + 7, 1, last);
591 CHECK(ck_setup_load(&st, &d, text, sizeof text, &n) == 0 && !strcmp(text, "aienos-setup v1\nstep 5\n"),
592 "a copy with its unit's last byte set is not taken: %s", text);
593 CHECK(ck_setup_save(&st, "x\x01y", 3) == 0, "a save of any bytes");
594 CHECK(ck_setup_load(&st, &d, text, sizeof text, &n) == 0 && !strcmp(text, "aienos-setup v1\nstep 5\n"),
595 "a copy with a control byte is not taken: %s", text);
596 disk_write(&d, (uint64_t)CK_SETUP_FIRST * 8, 1, junk);
597 CHECK(ck_setup_load(&st, &d, text, sizeof text, &n) == 1 && n == 0, "no valid copy: a first start, not junk");
598 char big[CK_SETUP_TEXT + 2];
599 memset(big, 'a', sizeof big);
600 CHECK(ck_setup_save(&st, big, CK_SETUP_TEXT + 1) != 0, "too long is refused");
601 disk_dev small = d;
602 small.block_count = blocks - 1;
603 CHECK(ck_setup_load(&st, &small, text, sizeof text, &n) < 0, "a section too small is refused");
604 disk_file_close(&f);
605}
606
607int main(int argc, char **argv)
608{
609 setvbuf(stdout, 0, _IONBF, 0);
610 const char *dir = argc > 1 ? argv[1] : "native/kernel/svc/tests/fixtures/setup";
611 load(dir, "games-pc.txt", 0);
612 load(dir, "ai-pc.txt", 1);
613 load(dir, "spark.txt", 2);
614 test_inventory();
615 parse_all();
616 test_games();
617 test_ai();
618 test_spark();
619 test_key_fails();
620 test_alone();
621 test_resume();
622 test_store(argc > 2 ? argv[2] : "/tmp");
623 if (fails) {
624 printf("%d checks failed\n", fails);
625 return 1;
626 }
627 printf("all setup checks passed\n");
628 return 0;
629}