interplane.Get Omega
Read in your language

hop

Hop: a small native macOS launcher. Clipboard history, find, on-device dictation and your coding agent behind one key.

Brought by drakestapleton

branch main Branches Tags History

hop / Omega / tests / HopFactCheck.swift

172 lines · 10390 bytes Raw Blame History
1// Native caller check for the hopfact core (Omega/hopfact.om, contract in Omega/README.md).
2// Calls omega_hopfact through the generated C binding exactly as Hop's Swift host does:
3// two reused [UInt64](256) buffers, no per-call allocation, status != 0 treated as INVALID.
4// Prints PASS/FAIL per check; the last line is HOPFACT-CHECK PASS or FAIL n.
5import Foundation
6
7let magic: UInt64 = 1213157446
8let none = UInt64.max // -1: absent / unknown
9let now: UInt64 = 1_000_000_000_000
10
11var fails = 0
12func check(_ name: String, _ ok: Bool, _ detail: @autoclosure () -> String = "") {
13 if ok { print("PASS \(name)") } else { fails += 1; print("FAIL \(name) \(detail())") }
14}
15
16struct Row { var id: UInt64; var kind: UInt64; var state: UInt64; var count: UInt64; var valid: UInt64 = 1 }
17
18func snapshot(_ rows: [Row], link: UInt64 = 1, mask: UInt64 = 1, gen: UInt64 = now - 5000, n: UInt64? = nil) -> [UInt64] {
19 var w = [UInt64](repeating: 0, count: 256)
20 w[0] = magic; w[1] = 1; w[2] = n ?? UInt64(rows.count); w[3] = now; w[4] = gen; w[5] = link; w[6] = mask
21 for (r, x) in rows.enumerated() {
22 let b = 8 + 5 * r
23 w[b] = x.id; w[b + 1] = x.kind; w[b + 2] = x.state; w[b + 3] = x.count; w[b + 4] = x.valid
24 }
25 return w
26}
27
28var res = [UInt64](repeating: 0xAAAA_AAAA_AAAA_AAAA, count: 256)
29
30@discardableResult
31func call(_ inp: [UInt64], inLen: UInt64 = 256) -> (status: UInt32, verdict: UInt64) {
32 var v: UInt64 = 0xDEAD
33 let st = inp.withUnsafeBytes { i in
34 res.withUnsafeMutableBytes { o in
35 omega_hopfact(&v, i.baseAddress, inLen, o.baseAddress, 256)
36 }
37 }
38 return (st, v)
39}
40
41// Base (what Hop sends): two stations (running, idle) and the six reported review/merge groups.
42let base: [Row] = [
43 Row(id: 100, kind: 1, state: 4, count: 3),
44 Row(id: 101, kind: 1, state: 1, count: 0),
45 Row(id: 500, kind: 2, state: 1, count: 1), // review bay
46 Row(id: 501, kind: 3, state: 1, count: 0), // review quota
47 Row(id: 502, kind: 4, state: 1, count: 0), // review decision
48 Row(id: 503, kind: 5, state: 1, count: 0), // merge ready
49 Row(id: 504, kind: 6, state: 1, count: 0), // merge owner
50 Row(id: 505, kind: 7, state: 1, count: 0), // merge decision
51]
52
53var r = call(snapshot(base))
54check("working: status 0, verdict 7 WORKING reason 60", r.status == 0 && r.verdict == 7 && res[2] == 7 && res[3] == 60, "\(r) \(res[2]) \(res[3])")
55check("working: header magic/version/nrows/age", res[0] == magic && res[1] == 1 && res[5] == 8 && res[6] == 5000)
56check("working: presence mask kinds 1..7", res[7] == 0xFE, "\(res[7])")
57check("working: category counts WORK 2, IDLE 6", res[16] == 2 && res[20] == 6 && res[8...23].reduce(0, +) == 8)
58check("working: kind totals; unsent kinds 8..15 are -1, not 0",
59 res[25] == 3 && res[26] == 1 && res[27...31].allSatisfy { $0 == 0 } && res[32...39].allSatisfy { $0 == none })
60check("working: agents, quota unknown (-1); attention 0; fresh admission 1", res[40] == none && res[41] == none && res[42] == none && res[43] == 0 && res[45] == 1)
61check("working: order 100,500,101,501..505", Array(res[48..<56]) == [100, 500, 101, 501, 502, 503, 504, 505] && res[56] == 0, "\(Array(res[48..<56]))")
62check("working: per-row categories", Array(res[96..<104]) == [8, 12, 8, 12, 12, 12, 12, 12])
63
64var clear = base; clear[0].state = 1; clear[2].count = 0
65r = call(snapshot(clear))
66check("clear: verdict 8 reason 0 (no reported blockers in declared coverage)", r.verdict == 8 && res[3] == 0 && res[20] == 8)
67
68var groups = base
69groups[4].count = 2 // review decision
70groups[5].count = 1 // merge ready
71groups += [
72 Row(id: 506, kind: 10, state: 1, count: 4), // unrecognized group: kept visible
73 Row(id: 507, kind: 9, state: 1, count: 7), // superseded: neutral
74 Row(id: 508, kind: 15, state: 1, count: 2), // merge triage
75]
76r = call(snapshot(groups))
77check("triage: verdict 6 ATTENTION reason 42 (decision first)", r.verdict == 6 && res[3] == 42, "\(r) \(res[3])")
78check("triage: order decision, triage, ready, work, other, neutral, idle",
79 Array(res[48..<59]) == [502, 508, 503, 100, 500, 506, 507, 101, 501, 504, 505], "\(Array(res[48..<59]))")
80check("triage: attention rows 4, TRIAGE 1, OTHER 1, NEUTRAL 1", res[43] == 4 && res[13] == 1 && res[18] == 1 && res[19] == 1)
81check("triage: totals decision 2, ready 1, superseded 7, other 4, triage 2, elsewhere -1",
82 res[28] == 2 && res[29] == 1 && res[33] == 7 && res[34] == 4 && res[39] == 2 && res[32] == none)
83
84let opt = base + [Row(id: 200, kind: 11, state: 1, count: 30), Row(id: 300, kind: 12, state: 1, count: 650)]
85r = call(snapshot(opt, mask: 3))
86check("optional kinds: agent working 1/1, min quota 650, verdict 7", r.verdict == 7 && res[40] == 1 && res[41] == 1 && res[42] == 650)
87
88var unk = base; unk[0].state = 0
89r = call(snapshot(unk))
90check("negative: unknown station state is UNKNOWN, never idle/clear (verdict 6 reason 41)", r.verdict == 6 && res[3] == 41 && res[96] == 1 && res[48] == 100, "\(r) \(res[3]) \(res[96])")
91var ug = clear; ug[3].state = 0
92r = call(snapshot(ug))
93check("negative: unknown group state is UNKNOWN (verdict 6 reason 41)", r.verdict == 6 && res[3] == 41 && res[99] == 1)
94var dec = clear; dec[1].valid = 0
95r = call(snapshot(dec))
96check("negative: host decode failure (valid 0) is UNKNOWN", r.verdict == 6 && res[97] == 1)
97
98// Fill the output, then refuse: an INVALID result must leave only the header.
99call(snapshot(groups))
100r = call(snapshot(base, n: 49))
101check("negative: overcapacity nrows 49 -> INVALID reason 3 at word 2", r.status == 0 && r.verdict == 1 && res[3] == 3 && res[4] == 2, "\(r) \(res[3]) \(res[4])")
102check("negative: INVALID output zeroed past header", res[0] == magic && res[2] == 1 && res[5...].allSatisfy { $0 == 0 })
103var neg = base; neg[0].count = UInt64(bitPattern: -1)
104r = call(snapshot(neg))
105check("negative: count -1 -> INVALID reason 4 at word 11", r.verdict == 1 && res[3] == 4 && res[4] == 11, "\(res[3]) \(res[4])")
106var big = base; big[0].count = 1_000_001
107r = call(snapshot(big))
108check("negative: station count over 1e6 -> INVALID reason 5 at word 11", r.verdict == 1 && res[3] == 5 && res[4] == 11)
109var qbig = opt; qbig[9].count = 1001
110r = call(snapshot(qbig, mask: 3))
111check("negative: quota permille 1001 -> INVALID reason 5 at word 56", r.verdict == 1 && res[3] == 5 && res[4] == 56, "\(res[3]) \(res[4])")
112var st6 = base; st6[0].state = 6
113r = call(snapshot(st6))
114check("negative: undefined state code -> INVALID reason 14", r.verdict == 1 && res[3] == 14 && res[4] == 10)
115var k16 = base; k16[7].kind = 16
116r = call(snapshot(k16))
117check("negative: undefined kind 16 -> INVALID reason 6 at word 44", r.verdict == 1 && res[3] == 6 && res[4] == 44)
118var dup = base; dup[3].id = 100
119r = call(snapshot(dup))
120check("negative: duplicate id -> INVALID reason 8 at the second row", r.verdict == 1 && res[3] == 8 && res[4] == 23)
121var pad = snapshot(base); pad[200] = 1
122r = call(pad)
123check("negative: nonzero padding -> INVALID reason 10 at word 200", r.verdict == 1 && res[3] == 10 && res[4] == 200)
124var bad = snapshot(base); bad[0] = 0
125r = call(bad)
126check("negative: bad magic -> INVALID reason 1", r.verdict == 1 && res[3] == 1 && res[4] == 0)
127r = call(snapshot(opt, mask: 1))
128check("negative: quota row without usage source -> INVALID reason 9 at word 54", r.verdict == 1 && res[3] == 9 && res[4] == 54)
129r = call(snapshot(base, mask: 0))
130check("negative: station rows without floor source -> INVALID reason 9", r.verdict == 1 && res[3] == 9 && res[4] == 9)
131
132r = call(snapshot(base, link: 2))
133check("unavailable: health failed -> OFFLINE 2 reason 20, admission 0", r.verdict == 2 && res[3] == 20 && res[45] == 0)
134r = call(snapshot(clear, link: 3))
135check("unavailable: stream disconnected -> UNAVAILABLE 3 reason 22 even with idle rows, admission 0", r.verdict == 3 && res[3] == 22 && res[45] == 0)
136r = call(snapshot(clear, link: 0))
137check("unavailable: link unknown -> UNAVAILABLE 3 reason 21, admission 0", r.verdict == 3 && res[3] == 21 && res[45] == 0)
138r = call(snapshot([], mask: 0))
139check("partial: floor stream not received -> PARTIAL 5 reason 26, admission 0", r.verdict == 5 && res[3] == 26 && res[45] == 0)
140r = call(snapshot([]))
141check("negative: empty rows are PARTIAL 5 reason 29 kind 1, never CLEAR; station total -1",
142 r.verdict == 5 && res[3] == 29 && res[4] == 1 && res[25] == none)
143r = call(snapshot(clear.filter { $0.kind != 4 }))
144check("partial: review decision group absent -> PARTIAL reason 30 kind 4, admission still 1", r.verdict == 5 && res[3] == 30 && res[4] == 4 && res[28] == none && res[45] == 1)
145r = call(snapshot(clear.filter { $0.kind != 6 }))
146check("partial: merge owner group absent -> PARTIAL reason 31 kind 6", r.verdict == 5 && res[3] == 31 && res[4] == 6)
147r = call(snapshot(clear, gen: now - 60001))
148check("stale: 60001 ms old -> STALE 4 reason 24, age 60001, admission 0", r.verdict == 4 && res[3] == 24 && res[6] == 60001 && res[45] == 0)
149r = call(snapshot(clear, gen: 0))
150check("stale: generated absent -> STALE reason 23, age -1, admission 0", r.verdict == 4 && res[3] == 23 && res[6] == none && res[45] == 0)
151r = call(snapshot(clear, gen: now + 30001))
152check("stale: generated 30001 ms ahead -> STALE reason 25, admission 0", r.verdict == 4 && res[3] == 25 && res[6] == none && res[45] == 0)
153
154// Bridge refusals: status 32, result and output untouched.
155call(snapshot(clear)); let before = res
156r = call(snapshot(clear), inLen: 255)
157check("bridge: wrong length -> status 32, result and output untouched", r.status == 32 && r.verdict == 0xDEAD && res == before, "\(r)")
158var same = snapshot(clear); var v: UInt64 = 0xDEAD
159let ov = same.withUnsafeMutableBytes { b in omega_hopfact(&v, UnsafeRawPointer(b.baseAddress), 256, b.baseAddress, 256) }
160check("bridge: overlapping in/out -> status 32", ov == 32 && v == 0xDEAD)
161
162// Latency on reused buffers (the call path Hop uses).
163let inp = snapshot(groups)
164let iters = 200_000
165let t0 = DispatchTime.now().uptimeNanoseconds
166var acc: UInt64 = 0
167for _ in 0..<iters { acc &+= call(inp).verdict }
168let ns = Double(DispatchTime.now().uptimeNanoseconds - t0) / Double(iters)
169check("latency: \(iters) calls, \(String(format: "%.0f", ns)) ns/call", acc == UInt64(iters) * 6)
170
171print(fails == 0 ? "HOPFACT-CHECK PASS" : "HOPFACT-CHECK FAIL \(fails)")
172exit(fails == 0 ? 0 : 1)