//! The interactive session (M1-PROJ-018, M1-UI-010, M1-UI-011, M1-UI-016). //! //! Loads a project the way `check` does, but does not stop at the first //! problem. M1-PROJ-018 lets interactive mode start when the rules file is //! broken, provided enough manifest and profile configuration remains to //! present the errors, and M1-UI-011 requires those errors be shown rather //! than turned into a refusal to start. //! //! After that the session is what edits act on. An edit re-evaluates through //! the same path as startup -- the draft is rendered to its canonical text and //! prepared exactly as a file would be -- so there is one definition of a //! valid ruleset rather than a second one for the editor (M1-UI-016). Nothing //! is reread from disk: the snapshot holds the bytes, and M1-PROJ-015 requires //! re-evaluation reuse them. //! //! Preparation is also what decides whether saving is available. Rules name //! field aliases, and aliases are defined by the profile, so a draft can only //! be judged against the manifest; there is no useful validation that stops //! short of it. const std = @import("std"); const diag = @import("../diag.zig"); const Editor = @import("Editor.zig"); const engine = @import("../engine.zig"); const Target = @import("../Target.zig"); const Output = @import("Output.zig"); const Project = @import("Project.zig"); const Snapshot = @import("../Snapshot.zig"); const state = @import("state.zig"); const Manifest = @import("../config/Manifest.zig"); const Stopwatch = @import("../util/time.zig").Stopwatch; const log = std.log.scoped(.session); /// M1-UI-014. `journal` is the complete text or nothing at all: a journal /// missing some of its transactions looks exactly like a complete one, so /// partial output is worse than none. pub const Preview = struct { /// Why there is no preview, when there is none. diagnostics: []const diag.Diagnostic, /// M1-UI-014: false means this is not what finalizing would write, because /// something else is still blocking it. final: bool, journal: ?[]const u8, }; const Session = @This(); /// Lives as long as the session: the snapshot, and the saved baseline. arena: std.mem.Allocator, base: std.Io.Dir, /// The prepared candidate for the current draft, kept so previewing and /// finalizing do not prepare it a second time. Null when the draft does /// not prepare, which is also when neither is available. candidate: ?engine.Candidate, editor: Editor, /// Everything one evaluation produces -- the candidate, the row results and /// the diagnostics the view hands out. Reset at the start of every /// evaluation, so a session that is edited all afternoon does not accumulate /// the results of every draft. evaluation_arena: std.heap.ArenaAllocator, /// The input root, reopened when finalizing. Held as a path rather than a /// descriptor: a session lasts as long as someone leaves the page open. input_path: []const u8, io: std.Io, manifest_path: []const u8, /// What was wrong with the rules file when the session opened. M1-UI-011 /// requires it be shown, and the first edit replaces the rules it is /// about, so it is dropped there rather than kept forever. opening: []const diag.Diagnostic, rules_file: []const u8, snap: Snapshot, view: state.View, /// Fails when the project itself cannot be loaded, and succeeds when only /// the rules are broken. /// /// M1-PROJ-018 lets interactive mode start with *rules-file* errors, on /// the condition that enough valid manifest and profile configuration /// remains to present and correct them. A missing manifest, an /// unreachable input root or a failed discovery leaves nothing to correct /// -- M1-UI-009 makes the manifest read-only here, so the interface could /// not fix any of them -- and starting a server to display one message /// would only hide the message. pub fn open( arena: std.mem.Allocator, io: std.Io, base: std.Io.Dir, manifest: *const Manifest, diags: *diag.List, ) error{ SessionFailed, OutOfMemory }!*Session { const opened = Project.open(arena, io, base, manifest, diags) catch return error.SessionFailed; const session = try arena.create(Session); session.* = .{ .io = io, .base = base, .arena = arena, .manifest_path = manifest.manifest_path, .rules_file = opened.rules_file, .input_path = opened.input_path, .snap = opened.snap, .candidate = null, .editor = if (opened.ruleset) |ruleset| try Editor.fromSaved(arena, ruleset) else .{ .rules = &.{}, .saved = null }, .opening = if (opened.ruleset != null) &.{} else // M1-PROJ-018: the rules file did not load, so the draft starts empty // and what was wrong with the file is all there is to say about it. try arena.dupe(diag.Diagnostic, diags.items.items), .evaluation_arena = std.heap.ArenaAllocator.init(arena), .view = .{ .file = opened.current_path, .rows = &.{}, .total_rows = 0, .history_pairs = opened.history_pairs, .history_ok = false, .configured = false, .dirty = false, .savable = false, .finalizable = false, .rules = &.{}, .fields = &.{}, .diagnostics = &.{}, }, }; try session.evaluate(); return session; } /// M1-UI-010: applies an edit and re-evaluates in the same breath, so the /// results a request returns are always the results for the draft it /// carried. There is no reload and no restart, and no window in which the /// two could disagree. pub fn apply(self: *Session, drafted: []const Editor.Rule) error{OutOfMemory}!void { // Whatever the file had wrong with it is no longer what is being // evaluated. self.opening = &.{}; _ = self.evaluation_arena.reset(.retain_capacity); const arena = self.evaluation_arena.allocator(); // The draft arrived in request scratch, which is reset between // requests; the session keeps its own copy. const kept = try arena.alloc(Editor.Rule, drafted.len); for (drafted, 0..) |rule, index| { kept[index] = .{ .name = try arena.dupe(u8, rule.name), .match = try arena.dupe(u8, rule.match), .target_account = try arena.dupe(u8, rule.target_account), .source_account = if (rule.source_account) |account| try arena.dupe(u8, account) else null, }; } self.editor.replace(kept); try self.evaluate(); } /// M1-UI-016: the one save action. Returns false with diagnostics when the /// draft is not savable; never writes a ruleset it has not validated. pub fn save(self: *Session) error{OutOfMemory}!bool { const arena = self.evaluation_arena.allocator(); var diags = diag.List.init(arena); if (!self.view.savable) { // The blocking diagnostics are already on show (M1-UI-011); this // says why the button did nothing. try diags.add(.{ .message = "the rules cannot be saved until they are valid" }); try self.appendDiagnostics(diags.items.items); return false; } // The exact bytes that were validated are the exact bytes written. const text = (try self.editor.canonical(arena)).?; const target: Target = .{ .root = self.base, .path = self.rules_file, .contents = text }; target.replace(arena, self.io, &diags) catch { try self.appendDiagnostics(diags.items.items); return false; }; // M1-PROJ-016: the saved state is now the candidate, decided here and // not by rereading the file. self.editor.markSaved(try self.arena.dupe(u8, text)); self.view.dirty = try self.editor.dirty(arena); // M1-UI-020 turns on this exact transition: the draft that was only // savable a moment ago is now also what is on disk. self.view.finalizable = self.finalizable(); return true; } /// Re-runs classification and history for the current draft. fn evaluate(self: *Session) !void { const arena = self.evaluation_arena.allocator(); var diags = diag.List.init(arena); try diags.items.appendSlice(arena, self.opening); self.candidate = null; self.view.rows = &.{}; self.view.fields = &.{}; self.view.total_rows = 0; self.view.history_ok = false; self.view.finalizable = false; self.view.rules = self.editor.rules; self.view.dirty = try self.editor.dirty(arena); self.view.configured = false; self.view.savable = false; // A profile that does not prepare leaves nothing to read the rows with. const loading: Stopwatch = .start(); if (engine.Profile.load( arena, .{ .path = self.manifest_path, .text = self.snap.manifest }, &diags, )) |profile| { log.debug("loaded the profile in {f}", .{loading.elapsed()}); try self.evaluateWith(arena, profile, &diags); } else |err| switch (err) { error.OutOfMemory => return error.OutOfMemory, error.EngineFailed => {}, } self.view.total_rows = self.view.rows.len; self.view.finalizable = self.finalizable(); self.view.diagnostics = diags.items.items; } /// With the profile prepared, the rows can be shown whether or not the /// draft's rules prepare. fn evaluateWith( self: *Session, arena: std.mem.Allocator, profile: engine.Profile, diags: *diag.List, ) error{OutOfMemory}!void { const csv_path = self.snap.current_path; const csv_text = self.snap.current_csv; const preparing: Stopwatch = .start(); const prepared = try self.prepare(arena, profile, diags); log.debug("prepared the rules in {f}", .{preparing.elapsed()}); const candidate = prepared orelse { // M1-RULE-013 makes every row unavailable, and M1-UI-007 still // shows them: they are what the rule being written is about. try self.show(engine.classifyUnevaluable(arena, profile, csv_path, csv_text, diags)); return; }; self.candidate = candidate; self.view.configured = true; // M1-UI-016: preparation is the whole of the save condition. Whether // rows match and whether history still reproduces are checked below // and reported, but they do not gate the save. self.view.savable = true; try self.show(engine.classify(arena, candidate, csv_path, csv_text, diags)); // M1-UI-012, M1-UI-013: historical problems are diagnostics, not a // refusal. const validating: Stopwatch = .start(); if (engine.validateHistory(arena, candidate, self.snap.history, diags)) |_| { self.view.history_ok = true; } else |_| { self.view.history_ok = false; } log.debug("validated {d} history pairs in {f}", .{ self.snap.history.len, validating.elapsed() }); } /// The candidate for the draft, or null when it does not prepare. fn prepare( self: *Session, arena: std.mem.Allocator, profile: engine.Profile, diags: *diag.List, ) error{OutOfMemory}!?engine.Candidate { // A draft with no rules has no canonical text, and so nothing that // could be prepared or saved (M1-UI-019). const text = try self.editor.canonical(arena) orelse { try diags.add(.{ .message = "at least one rule is required" }); return null; }; return engine.Candidate.withRules( arena, profile, .{ .path = self.rules_file, .text = text }, diags, ) catch |err| switch (err) { error.OutOfMemory => error.OutOfMemory, error.EngineFailed => null, }; } fn show(self: *Session, classified: engine.Error!engine.Classification) error{OutOfMemory}!void { if (classified) |rows| { self.view.rows = rows.rows; self.view.fields = rows.layout.columns; } else |err| switch (err) { error.OutOfMemory => return error.OutOfMemory, // Classification failed for the whole file; the reason is already // a diagnostic, and the session stays up to show it. error.EngineFailed => {}, } } /// Everything finalization needs that can be decided without writing. /// /// Whether the target path is free is deliberately not here. M1-JRN-014 /// and M1-PROJ-014 are settled by the atomic create in Target.zig, and /// asking first would only add a window in which the answer could change. /// M1-UI-019 needs no line of its own here: a draft without at least one /// valid rule never reaches history validation, so `history_ok` is still /// false and this returns false. fn finalizable(self: *const Session) bool { // M1-UI-020: the candidate must be the rules that were saved. if (self.view.dirty) return false; if (!self.view.history_ok) return false; // No row-count check: a CSV with only a header gives `convert` an // empty journal and exit zero, and M1-UI-021 asks for the same // behaviour here rather than a better one. for (self.view.rows) |result| { if (result.status != .one) return false; } return true; } /// M1-UI-014: the whole journal, or why there is none. /// /// This runs in the caller's arena rather than the session's, because a /// preview is asked for far less often than an edit is made and there is /// no reason to keep the last one alive until the next edit. pub fn preview(self: *const Session, arena: std.mem.Allocator) error{OutOfMemory}!Preview { var diags = diag.List.init(arena); const journal: ?[]const u8 = blk: { const candidate = self.candidate orelse { try diags.add(.{ .message = "the rules must be valid before a journal can be previewed", }); break :blk null; }; break :blk self.output(candidate).text(arena, &diags) catch |err| switch (err) { error.OutOfMemory => return error.OutOfMemory, error.OutputFailed => null, }; }; return .{ .journal = journal, .diagnostics = diags.items.items, // M1-UI-014: visibly non-final whenever this is not what writing // would produce. .final = journal != null and self.view.finalizable, }; } /// M1-UI-021: the same validation, atomic creation and no-overwrite /// behaviour as `convert`, because below the checks it is the same code. /// Returns false with diagnostics when anything refuses. pub fn finalize(self: *Session, arena: std.mem.Allocator) error{OutOfMemory}!bool { var diags = diag.List.init(arena); const candidate = self.candidate orelse return self.refuse( &diags, "the rules must be valid before a journal can be written", ); // M1-UI-020: the rules on disk are what a later `convert` would read, // so they are what must have produced this. Checked ahead of the // general gate below only so the message says what to do about it. if (self.view.dirty) return self.refuse( &diags, "the rules must be saved before a journal can be written", ); if (!self.view.finalizable) return self.refuse( &diags, "every row must match exactly one rule and every approved journal must still match", ); self.output(candidate).write(arena, &diags) catch |err| switch (err) { error.OutOfMemory => return error.OutOfMemory, error.OutputFailed => { try self.appendDiagnostics(diags.items.items); return false; }, }; return true; } fn output(self: *const Session, candidate: engine.Candidate) Output { return .{ .io = self.io, .base = self.base, .input_path = self.input_path, .candidate = candidate, .csv_path = self.snap.current_path, .csv_text = self.snap.current_csv, }; } fn refuse(self: *Session, diags: *diag.List, message: []const u8) !bool { try diags.add(.{ .message = message }); try self.appendDiagnostics(diags.items.items); return false; } fn appendDiagnostics(self: *Session, added: []const diag.Diagnostic) !void { const arena = self.evaluation_arena.allocator(); var combined: std.ArrayList(diag.Diagnostic) = .empty; try combined.appendSlice(arena, self.view.diagnostics); try combined.appendSlice(arena, added); self.view.diagnostics = combined.items; } // -- tests ------------------------------------------------------------------- const expect = std.testing.expect; const expectEqual = std.testing.expectEqual; const expectEqualStrings = std.testing.expectEqualStrings; const expectError = std.testing.expectError; const test_io = std.testing.io; const good_manifest = @embedFile("../testdata/ui/good_manifest.yaml"); const good_rules = @embedFile("../testdata/ui/good_rules.yaml"); const generatable_manifest = @embedFile("../testdata/ui/generatable_manifest.yaml"); const Fixture = struct { tmp: std.testing.TmpDir, fn init() !Fixture { var fixture: Fixture = .{ .tmp = std.testing.tmpDir(.{ .iterate = true }) }; try fixture.write("project.yaml", good_manifest); try fixture.write("rules.yaml", good_rules); try fixture.tmp.dir.createDirPath(test_io, "data"); try fixture.write("data/x.csv", "A\n1\n"); return fixture; } fn deinit(self: *Fixture) void { self.tmp.cleanup(); } fn write(self: *Fixture, path: []const u8, data: []const u8) !void { try self.tmp.dir.writeFile(test_io, .{ .sub_path = path, .data = data }); } /// The shared fixture's row cannot be converted -- its one column stands /// in for a date and an amount at once -- which is what several tests /// want. Generation needs a CSV that actually converts. fn makeGeneratable(self: *Fixture) !void { try self.write("project.yaml", generatable_manifest); try self.write("data/x.csv", "Datum,Betrag,Text\n2026-09-11,-1.50,markt\n"); } fn exists(self: *Fixture, path: []const u8) !bool { self.tmp.dir.access(test_io, path, .{}) catch return false; return true; } fn read(self: *Fixture, arena: std.mem.Allocator, path: []const u8) ![]const u8 { return self.tmp.dir.readFileAlloc(test_io, path, arena, .unlimited); } fn open(self: *Fixture, arena: *std.heap.ArenaAllocator) !*Session { const alloc = arena.allocator(); const diags = diag.List.create(alloc) catch return error.OutOfMemory; const manifest = try Manifest.loadFromFileVerified(arena.allocator(), test_io, self.tmp.dir, "project.yaml", diags); return Session.open(alloc, test_io, self.tmp.dir, &manifest, diags); } }; test "M1-PROJ-018: a broken rules file still opens a session" { var fixture = try Fixture.init(); defer fixture.deinit(); try fixture.write("rules.yaml", "schema:\n version: 1\nrules:\n r:\n match: 'a =='\n"); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); // The manifest and profile are fine, so the interface can open and show // the rule error for correction (M1-UI-011). const session = try fixture.open(&arena); try expect(!session.view.configured); try expect(session.view.diagnostics.len > 0); } test "M1-PROJ-018: a malformed manifest does not open a session" { var fixture = try Fixture.init(); defer fixture.deinit(); try fixture.write("project.yaml", "schema:\n version: 2\n"); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); try expectError(error.InvalidConfig, fixture.open(&arena)); } test "M1-PROJ-018: an unreachable input root does not open a session" { var fixture = try Fixture.init(); defer fixture.deinit(); try fixture.tmp.dir.deleteTree(test_io, "data"); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); try expectError(error.InvalidConfig, fixture.open(&arena)); } test "M1-PROJ-018: a failed discovery does not open a session" { var fixture = try Fixture.init(); defer fixture.deinit(); // Two unpaired CSVs: nothing the interface could choose between. try fixture.write("data/y.csv", "A\n2\n"); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); try expectError(error.SessionFailed, fixture.open(&arena)); } test "a healthy project opens and classifies" { var fixture = try Fixture.init(); defer fixture.deinit(); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const session = try fixture.open(&arena); try expect(session.view.configured); try std.testing.expectEqual(@as(usize, 1), session.view.total_rows); } test "the view names the column each alias resolved to" { var fixture = try Fixture.init(); defer fixture.deinit(); try fixture.makeGeneratable(); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var session = try fixture.open(&arena); try session.apply(&.{draft("markt", "text == \"markt\"", "expenses:food")}); const resolved = session.view.fields; try expectEqual(@as(usize, 3), resolved.len); try expectEqualStrings("text", resolved[2].field.alias); try expectEqualStrings("Text", resolved[2].field.selector.header); try expectEqual(@as(usize, 2), resolved[2].index); } /// What M1-UI-007 asks of a draft that cannot be evaluated: every row, each /// unavailable, with the profile's fields still there to describe them. fn expectEveryRowUnavailable(session: *const Session) !void { try expect(!session.view.configured); try expectEqual(@as(usize, 1), session.view.total_rows); try expectEqual(engine.RowStatus.unavailable, session.view.rows[0].status); try expectEqual(@as(usize, 0), session.view.rows[0].matches.len); try expectEqual(@as(usize, 3), session.view.fields.len); } test "M1-UI-007: a rule still being written leaves every row shown, unavailable" { var fixture = try Fixture.init(); defer fixture.deinit(); try fixture.makeGeneratable(); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); // A rule just added and named, with nothing else filled in yet. var session = try fixture.open(&arena); try session.apply(&.{draft("markt", "", "")}); try expectEveryRowUnavailable(session); // The blocking diagnostic is shown once, not once per row. var found = false; for (session.view.diagnostics) |shown| { try expect(shown.physical_row == null); if (std.mem.find(u8, shown.message, "expected a comparison") != null) found = true; } try expect(found); } test "M1-UI-007: a draft with no rules leaves every row shown, unavailable" { var fixture = try Fixture.init(); defer fixture.deinit(); try fixture.makeGeneratable(); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var session = try fixture.open(&arena); try session.apply(&.{}); try expectEveryRowUnavailable(session); } fn draft(name: []const u8, match: []const u8, target: []const u8) Editor.Rule { return .{ .name = name, .match = match, .target_account = target, .source_account = null }; } test "M1-UI-010: an edit re-evaluates without reloading anything" { var fixture = try Fixture.init(); defer fixture.deinit(); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var session = try fixture.open(&arena); try expectEqual(engine.RowStatus.one, session.view.rows[0].status); // The same session, the same snapshot, a different answer. try session.apply(&.{draft("r", "a == \"nope\"", "e:f")}); try expectEqual(engine.RowStatus.zero, session.view.rows[0].status); try expect(session.view.configured); try session.apply(&.{ draft("r", "a == \"1\"", "e:f"), draft("s", "a == \"1\"", "e:g") }); try expectEqual(engine.RowStatus.multiple, session.view.rows[0].status); try expectEqual(@as(usize, 2), session.view.rows[0].matches.len); } test "M1-UI-016: a broken draft blocks saving and a good one recovers" { var fixture = try Fixture.init(); defer fixture.deinit(); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var session = try fixture.open(&arena); try session.apply(&.{draft("r", "a ==", "e:f")}); try expect(!session.view.configured); try expect(!session.view.savable); try expect(session.view.diagnostics.len > 0); // M1-UI-011: the session stays up, and the error goes away when fixed. try session.apply(&.{draft("r", "a == \"1\"", "e:f")}); try expect(session.view.savable); try expectEqual(@as(usize, 0), session.view.diagnostics.len); } test "M1-UI-016: an unknown field alias is refused, because rules name profile aliases" { var fixture = try Fixture.init(); defer fixture.deinit(); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var session = try fixture.open(&arena); try session.apply(&.{draft("r", "nosuchfield == \"1\"", "e:f")}); try expect(!session.view.savable); } test "M1-UI-019: a draft with no rules cannot be saved" { var fixture = try Fixture.init(); defer fixture.deinit(); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var session = try fixture.open(&arena); try session.apply(&.{}); try expect(!session.view.savable); try expect(!try session.save()); // And the file still holds what it held. try expectEqualStrings(good_rules, try fixture.read(arena.allocator(), "rules.yaml")); } test "M1-UI-016: an invalid draft is never written" { var fixture = try Fixture.init(); defer fixture.deinit(); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var session = try fixture.open(&arena); try session.apply(&.{draft("r", "a ==", "e:f")}); try expect(!try session.save()); try expectEqualStrings(good_rules, try fixture.read(arena.allocator(), "rules.yaml")); try expect(session.view.diagnostics.len > 0); } test "M1-UI-015: an edit is dirty until it is saved" { var fixture = try Fixture.init(); defer fixture.deinit(); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var session = try fixture.open(&arena); try expect(!session.view.dirty); try session.apply(&.{draft("r", "a == \"1\"", "e:changed")}); try expect(session.view.dirty); try expect(try session.save()); try expect(!session.view.dirty); } test "M1-UI-017: saving rewrites the rules file and nothing else" { var fixture = try Fixture.init(); defer fixture.deinit(); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); var session = try fixture.open(&arena); try session.apply(&.{draft("r", "a == \"1\"", "e:changed")}); try expect(try session.save()); const written = try fixture.read(alloc, "rules.yaml"); try expect(std.mem.find(u8, written, "e:changed") != null); // Canonical, so it loads back (M1-UI-018). try expect(std.mem.startsWith(u8, written, "schema:\n version: 1\n")); // M1-UI-017: the manifest, the CSVs and the journals are untouched. try expectEqualStrings(good_manifest, try fixture.read(alloc, "project.yaml")); try expectEqualStrings("A\n1\n", try fixture.read(alloc, "data/x.csv")); } test "M1-PROJ-016: after a save the session does not go back to the file" { var fixture = try Fixture.init(); defer fixture.deinit(); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var session = try fixture.open(&arena); try session.apply(&.{draft("r", "a == \"1\"", "e:changed")}); try expect(try session.save()); // Someone else rewrites the file. The session's saved state was decided // when it wrote, so this changes nothing about it. try fixture.write("rules.yaml", "not even yaml: ["); try expect(!session.view.dirty); try session.apply(&.{draft("r", "a == \"1\"", "e:changed")}); try expect(!session.view.dirty); try expect(session.view.savable); } test "M1-AC-008: a valid edit leaving a row unmatched still saves" { var fixture = try Fixture.init(); defer fixture.deinit(); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); var session = try fixture.open(&arena); try session.apply(&.{draft("r", "a == \"nothing matches this\"", "e:f")}); // The rules are valid, so the save goes through (M1-UI-016: row matching // is not a save prerequisite) ... try expect(session.view.savable); try expect(try session.save()); try expect(std.mem.find(u8, try fixture.read(alloc, "rules.yaml"), "nothing matches") != null); // ... while the row is unmatched, which is what keeps finalization // unavailable and makes non-interactive checking fail. try expectEqual(engine.RowStatus.zero, session.view.rows[0].status); } test "M1-UI-011: the rules file's own errors show at startup and go on the first edit" { var fixture = try Fixture.init(); defer fixture.deinit(); // A rules file that does not load at all, so the draft starts empty and // the only account of what is wrong is the file's own error. try fixture.write("rules.yaml", "schema:\n version: 1\nrules:\n r: [1, 2\n"); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const session = try fixture.open(&arena); try expect(!session.view.configured); try expect(std.mem.find( u8, session.view.diagnostics[0].message, "flow sequences are not supported", ) != null); // The edit replaces the rules that error was about, so keeping it would // leave the interface complaining about text that is gone. try session.apply(&.{draft("r", "a == \"1\"", "e:f")}); try expect(session.view.configured); try expectEqual(@as(usize, 0), session.view.diagnostics.len); } test "M1-UI-010: editing all afternoon does not accumulate evaluations" { var fixture = try Fixture.init(); defer fixture.deinit(); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var session = try fixture.open(&arena); try session.apply(&.{draft("r", "a == \"1\"", "e:f")}); try session.apply(&.{draft("r", "a == \"1\"", "e:f")}); const settled = session.evaluation_arena.queryCapacity(); // Each evaluation reuses the previous one's memory rather than adding to // it; a session is edited far more often than it is opened. var round: usize = 0; while (round < 50) : (round += 1) { try session.apply(&.{draft("r", "a == \"1\"", "e:f")}); } try expectEqual(settled, session.evaluation_arena.queryCapacity()); } // -- preview and finalization ------------------------------------------------ test "M1-UI-014: a preview appears when every row can be generated" { var fixture = try Fixture.init(); defer fixture.deinit(); try fixture.makeGeneratable(); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); var session = try fixture.open(&arena); try session.apply(&.{draft("markt", "text == \"markt\"", "expenses:food")}); const previewed = try session.preview(alloc); try expectEqualStrings(@embedFile("../testdata/ui/a_preview_appears_when_every_row_can_be_generated.journal"), previewed.journal.?); } test "M1-UI-014: an ungeneratable row gives the reason, not partial output" { var fixture = try Fixture.init(); defer fixture.deinit(); try fixture.makeGeneratable(); try fixture.write("data/x.csv", "Datum,Betrag,Text\n2026-09-11,-1.50,markt\n2026-09-12,-2.50,bahn\n"); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); // The first row generates and the second does not. Showing the first // alone would look like a complete journal that is missing a transaction. var session = try fixture.open(&arena); try session.apply(&.{draft("markt", "text == \"markt\"", "expenses:food")}); const previewed = try session.preview(alloc); try expectEqual(@as(?[]const u8, null), previewed.journal); try expect(previewed.diagnostics.len > 0); } test "M1-UI-014: a preview is marked non-final while anything else blocks" { var fixture = try Fixture.init(); defer fixture.deinit(); try fixture.makeGeneratable(); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); var session = try fixture.open(&arena); try session.apply(&.{draft("markt", "text == \"markt\"", "expenses:food")}); // Everything generates, but the draft is unsaved, so what is on screen is // not what finalizing would produce from the saved rules. const before = try session.preview(alloc); try expect(before.journal != null); try expect(!before.final); try expect(!session.view.finalizable); try expect(try session.save()); const after = try session.preview(alloc); try expect(after.final); try expect(session.view.finalizable); // Saving changed nothing about the journal itself, only whether it may // be written. try expectEqualStrings(before.journal.?, after.journal.?); } test "M1-UI-014: previewing writes nothing" { var fixture = try Fixture.init(); defer fixture.deinit(); try fixture.makeGeneratable(); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); var session = try fixture.open(&arena); try session.apply(&.{draft("markt", "text == \"markt\"", "expenses:food")}); _ = try session.preview(alloc); // The target path M1-UI-014 names is x.csv.journal, and nothing has // appeared beside the CSV. var data = try fixture.tmp.dir.openDir(test_io, "data", .{ .iterate = true }); defer data.close(test_io); var count: usize = 0; var entries = data.iterate(); while (try entries.next(test_io)) |_| count += 1; try expectEqual(@as(usize, 1), count); } test "M1-AC-009: an unsaved edit blocks finalization until it is saved" { var fixture = try Fixture.init(); defer fixture.deinit(); try fixture.makeGeneratable(); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); var session = try fixture.open(&arena); try session.apply(&.{draft("markt", "text == \"markt\"", "expenses:food")}); // Every behavioural check passes; the only thing wrong is that the rules // on disk are not these rules (M1-UI-020). try expect(session.view.dirty); try expect(!session.view.finalizable); try expect(!try session.finalize(alloc)); try expect(!try fixture.exists("data/x.csv.journal")); // And the message says what to do about it, rather than listing every // check that did pass. const last = session.view.diagnostics[session.view.diagnostics.len - 1].message; try expect(std.mem.find(u8, last, "saved") != null); try expect(try session.save()); try expect(try session.finalize(alloc)); try expect(try fixture.exists("data/x.csv.journal")); } test "M1-UI-021: finalization refuses to overwrite and leaves the file alone" { var fixture = try Fixture.init(); defer fixture.deinit(); try fixture.makeGeneratable(); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); var session = try fixture.open(&arena); try session.apply(&.{draft("markt", "text == \"markt\"", "expenses:food")}); try expect(try session.save()); // M1-PROJ-014: the target appears after discovery, which is the case the // atomic create exists for. Everything the session knows says it may // write, and the kernel says otherwise. try expect(session.view.finalizable); try fixture.write("data/x.csv.journal", "someone else's work\n"); try expect(!try session.finalize(alloc)); try expectEqualStrings("someone else's work\n", try fixture.read(alloc, "data/x.csv.journal")); } test "M1-UI-021: finalization refuses a row that matches no rule" { var fixture = try Fixture.init(); defer fixture.deinit(); try fixture.makeGeneratable(); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); var session = try fixture.open(&arena); try session.apply(&.{draft("nothing", "text == \"absent\"", "expenses:food")}); try expect(try session.save()); // The same rule `convert` applies: every row, exactly one match. try expect(!session.view.finalizable); try expect(!try session.finalize(alloc)); try expect(!try fixture.exists("data/x.csv.journal")); } test "M1-UI-019: an empty draft is not finalizable" { var fixture = try Fixture.init(); defer fixture.deinit(); try fixture.makeGeneratable(); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); var session = try fixture.open(&arena); try session.apply(&.{}); try expect(!session.view.finalizable); try expect(!try session.finalize(alloc)); } test "M1-UI-021: what finalization writes is what the preview showed" { var fixture = try Fixture.init(); defer fixture.deinit(); try fixture.makeGeneratable(); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); var session = try fixture.open(&arena); try session.apply(&.{draft("markt", "text == \"markt\"", "expenses:food")}); try expect(try session.save()); const previewed = try session.preview(alloc); try expect(try session.finalize(alloc)); try expectEqualStrings(previewed.journal.?, try fixture.read(alloc, "data/x.csv.journal")); } test "M1-UI-021: a historical journal that no longer matches blocks finalization" { var fixture = try Fixture.init(); defer fixture.deinit(); try fixture.makeGeneratable(); try fixture.write("data/old.csv", "Datum,Betrag,Text\n2026-01-02,-3.00,markt\n"); try fixture.write("data/old.csv.journal", "2026-01-02 something else\n a:b -3.00 EUR\n c:d\n"); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); var session = try fixture.open(&arena); try session.apply(&.{draft("markt", "text == \"markt\"", "expenses:food")}); try expect(try session.save()); // The current import is fine on its own; the approved journal is not // reproduced, which is the whole point of M1-HIST-001. try expect(!session.view.history_ok); try expect(!session.view.finalizable); try expect(!try session.finalize(alloc)); try expect(!try fixture.exists("data/x.csv.journal")); } test "M1-UI-021: a CSV with only a header finalizes to an empty journal, as convert does" { var fixture = try Fixture.init(); defer fixture.deinit(); try fixture.makeGeneratable(); try fixture.write("data/x.csv", "Datum,Betrag,Text\n"); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); var session = try fixture.open(&arena); try session.apply(&.{draft("markt", "text == \"markt\"", "expenses:food")}); try expect(try session.save()); // `convert` writes an empty journal here and exits zero. M1-UI-021 asks // for the same behaviour, not a stricter one. try expect(session.view.finalizable); try expect(try session.finalize(alloc)); try expectEqualStrings("", try fixture.read(alloc, "data/x.csv.journal")); } test "M1-UI-014: with unusable rules there is no preview, and the reason says so" { var fixture = try Fixture.init(); defer fixture.deinit(); try fixture.makeGeneratable(); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); var session = try fixture.open(&arena); try session.apply(&.{draft("markt", "text ==", "expenses:food")}); const previewed = try session.preview(alloc); try expectEqual(@as(?[]const u8, null), previewed.journal); try expect(!previewed.final); try expect(std.mem.find(u8, previewed.diagnostics[0].message, "valid") != null); }