interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / tests / visor / test_visor_console.c

723 lines · 32534 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* test_visor_console.c -- lane 2: parser + console framework tests (Omega Visor V1). */
18#include "visor.h"
19#include "visor_console.h"
20#include "visor_parse_command.h"
21#include "omega_core.h"
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
27static int g_pass, g_total;
28
29#define CHECK(cond, name) do { \
30 g_total++; \
31 if (cond) g_pass++; \
32 else fprintf(stderr, "FAIL %s (%s:%d)\n", name, __FILE__, __LINE__); \
33} while (0)
34
35static const char ZERO_ID[] = "sha256:0000000000000000000000000000000000000000000000000000000000000000";
36
37/* ---------------- parser ---------------- */
38
39typedef struct {
40 const char *line;
41 int rc;
42 VisorCmd cmd;
43 size_t argc;
44 const char *a0, *a1;
45 bool for_cur;
46} ParseCase;
47
48static void test_parser(void) {
49 static const ParseCase cases[] = {
50 { "help", 0, VISOR_CMD_HELP, 0, NULL, NULL, false },
51 { "help id\n", 0, VISOR_CMD_HELP, 1, "id", NULL, false },
52 { "quit\r\n", 0, VISOR_CMD_QUIT, 0, NULL, NULL, false },
53 { " inspect x ", 0, VISOR_CMD_INSPECT, 1, "x", NULL, false },
54 { "type _", 0, VISOR_CMD_TYPE, 1, "_", NULL, false },
55 { "id _", 0, VISOR_CMD_ID, 1, "_", NULL, false },
56 { "graph", -1, VISOR_CMD_GRAPH, 0, NULL, NULL, false },
57 { "graph y", 0, VISOR_CMD_GRAPH, 1, "y", NULL, false },
58 { "verify f", 0, VISOR_CMD_VERIFY, 1, "f", NULL, false },
59 { "machine", 0, VISOR_CMD_MACHINE, 0, NULL, NULL, false },
60 { "realize x", 0, VISOR_CMD_REALIZE, 1, "x", NULL, false },
61 { "realize x for current.machine", 0, VISOR_CMD_REALIZE, 1, "x", NULL, true },
62 { "cost x", 0, VISOR_CMD_COST, 1, "x", NULL, false },
63 { "run f 3 4", 0, VISOR_CMD_RUN, 3, "f", "3", false },
64 { "evidence", 0, VISOR_CMD_EVIDENCE, 0, NULL, NULL, false },
65 { "evidence VISOR", 0, VISOR_CMD_EVIDENCE, 1, "VISOR", NULL, false },
66 { "bindings", 0, VISOR_CMD_BINDINGS, 0, NULL, NULL, false },
67 { "clear", 0, VISOR_CMD_CLEAR, 0, NULL, NULL, false },
68 { "world", 0, VISOR_CMD_WORLD, 0, NULL, NULL, false },
69 { "effects x", 0, VISOR_CMD_EFFECTS, 1, "x", NULL, false },
70 { "alternatives x", 0, VISOR_CMD_ALTERNATIVES, 1, "x", NULL, false },
71 { "compare a b", 0, VISOR_CMD_COMPARE, 2, "a", "b", false },
72 { "why\tx", 0, VISOR_CMD_WHY, 1, "x", NULL, false },
73 { "let x = 3 * 6", 0, VISOR_CMD_SOURCE, 0, NULL, NULL, false },
74 { "fn f(x) = x + 1", 0, VISOR_CMD_SOURCE, 0, NULL, NULL, false },
75 { "x + 1", 0, VISOR_CMD_SOURCE, 0, NULL, NULL, false },
76 { "helpp", 0, VISOR_CMD_SOURCE, 0, NULL, NULL, false },
77 { "let \xCE\xA9 = 1", 0, VISOR_CMD_SOURCE, 0, NULL, NULL, false },
78 { "", 0, VISOR_CMD_EMPTY, 0, NULL, NULL, false },
79 { " \t ", 0, VISOR_CMD_EMPTY, 0, NULL, NULL, false },
80 { "# comment", 0, VISOR_CMD_EMPTY, 0, NULL, NULL, false },
81 /* malformed */
82 { "id", -1, VISOR_CMD_ID, 0, NULL, NULL, false },
83 { "id a b", -1, VISOR_CMD_ID, 0, NULL, NULL, false },
84 { "quit now", -1, VISOR_CMD_QUIT, 0, NULL, NULL, false },
85 { "run a b c d e", -1, VISOR_CMD_RUN, 0, NULL, NULL, false },
86 { "run a b c d e f g", -1, VISOR_CMD_RUN, 0, NULL, NULL, false },
87 { "realize x for mars", -1, VISOR_CMD_REALIZE, 0, NULL, NULL, false },
88 { "inspect x\x01", -1, VISOR_CMD_UNKNOWN, 0, NULL, NULL, false },
89 { "let x = 1\x1b[2J", -1, VISOR_CMD_UNKNOWN, 0, NULL, NULL, false },
90 { "let x = \x7f", -1, VISOR_CMD_UNKNOWN, 0, NULL, NULL, false },
91 { "id \xff", -1, VISOR_CMD_UNKNOWN, 0, NULL, NULL, false },
92 { "id \xC0\xAF", -1, VISOR_CMD_UNKNOWN, 0, NULL, NULL, false }, /* overlong '/' */
93 { "id \xED\xA0\x80", -1, VISOR_CMD_UNKNOWN, 0, NULL, NULL, false }, /* surrogate */
94 { "id \xCE", -1, VISOR_CMD_UNKNOWN, 0, NULL, NULL, false }, /* truncated */
95 };
96 for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
97 const ParseCase *pc = &cases[i];
98 VisorCommand cmd;
99 char err[256];
100 int rc = visor_parse_command(pc->line, &cmd, err, sizeof(err));
101 char name[96];
102 snprintf(name, sizeof(name), "parse case %zu", i);
103 bool ok = (rc == pc->rc) && (cmd.cmd == pc->cmd);
104 if (ok && rc == 0) {
105 ok = cmd.argc == pc->argc && cmd.for_current_machine == pc->for_cur;
106 if (pc->a0) ok = ok && strcmp(cmd.args[0], pc->a0) == 0;
107 if (pc->a1) ok = ok && strcmp(cmd.args[1], pc->a1) == 0;
108 }
109 if (ok && rc != 0) ok = err[0] != '\0';
110 CHECK(ok, name);
111 }
112
113 /* SOURCE keeps the whole trimmed line, untokenized. */
114 VisorCommand cmd;
115 char err[256];
116 CHECK(visor_parse_command(" let x = 3 * 6 \n", &cmd, err, sizeof(err)) == 0 &&
117 strcmp(cmd.line, "let x = 3 * 6") == 0 && cmd.argc == 0, "source line kept");
118
119 /* Oversized token: 256 bytes rejected (not truncated), 255 accepted. */
120 char big[300];
121 memcpy(big, "id ", 3);
122 memset(big + 3, 'a', 256);
123 big[3 + 256] = '\0';
124 CHECK(visor_parse_command(big, &cmd, err, sizeof(err)) == -1 &&
125 strstr(err, "too long") != NULL, "oversized token rejected");
126 big[3 + 255] = '\0';
127 CHECK(visor_parse_command(big, &cmd, err, sizeof(err)) == 0 && strlen(cmd.args[0]) == 255,
128 "255-byte token accepted");
129
130 /* Over-long line rejected, not truncated. */
131 char *longline = malloc(VISOR_LINE_MAX + 8);
132 memset(longline, 'x', VISOR_LINE_MAX + 4);
133 longline[VISOR_LINE_MAX + 4] = '\0';
134 CHECK(visor_parse_command(longline, &cmd, err, sizeof(err)) == -1 && strstr(err, "line too long"),
135 "oversized line rejected");
136 free(longline);
137
138 /* Deterministic: same input, identical struct bytes. */
139 VisorCommand c1, c2;
140 visor_parse_command("run f 1 2", &c1, err, sizeof(err));
141 visor_parse_command("run f 1 2", &c2, err, sizeof(err));
142 CHECK(memcmp(&c1, &c2, sizeof(c1)) == 0, "parser deterministic");
143
144 CHECK(strcmp(visor_cmd_name(VISOR_CMD_ALTERNATIVES), "alternatives") == 0 &&
145 strcmp(visor_cmd_name(VISOR_CMD_SOURCE), "source") == 0, "cmd names");
146}
147
148/* ---------------- console harness ---------------- */
149
150typedef struct {
151 VisorSession s;
152 VisorConsole c;
153 FILE *f;
154 char *buf;
155 size_t len;
156} Harness;
157
158static void h_open(Harness *h, bool json) {
159 memset(h, 0, sizeof(*h));
160 if (visor_session_init(&h->s) != 0) { fprintf(stderr, "session init failed\n"); exit(1); }
161 h->f = open_memstream(&h->buf, &h->len);
162 visor_console_init(&h->c, &h->s, h->f, h->f, json); /* out and err share one stream */
163}
164
165/* Returns the output of this one line (caller frees). */
166static char *h_exec(Harness *h, const char *line, int *rc) {
167 size_t before = h->len;
168 fflush(h->f);
169 before = h->len;
170 int r = visor_console_exec_line(&h->c, line);
171 fflush(h->f);
172 if (rc) *rc = r;
173 return strndup(h->buf + before, h->len - before);
174}
175
176static void h_close(Harness *h) {
177 fclose(h->f);
178 free(h->buf);
179 visor_session_destroy(&h->s);
180}
181
182static void expect(Harness *h, const char *line, int want_rc, const char *want, const char *name) {
183 int rc;
184 char *got = h_exec(h, line, &rc);
185 bool ok = rc == want_rc && strcmp(got, want) == 0;
186 if (!ok) fprintf(stderr, "--- %s: rc=%d want %d\n--- got:\n%s--- want:\n%s", name, rc, want_rc, got, want);
187 CHECK(ok, name);
188 free(got);
189}
190
191static void fake_id(SemanticId *id, unsigned char seed) {
192 for (size_t i = 0; i < sizeof(id->bytes); i++) id->bytes[i] = (unsigned char)(seed + i);
193}
194
195static void test_console_human(void) {
196 Harness h;
197 h_open(&h, false);
198
199 /* help: exact first/last lines + one entry per command word. */
200 int rc;
201 char *help = h_exec(&h, "help", &rc);
202 CHECK(rc == 0 && strncmp(help, "Omega Visor commands (classes: inspection, pure-execution, simulation, effect-request):\n", 87) == 0,
203 "help header");
204 static const char *words[] = { "help", "quit", "inspect", "type", "id", "graph", "verify", "machine", "realize",
205 "cost", "run", "evidence", "bindings", "clear", "world", "effects", "alternatives",
206 "compare", "why", "let x" };
207 size_t lines = 0;
208 for (const char *p = help; *p; p++) if (*p == '\n') lines++;
209 CHECK(lines == 22, "help line count");
210 for (size_t i = 0; i < sizeof(words) / sizeof(words[0]); i++) {
211 char needle[64];
212 snprintf(needle, sizeof(needle), "\n %s", words[i]);
213 CHECK(strstr(help, needle) != NULL, "help lists command");
214 }
215 CHECK(strstr(help, " run <x> [args...] pure-execution run x if pure; effects become an effect request\n") != NULL,
216 "help row exact");
217 free(help);
218 expect(&h, "help id", 0,
219 " id <x> inspection show the semantic id of a name, _ or hex id\n",
220 "help one");
221 expect(&h, "help nope", 1, "error: help: no such command 'nope'\n", "help unknown");
222
223 /* bindings empty, then after visor_binding_set (insertion order). */
224 expect(&h, "bindings", 0, "(no bindings)\n", "bindings empty");
225 SemanticId a, b;
226 fake_id(&a, 0x10);
227 fake_id(&b, 0xA0);
228 CHECK(visor_binding_set(&h.s, "zeta", VISOR_BIND_OBJECT, &a, -1) == 0, "bind zeta");
229 CHECK(visor_binding_set(&h.s, "alpha", VISOR_BIND_PROGRAM, &b, 0) == 0, "bind alpha");
230 char ida[72], idb[72];
231 visor_format_id(&a, ida);
232 visor_format_id(&b, idb);
233 char want[512];
234 snprintf(want, sizeof(want), "zeta object %s\nalpha program %s\n", ida, idb);
235 expect(&h, "bindings", 0, want, "bindings insertion order");
236
237 /* id of a binding, unknown name, hex not in graph (fail closed), no `_` yet. */
238 snprintf(want, sizeof(want), "%s\n", ida);
239 expect(&h, "id zeta", 0, want, "id binding");
240 expect(&h, "id nope", 1, "error: id: unknown name or id 'nope'\n", "id unknown");
241 snprintf(want, sizeof(want), "error: id: unknown name or id '%s'\n", ZERO_ID);
242 expect(&h, ZERO_ID + 0, 1, "error: source: not wired in this build\n", "bare hex is source");
243 char line[128];
244 snprintf(line, sizeof(line), "id %s", ZERO_ID);
245 expect(&h, line, 1, want, "id hex not in graph");
246 expect(&h, "id _", 1, "error: id: unknown name or id '_'\n", "id _ unset");
247
248 /* hex id of an object that IS in the graph resolves. */
249 OmegaObject *t = omega_build_type_uint(h.s.graph, 64);
250 CHECK(t != NULL, "build type");
251 if (t) {
252 char tid[72];
253 visor_format_id(&t->id, tid);
254 snprintf(line, sizeof(line), "id %s", tid);
255 snprintf(want, sizeof(want), "%s\n", tid);
256 expect(&h, line, 0, want, "id hex in graph");
257 }
258
259 /* not-wired shapes (phase A default ops). */
260 expect(&h, "inspect zeta", 1, "error: inspect: not wired in this build\n", "inspect not wired");
261 expect(&h, "let y = 1", 1, "error: source: not wired in this build\n", "source not wired");
262 expect(&h, "run zeta", 1, "error: run: not wired in this build\n", "run not wired");
263 expect(&h, "machine", 1, "error: machine: not wired in this build\n", "machine not wired");
264
265 /* malformed / empty */
266 expect(&h, "", 0, "", "empty line");
267 expect(&h, "# note", 0, "", "comment line");
268 expect(&h, "compare a", 1, "error: compare: expected 2 arguments, got 1\n", "arity error");
269 expect(&h, "id \x01", 1, "error: unknown: control character 0x01 at byte 3 is not allowed\n", "control char");
270
271 /* clear then quit */
272 expect(&h, "clear", 0, "session cleared\n", "clear");
273 expect(&h, "bindings", 0, "(no bindings)\n", "bindings after clear");
274 CHECK(!h.c.quit, "not quit yet");
275 expect(&h, "quit", 0, "bye\n", "quit");
276 CHECK(h.c.quit, "quit sets flag");
277 h_close(&h);
278}
279
280/* ---------------- stub ops (phase-B contract exercised now) ---------------- */
281
282static int stub_eval(VisorSession *s, const char *line, VisorBinding *out, char *value, size_t n,
283 char *err, size_t errn) {
284 if (strcmp(line, "bad") == 0) { snprintf(err, errn, "parse error at 1:1"); return 1; }
285 SemanticId id;
286 fake_id(&id, 0x33);
287 if (strncmp(line, "let ", 4) == 0) visor_binding_set(s, "x", VISOR_BIND_OBJECT, &id, -1);
288 memset(out, 0, sizeof(*out));
289 out->kind = VISOR_BIND_OBJECT;
290 out->id = id;
291 out->index = -1;
292 snprintf(value, n, "18");
293 return 0;
294}
295
296static int stub_classify(const VisorViewCall *call, VisorClass *cls, char *reason, size_t rn,
297 char *err, size_t errn) {
298 (void)err; (void)errn;
299 if (strcmp(call->cmd->args[0], "x") == 0) { *cls = VISOR_CLASS_PURE_EXECUTION; snprintf(reason, rn, "no effects"); }
300 else { *cls = VISOR_CLASS_EFFECT_REQUEST; snprintf(reason, rn, "writes a file"); }
301 return 0;
302}
303
304static int g_run_calls;
305static int stub_run(const VisorViewCall *call, FILE *out, char *err, size_t errn) {
306 (void)err; (void)errn;
307 g_run_calls++;
308 if (call->json) fputs("{\"value\":\"18\"}", out); else fputs("18\n", out);
309 return 0;
310}
311
312static int stub_cost(const VisorViewCall *call, FILE *out, char *err, size_t errn) {
313 (void)call; (void)out;
314 snprintf(err, errn, "no \"estimate\"");
315 return 1;
316}
317
318static int stub_effects(const VisorViewCall *call, FILE *out, char *err, size_t errn) {
319 (void)err; (void)errn;
320 fputs(call->json ? "[]" : "no effects\n", out);
321 return 0;
322}
323
324static int stub_verify(const VisorViewCall *call, FILE *out, char *err, size_t errn) {
325 fputs(call->json ? "{\"passed\":false}" : "V0 FAIL\n", out);
326 snprintf(err, errn, "verification failed: V0");
327 return VISOR_VIEW_ERROR_WITH_OUTPUT;
328}
329
330static void install_stubs(Harness *h) {
331 VisorConsoleOps ops;
332 memset(&ops, 0, sizeof(ops));
333 ops.eval_line = stub_eval;
334 ops.effects_classify = stub_classify;
335 ops.run_pure = stub_run;
336 ops.cost = stub_cost;
337 ops.effects = stub_effects;
338 ops.verify = stub_verify;
339 visor_console_set_ops(&h->c, &ops);
340}
341
342static void test_console_stubs(void) {
343 Harness h;
344 h_open(&h, false);
345 install_stubs(&h);
346 expect(&h, "let x = 3 * 6", 0, "18\n", "source ok");
347 CHECK(h.s.has_last && strcmp(h.s.last.name, "_") == 0, "source sets _");
348 SemanticId id;
349 fake_id(&id, 0x33);
350 char want[256], idt[72];
351 visor_format_id(&id, idt);
352 snprintf(want, sizeof(want), "%s\n", idt);
353 expect(&h, "id _", 0, want, "id _ after source");
354 snprintf(want, sizeof(want), "x object %s\n_ object %s\n", idt, idt);
355 expect(&h, "bindings", 0, want, "bindings shows _ last");
356 expect(&h, "bad", 1, "error: source: parse error at 1:1\n", "source error");
357
358 g_run_calls = 0;
359 expect(&h, "run x", 0, "[pure-execution]\n18\n", "run pure");
360 CHECK(g_run_calls == 1, "run_pure called once");
361 SemanticId e;
362 fake_id(&e, 0x44);
363 visor_binding_set(&h.s, "w", VISOR_BIND_OBJECT, &e, -1);
364 expect(&h, "run w", 1,
365 "EFFECT REQUEST: requires authority; routed via EffectIntent -> AEGIS/PHYSICS (not executed); reason: writes a file\n",
366 "run effect request");
367 CHECK(g_run_calls == 1, "effect request never executed");
368 expect(&h, "run nope", 1, "error: run: unknown name or id 'nope'\n", "run unresolved");
369 CHECK(g_run_calls == 1, "unresolved never executed");
370 expect(&h, "effects", 0, "[effect-request]\nno effects\n", "effects view");
371 expect(&h, "verify x", 1, "V0 FAIL\nerror: verify: verification failed: V0\n", "error with output (human)");
372 expect(&h, "cost x", 1, "error: cost: no \"estimate\"\n", "hook error");
373 h_close(&h);
374
375 /* Classifier missing but run_pure present: fail closed. */
376 h_open(&h, false);
377 VisorConsoleOps ops;
378 memset(&ops, 0, sizeof(ops));
379 ops.run_pure = stub_run;
380 visor_console_set_ops(&h.c, &ops);
381 g_run_calls = 0;
382 expect(&h, "run _", 1, "error: run: not wired in this build\n", "run without classifier");
383 CHECK(g_run_calls == 0, "run without classifier not executed");
384 h_close(&h);
385}
386
387static void test_console_json(void) {
388 Harness h;
389 h_open(&h, true);
390 install_stubs(&h);
391 expect(&h, "bindings", 0, "{\"command\":\"bindings\",\"status\":\"ok\",\"class\":\"inspection\",\"result\":[],\"error\":null}\n",
392 "json bindings empty");
393 expect(&h, "help quit", 0,
394 "{\"command\":\"help\",\"status\":\"ok\",\"class\":\"inspection\",\"result\":[{\"command\":\"quit\",\"usage\":\"quit\","
395 "\"class\":\"inspection\",\"meaning\":\"leave the console\"}],\"error\":null}\n", "json help one");
396 SemanticId id;
397 fake_id(&id, 0x33);
398 char idt[72], want[1024];
399 visor_format_id(&id, idt);
400 snprintf(want, sizeof(want),
401 "{\"command\":\"source\",\"status\":\"ok\",\"class\":\"pure-execution\",\"result\":{\"value\":\"18\",\"kind\":\"object\",\"id\":\"%s\"},\"error\":null}\n", idt);
402 expect(&h, "let x = 3 * 6", 0, want, "json source");
403 snprintf(want, sizeof(want),
404 "{\"command\":\"id\",\"status\":\"ok\",\"class\":\"inspection\",\"result\":{\"name\":\"x\",\"kind\":\"object\",\"id\":\"%s\"},\"error\":null}\n", idt);
405 expect(&h, "id x", 0, want, "json id");
406 snprintf(want, sizeof(want),
407 "{\"command\":\"bindings\",\"status\":\"ok\",\"class\":\"inspection\",\"result\":[{\"name\":\"x\",\"kind\":\"object\",\"id\":\"%s\"},"
408 "{\"name\":\"_\",\"kind\":\"object\",\"id\":\"%s\"}],\"error\":null}\n", idt, idt);
409 expect(&h, "bindings", 0, want, "json bindings");
410 expect(&h, "id nope", 1,
411 "{\"command\":\"id\",\"status\":\"error\",\"class\":\"inspection\",\"result\":null,\"error\":\"unknown name or id 'nope'\"}\n",
412 "json id error");
413 expect(&h, "inspect x", 1,
414 "{\"command\":\"inspect\",\"status\":\"error\",\"class\":\"inspection\",\"result\":null,\"error\":\"not wired in this build\"}\n",
415 "json not wired");
416 expect(&h, "run x", 0,
417 "{\"command\":\"run\",\"status\":\"ok\",\"class\":\"pure-execution\",\"result\":{\"value\":\"18\"},\"error\":null}\n",
418 "json run pure");
419 SemanticId e;
420 fake_id(&e, 0x44);
421 visor_binding_set(&h.s, "w", VISOR_BIND_OBJECT, &e, -1);
422 expect(&h, "run w", 1,
423 "{\"command\":\"run\",\"status\":\"error\",\"class\":\"effect-request\",\"result\":null,\"error\":\"EFFECT REQUEST: requires authority; "
424 "routed via EffectIntent -> AEGIS/PHYSICS (not executed); reason: writes a file\"}\n", "json effect request");
425 expect(&h, "cost x", 1,
426 "{\"command\":\"cost\",\"status\":\"error\",\"class\":\"simulation\",\"result\":null,\"error\":\"no \\\"estimate\\\"\"}\n",
427 "json escaping");
428 expect(&h, "verify x", 1,
429 "{\"command\":\"verify\",\"status\":\"error\",\"class\":\"pure-execution\",\"result\":{\"passed\":false},\"error\":\"verification failed: V0\"}\n",
430 "json error with output");
431 expect(&h, "effects", 0,
432 "{\"command\":\"effects\",\"status\":\"ok\",\"class\":\"effect-request\",\"result\":[],\"error\":null}\n", "json effects");
433 expect(&h, "quit", 0, "{\"command\":\"quit\",\"status\":\"ok\",\"class\":\"inspection\",\"result\":\"bye\",\"error\":null}\n", "json quit");
434 h_close(&h);
435}
436
437/* ---------------- script mode ---------------- */
438
439static char *slurp(const char *path, size_t *n) {
440 FILE *f = fopen(path, "rb");
441 if (!f) return NULL;
442 char *buf = NULL;
443 size_t cap = 0;
444 FILE *m = open_memstream(&buf, &cap);
445 int ch;
446 while ((ch = fgetc(f)) != EOF) fputc(ch, m);
447 fclose(f);
448 fclose(m);
449 *n = cap;
450 return buf;
451}
452
453static void test_script(void) {
454 const char *spath = "tests/visor/sessions/basic.omega-session";
455 const char *epath = "tests/visor/sessions/basic.expected";
456 size_t en = 0;
457 char *expected = slurp(epath, &en);
458 FILE *script = fopen(spath, "r");
459 CHECK(expected && script, "session files present (run from repo root)");
460 if (!expected || !script) { free(expected); if (script) fclose(script); return; }
461
462 Harness h;
463 h_open(&h, false);
464 int rc = visor_console_run_script(&h.c, script);
465 fflush(h.f);
466 fclose(script);
467 bool same = h.len == en && memcmp(h.buf, expected, en) == 0;
468 if (!same) fprintf(stderr, "--- script got:\n%.*s--- expected:\n%s", (int)h.len, h.buf, expected);
469 CHECK(same, "basic.omega-session matches basic.expected exactly");
470 CHECK(rc == 1, "script with errors returns 1");
471 CHECK(h.c.quit, "script stopped at quit");
472 h_close(&h);
473
474 /* JSON mode: no echo, exactly one object per executed command (12 before quit + quit). */
475 script = fopen(spath, "r");
476 h_open(&h, true);
477 visor_console_run_script(&h.c, script);
478 fflush(h.f);
479 fclose(script);
480 size_t objs = 0;
481 bool shape = true;
482 for (char *p = h.buf; p && *p;) {
483 char *nl = strchr(p, '\n');
484 if (!nl) { shape = false; break; }
485 if (strncmp(p, "{\"command\":\"", 12) != 0 || nl[-1] != '}' || memchr(p, '\n', (size_t)(nl - p)) != NULL) shape = false;
486 objs++;
487 p = nl + 1;
488 }
489 CHECK(shape && objs == 12, "json script: one object per line");
490 CHECK(strstr(h.buf, "\xCE\xA9>") == NULL, "json script: no echo");
491 h_close(&h);
492
493 free(expected);
494}
495
496/* ---------------- end-to-end through the real wiring (tools/omega.c) ---------------- */
497#ifndef VISOR_CONSOLE_LANE_ONLY
498#define OMEGA_TOOL_NO_MAIN
499#include "../../tools/omega.c"
500
501/* Host-dependent output in the e2e session: the whole `machine` command (its
502 * echo up to the next echo) and any other line naming the host machine
503 * profile (realization "machine" lines, estimated-cost sources, the verify
504 * MACHINE row). Both are replaced by fixed markers before comparing. */
505#define E2E_PROMPT "\xCE\xA9> "
506#define E2E_MACHINE_MARK "<machine block: host-dependent, masked>\n"
507#define E2E_LINE_MARK "<host machine line: host-dependent, masked>\n"
508
509/* Finds the `machine` block in actual output; checks it is non-empty and
510 * carries the fixed `provenance` key; returns the machine name it prints. */
511static bool e2e_machine_block(const char *buf, size_t n, char *name, size_t nn) {
512 static const char echo[] = E2E_PROMPT "machine\n";
513 char *copy = malloc(n + 1);
514 if (!copy) return false;
515 memcpy(copy, buf, n);
516 copy[n] = '\0';
517 bool ok = false;
518 char *b = strstr(copy, echo);
519 if (b) {
520 b += sizeof(echo) - 1;
521 char *end = strstr(b, E2E_PROMPT);
522 if (end) *end = '\0';
523 if (b[0] && strstr(b, "\n provenance ") && strncmp(b, "machine ", 8) == 0) {
524 size_t len = strcspn(b + 8, "\n");
525 if (len > 0 && len < nn) {
526 memcpy(name, b + 8, len);
527 name[len] = '\0';
528 ok = true;
529 }
530 }
531 }
532 free(copy);
533 return ok;
534}
535
536static bool e2e_put(char **out, size_t *o, size_t *cap, const char *src, size_t sl) {
537 if (*o + sl + 1 > *cap) {
538 size_t nc = (*o + sl + 1) * 2;
539 char *t = realloc(*out, nc);
540 if (!t) return false;
541 *out = t;
542 *cap = nc;
543 }
544 memcpy(*out + *o, src, sl);
545 *o += sl;
546 return true;
547}
548
549static char *e2e_mask(const char *buf, size_t n, const char *host, size_t *outlen) {
550 size_t cap = n + 1;
551 char *out = malloc(cap);
552 if (!out) return NULL;
553 size_t o = 0, i = 0;
554 size_t pl = sizeof(E2E_PROMPT) - 1;
555 bool in_machine = false;
556 while (i < n) {
557 size_t e = i;
558 while (e < n && buf[e] != '\n') e++;
559 size_t ll = e - i + (e < n ? 1 : 0);
560 const char *line = buf + i;
561 bool is_echo = ll >= pl && memcmp(line, E2E_PROMPT, pl) == 0;
562 if (is_echo) in_machine = ll == pl + 8 && memcmp(line + pl, "machine\n", 8) == 0;
563 if (in_machine) {
564 if (is_echo && !e2e_put(&out, &o, &cap, E2E_MACHINE_MARK, sizeof(E2E_MACHINE_MARK) - 1)) { free(out); return NULL; }
565 } else {
566 bool host_line = false;
567 size_t hl = strlen(host);
568 if (!is_echo && hl > 0)
569 for (size_t k = 0; k + hl <= ll; k++)
570 if (memcmp(line + k, host, hl) == 0) { host_line = true; break; }
571 const char *src = host_line ? E2E_LINE_MARK : line;
572 size_t sl = host_line ? sizeof(E2E_LINE_MARK) - 1 : ll;
573 if (!e2e_put(&out, &o, &cap, src, sl)) { free(out); return NULL; }
574 }
575 i += ll;
576 }
577 out[o] = '\0';
578 *outlen = o;
579 return out;
580}
581
582static void e2e_open(Harness *h, bool json) {
583 memset(h, 0, sizeof(*h));
584 if (omega_tool_session_init(&h->s) != 0) { fprintf(stderr, "session init failed\n"); exit(1); }
585 h->f = open_memstream(&h->buf, &h->len);
586 visor_console_init(&h->c, &h->s, h->f, h->f, json);
587 VisorConsoleOps ops;
588 visor_console_default_ops(&ops);
589 visor_console_set_ops(&h->c, &ops);
590 h->c.evidence_root = "tests/visor/sessions/evidence-root";
591}
592
593static void test_e2e(void) {
594 VisorConsoleOps ops;
595 visor_console_default_ops(&ops);
596 CHECK(ops.inspect && ops.type_of && ops.id_of && ops.graph_text && ops.eval_line && ops.verify && ops.machine &&
597 ops.realize && ops.cost && ops.run_pure && ops.evidence && ops.world && ops.effects_classify && ops.effects &&
598 ops.alternatives && ops.compare && ops.why, "every hook wired");
599
600 size_t en = 0;
601 char *expected = slurp("tests/visor/sessions/e2e.expected", &en);
602 FILE *script = fopen("tests/visor/sessions/e2e.omega-session", "r");
603 CHECK(expected && script, "e2e session files present");
604 if (expected && script) {
605 Harness h;
606 e2e_open(&h, false);
607 int rc = visor_console_run_script(&h.c, script);
608 fflush(h.f);
609 /* Host-dependent parts are checked separately, then masked on both sides. */
610 char host[128] = "";
611 bool block_ok = e2e_machine_block(h.buf, h.len, host, sizeof(host));
612 CHECK(block_ok && host[0], "e2e machine block present, non-empty, has provenance + machine name");
613 size_t al = 0, el = 0;
614 char *am = e2e_mask(h.buf, h.len, host, &al);
615 char *em = e2e_mask(expected, en, host, &el);
616 bool same = am && em && al == el && memcmp(am, em, al) == 0;
617 if (!same) fprintf(stderr, "--- e2e got (masked):\n%.*s--- expected (masked):\n%.*s", (int)al, am ? am : "",
618 (int)el, em ? em : "");
619 CHECK(same, "e2e.omega-session matches e2e.expected exactly (host-dependent lines masked)");
620 CHECK(rc == 0, "e2e script all ok");
621 CHECK(strstr(h.buf, "\xCE\xA9> x + y\n18\n") && strstr(h.buf, "\xCE\xA9> type _\nu64\n") &&
622 strstr(h.buf, "\xCE\xA9> run _\n[pure-execution]\n18\n") &&
623 strstr(h.buf, "\xCE\xA9> run _ 5\n[pure-execution]\n11\n"), "e2e key values 18/u64/18/11");
624 CHECK(strstr(h.buf, " id sha256:ffe7656dc91cf25a4aacfc005ffb643fa79f3fd8ce55d11420fd4982e9d0ff76\n") &&
625 strstr(h.buf, " id sha256:87c6fbd6d54c90525d018c65138314eb404cba4efa8d66d42dab17441e609ba6\n"),
626 "e2e realization ids exact");
627 CHECK(strstr(h.buf, "STRUCTURAL PASS") && strstr(h.buf, "REALIZATION PASS") &&
628 strstr(h.buf, "AUTHORITY PASS") && strstr(h.buf, "MACHINE PASS") &&
629 strstr(h.buf, "VERDICT PASS"), "e2e verify rows");
630 CHECK(strstr(h.buf, "\nno evidence\n") && strstr(h.buf, "\nno effect objects in session\n"),
631 "e2e no evidence / no effect objects");
632 free(am);
633 free(em);
634 h_close(&h);
635 }
636 if (script) fclose(script);
637 free(expected);
638
639 /* JSON: let x = 7; x + x; run _ -> three objects, result 14. */
640 Harness h;
641 e2e_open(&h, true);
642 int rc;
643 char *a = h_exec(&h, "let x: u64 = 7", &rc);
644 CHECK(rc == 0 && strstr(a, "\"value\":\"7\""), "json let");
645 free(a);
646 a = h_exec(&h, "x + x", &rc);
647 CHECK(rc == 0 && strstr(a, "\"value\":\"14\""), "json x + x");
648 free(a);
649 a = h_exec(&h, "run _", &rc);
650 static const char run_prefix[] = "{\"command\":\"run\",\"status\":\"ok\",\"class\":\"pure-execution\",\"result\":{\"result\":\"14\",\"realization\":\"sha256:";
651 CHECK(rc == 0 && strncmp(a, run_prefix, sizeof(run_prefix) - 1) == 0,
652 "json run _ = 14");
653 free(a);
654 size_t lines = 0;
655 for (size_t i = 0; i < h.len; i++) if (h.buf[i] == '\n') lines++;
656 CHECK(lines == 3, "json: 3 lines for 3 commands");
657
658 /* Fail-closed paths through real wiring. */
659 a = h_exec(&h, "alternatives x", &rc);
660 CHECK(rc == 1 && strstr(a, "only programs have alternative realizations in V1"), "alternatives on object refused");
661 free(a);
662 a = h_exec(&h, "effects x", &rc);
663 CHECK(rc == 1 && strstr(a, "is not an EFFECT object"), "effects on non-effect refused");
664 free(a);
665 a = h_exec(&h, "world", &rc);
666 CHECK(rc == 0 && strstr(a, "\"command\":\"world\""), "world unattached view");
667 free(a);
668 h_close(&h);
669
670 /* D2: run argument counts, fail closed (human mode, exact lines). */
671 e2e_open(&h, false);
672 expect(&h, "fn f(x: u64) -> u64 { x * 2 + 1 }", 0, "fn f: u64 -> u64\n", "d2 fn f");
673 expect(&h, "run f", 1, "error: run: program f takes 1 input; give it on the command line\n", "d2 run f (0 args)");
674 expect(&h, "run f 5 6", 1, "error: run: program f takes 1 input; got 2\n", "d2 run f 5 6");
675 expect(&h, "run f 5", 0, "[pure-execution]\n11\n", "d2 run f 5");
676 expect(&h, "let x: u64 = 7", 0, "7\n", "d2 let x");
677 expect(&h, "let y: u64 = 11", 0, "11\n", "d2 let y");
678 expect(&h, "x + y", 0, "18\n", "d2 x + y");
679 a = h_exec(&h, "realize _", &rc);
680 CHECK(rc == 0, "d2 realize _");
681 free(a);
682 expect(&h, "run _ 1", 1, "error: run: _ takes 2 operands; got 1 (give none, or all 2)\n", "d2 run _ 1");
683 expect(&h, "run _ 1 2 3", 1, "error: run: _ takes 2 operands; got 3 (give none, or all 2)\n", "d2 run _ 1 2 3");
684 expect(&h, "run _", 0, "[pure-execution]\n18\n", "d2 run _");
685 expect(&h, "run _ 1 2", 0, "[pure-execution]\n3\n", "d2 run _ 1 2");
686 expect(&h, "run _ 1 x", 1, "error: run: argument 2 'x' is not a u64\n", "d2 non-numeric arg");
687
688 /* D4: message shapes. */
689 expect(&h, "alternatives x", 1, "error: alternatives: only programs have alternative realizations in V1\n",
690 "d4 alternatives single prefix");
691 expect(&h, "effects x", 1, "error: effects: 'x' is not an EFFECT object\n", "d4 effects error prefix");
692 expect(&h, "authorize x", 1, "error: unknown: unknown command or invalid source line: 'authorize'\n", "d4 authorize");
693 expect(&h, "execute _", 1, "error: unknown: unknown command or invalid source line: 'execute'\n", "d4 execute");
694 expect(&h, "graph", 1, "error: graph: expected 1 argument, got 0\n", "d4 graph needs x");
695 h_close(&h);
696
697 /* D3: --script refuses directories. */
698 FILE *sf = NULL;
699 char serr[256];
700 CHECK(omega_tool_open_script("/", &sf, serr, sizeof(serr)) == -1 && sf == NULL &&
701 strcmp(serr, "error: --script: / is a directory") == 0, "d3 script / refused");
702 CHECK(omega_tool_open_script("tests/visor/sessions", &sf, serr, sizeof(serr)) == -1 &&
703 strstr(serr, "is a directory"), "d3 script dir refused");
704 CHECK(omega_tool_open_script("tests/visor/sessions/nope.omega-session", &sf, serr, sizeof(serr)) == -1 &&
705 strstr(serr, "cannot open"), "d3 missing script refused");
706 CHECK(omega_tool_open_script("tests/visor/sessions/e2e.omega-session", &sf, serr, sizeof(serr)) == 0 && sf,
707 "d3 regular script accepted");
708 if (sf) fclose(sf);
709}
710#endif
711
712int main(void) {
713 test_parser();
714 test_console_human();
715 test_console_stubs();
716 test_console_json();
717 test_script();
718#ifndef VISOR_CONSOLE_LANE_ONLY
719 test_e2e();
720#endif
721 printf("%s %d/%d\n", g_pass == g_total ? "PASS" : "FAIL", g_pass, g_total);
722 return g_pass == g_total ? 0 : 1;
723}