ident
All repositories: gitoria
8.5 KB
// Cross-compat proof driver (mission 066, acceptance gate for D12):// exercises the Zig mpackdb engine against db files written by the REAL// JS mpackdb v1.0.7 — and writes db files the JS side then verifies.//// Companion JS scripts: testdata/compat/*.mjs (see their headers for setup).//// Usage:// zig run compat_driver.zig -- verify_a <dbfile> # read a JS-written db// zig run compat_driver.zig -- mutate_a <dbfile> # mutate it (JS re-verifies)// zig run compat_driver.zig -- write_b <dbfile> # write a fresh db for JS// zig run compat_driver.zig -- verify_b2 <dbfile> # verify JS-side mutationsconst std = @import("std");const msgpack = @import("msgpack.zig");const engine = @import("engine.zig");const P = std.debug.print;fn keyStr(s: []const u8) engine.Key {return .{ .str = s };}fn keyNum(n: f64) engine.Key {return .{ .num = n };}fn printValue(v: msgpack.Value) void {switch (v) {.nil => P("null", .{}),.undef => P("undefined", .{}),.boolean => |b| P("{}", .{b}),.number => |n| {var buf: [40]u8 = undefined;P("{s}", .{engine.jsNumFmt(&buf, n)});},.str => |s| P("\"{s}\"", .{s}),.bin => |b| P("<bin {d}>", .{b.len}),.array => |items| {P("[", .{});for (items, 0..) |it, i| {if (i > 0) P(",", .{});printValue(it);}P("]", .{});},.map => |entries| {P("{{", .{});for (entries, 0..) |e, i| {if (i > 0) P(",", .{});printValue(e.key);P(":", .{});printValue(e.value);}P("}}", .{});},.ext => |e| {if (v.timestampMs()) |ms| {var buf: [40]u8 = undefined;P("Date({s})", .{engine.jsNumFmt(&buf, ms)});} else {P("<ext {d} len {d}>", .{ e.type, e.data.len });}},}}fn printRecords(label: []const u8, db: *engine.Db, arena: std.mem.Allocator, locs: []const engine.Loc) !void {P("{s}: {d} record(s)\n", .{ label, locs.len });for (locs) |loc| {const rec = try db.readAt(arena, loc);P(" ", .{});printValue(rec);P("\n", .{});}}pub fn main(init: std.process.Init) !void {const gpa = init.gpa;var arena_state = std.heap.ArenaAllocator.init(gpa);defer arena_state.deinit();const arena = arena_state.allocator();var iter = std.process.Args.Iterator.init(init.minimal.args);_ = iter.next();const cmd = iter.next() orelse {P("usage: compat_driver <verify_a|mutate_a|write_b|verify_b2> <dbfile>\n", .{});return;};const dbfile = iter.next() orelse {P("missing dbfile\n", .{});return;};if (std.mem.eql(u8, cmd, "verify_a")) {// Open the JS-written db WITHOUT compaction so we read its files// (data, indexes, idxstate, meta) exactly as JS persisted them.const db = try engine.Db.open(gpa, dbfile, .{.primary_key = "*id",.indexes = &.{ "email", "*age" },.compact = false,});defer db.close();var locs: std.ArrayList(engine.Loc) = .empty;try db.findPkLocs(arena, keyNum(1), &locs);try printRecords("pk id=1", db, arena, locs.items);var locs2: std.ArrayList(engine.Loc) = .empty;try db.findIndexLocs(arena, "email", keyStr("[email protected]"), &locs2);try printRecords("[email protected]", db, arena, locs2.items);// Carol (id=2) was deleted by JS — her email must yield nothingvar locs2b: std.ArrayList(engine.Loc) = .empty;try db.findIndexLocs(arena, "email", keyStr("[email protected]"), &locs2b);P("[email protected] (JS-deleted): {d} record(s)\n", .{locs2b.items.len});var locs3: std.ArrayList(engine.Loc) = .empty;try db.findRangeLocs(arena, "age", keyNum(28), keyNum(40), &locs3);try printRecords("age in [28,40]", db, arena, locs3.items);// deleted record must be invisiblevar locs4: std.ArrayList(engine.Loc) = .empty;try db.findPkLocs(arena, keyNum(2), &locs4);P("pk id=2 (JS-deleted): {d} record(s)\n", .{locs4.items.len});// full scan countvar scanner = engine.Db.Scanner.init(db, arena, true);defer scanner.deinit();var count: usize = 0;while (try scanner.next(arena)) |_| count += 1;P("total live records: {d}\n", .{count});P("nextId: ", .{});var buf: [40]u8 = undefined;P("{s}\n", .{engine.jsNumFmt(&buf, db.next_id)});} else if (std.mem.eql(u8, cmd, "mutate_a")) {const db = try engine.Db.open(gpa, dbfile, .{.primary_key = "*id",.indexes = &.{ "email", "*age" },.compact = false,});defer db.close();// 1) insert a new record — auto id must continue the JS sequencevar rec = engine.Db.MutableRecord{};try rec.set(arena, "name", .{ .str = "Ziggy Stardust" });try rec.set(arena, "email", .{ .str = "[email protected]" });try rec.set(arena, "age", .{ .number = 99 });var tags = [_]msgpack.Value{ .{ .str = "zig" }, .{ .str = "native" } };try rec.set(arena, "tags", .{ .array = &tags });const res = try db.insert(arena, rec.toValue(), false);var buf: [40]u8 = undefined;P("inserted id={s}\n", .{engine.jsNumFmt(&buf, res.pk_num)});// 2) update id=0: bump age to 31, KEEP the Date field byte-identicalvar locs: std.ArrayList(engine.Loc) = .empty;try db.findPkLocs(arena, keyNum(0), &locs);if (locs.items.len != 1) return error.UpdateTargetMissing;const old = try db.readAt(arena, locs.items[0]);var upd = engine.Db.MutableRecord{};for (old.map) |e| try upd.entries.append(arena, e);try upd.set(arena, "age", .{ .number = 31 });const n_upd = try db.updatePk(arena, keyNum(0), upd.toValue());P("updated id=0: {d} record(s), age -> 31\n", .{n_upd});// 3) delete id=3var deleted: std.ArrayList(msgpack.Value) = .empty;const n_del = try db.deletePk(arena, keyNum(3), &deleted);P("deleted id=3: {d} record(s)\n", .{n_del});} else if (std.mem.eql(u8, cmd, "write_b")) {// Fresh Zig-written db with uuid pk + secondary indexes for JS to read.const db = try engine.Db.open(gpa, dbfile, .{.primary_key = "@uuid",.indexes = &.{ "city", "*score" },});defer db.close();const cities = [_][]const u8{ "Klagenfurt", "Helsinki", "Wörgl" };const names = [_][]const u8{ "Ada", "Böb", "Chidi" };const scores = [_]f64{ 12.5, -3, 4294967296 };for (cities, names, scores) |city, name, score| {var rec = engine.Db.MutableRecord{};try rec.set(arena, "name", .{ .str = name });try rec.set(arena, "city", .{ .str = city });try rec.set(arena, "score", .{ .number = score });var nested_entries = [_]msgpack.Entry{.{ .key = .{ .str = "active" }, .value = .{ .boolean = true } },.{ .key = .{ .str = "note" }, .value = .nil },};try rec.set(arena, "meta", .{ .map = &nested_entries });const res = try db.insert(arena, rec.toValue(), false);P("inserted uuid={s} ({s})\n", .{ res.pk_str, name });}} else if (std.mem.eql(u8, cmd, "verify_b2")) {const db = try engine.Db.open(gpa, dbfile, .{.primary_key = "@uuid",.indexes = &.{ "city", "*score" },.compact = false,});defer db.close();// JS inserted a marker record with city "Byrodin"var locs: std.ArrayList(engine.Loc) = .empty;try db.findIndexLocs(arena, "city", keyStr("Byrodin"), &locs);try printRecords("city=Byrodin (JS-inserted)", db, arena, locs.items);// JS deleted "Ada" — city Klagenfurt must be gonevar locs2: std.ArrayList(engine.Loc) = .empty;try db.findIndexLocs(arena, "city", keyStr("Klagenfurt"), &locs2);P("city=Klagenfurt (JS-deleted): {d} record(s)\n", .{locs2.items.len});var scanner = engine.Db.Scanner.init(db, arena, true);defer scanner.deinit();var count: usize = 0;while (try scanner.next(arena)) |_| count += 1;P("total live records: {d}\n", .{count});} else {P("unknown command {s}\n", .{cmd});}}
Branches
Latest commits
- 24dd414eident#34: the shared lead, message and notice looks are statics in components/styles.hl, imported by the components (lib/looks.hl gone)mre
- 03ed26c7ident#34: access.hl: the remaining lead classes droppedmre
- 1d066c42ident#34: access.hl drops its unstyled lead class (the shared lead rule would otherwise mute it)mre
- 1e1dd7b4ident#34: the access page keeps its lead class string (the page has no ident-card, so its lead line was never muted; a shared rule would change the look)mre
- 89ac6553ident#34: the lead, message and notice lines are named with Style.pLead / pMessage / pNotice (shared bodies in lib/looks.hl) instead of class stringsmre
- e84be3deident#34: browser gate lists the Edit/Delete buttons by namemre
- fb7111faident#34: access page: unstyled test-hook classes (idrow, never, who, whoname, appname, neverrevoke) dropped; the tests use element/name selectorsmre
- 9f2ad803ident#34: notification switches: push/email by name=..., locked by aria-disabled, the switch look is the role=switch button (no switch/push/email/locked classes)mre
- e0f0ddaaident#34: inbox: the Mark read/unread button is styled as the button of its note-foot, the icon is found as the row's img (no toggle/icon classes)mre
- 16b49756ident#34: unused row classes (choice, usedrow, kind, shortid) droppedmre
- f9d6fb38ident#34: apps and identity rows: the Edit/New secret/Delete buttons carry name=..., the API key is found by its element; unused row classes (app, identity, shortid) droppedmre
- b323265aMerge branch 'f5-styles' into mainIdent
- 9ced53c3|||PR ident#33 ident#35 every component's styles live in its own Style block; styles.hl keeps the page-wide rest; the account bar is phone first (min-width: 60rem block); the avatar preview is hidden with the hidden attribute (ident#34 in part)mre
- f28333e4ident#33 ident#35: styles.hl keeps only the accent, the page column and the shared patterns; the account bar is written phone first (base rule + one min-width: 60rem block) instead of a max-width overridemre
- 5b4541fcident#33: the selector sign-in frame's rules move into embed.hl's Stylemre
- 00235f21ident#33: the per-app notification switches move into appsettings.hl's Stylemre
- 2249945cident#33: the inbox and connection list rules move into inbox.hl's Stylemre
- f2d9f79fident#33: the apps list and the secret box move into apps.hl's Stylemre
- 8370b748ident#33: the app request box moves into home.hl's Stylemre
- 40bf3b97ident#33: the 404 page's home link rule moves into notfound.hlmre