|
| 1 | +--!nocheck |
| 2 | +-- Offline test of the compiled live debug API (out/live-debug.luau: remoteDebug.command / stream, fleet.column / |
| 3 | +-- columnsChanged; plans/34) under Lune, against stub kernels: names and options are checked (bad ones, duplicates and |
| 4 | +-- ones past the limits throw), a client throws, older kernels and edit mode accept and send nothing, a 0.5.7 kernel |
| 5 | +-- gets one declaration per frame (in order, with labels, kinds and types), its handlers reach the game's functions, |
| 6 | +-- removing an entry re-declares, columnsChanged reaches the kernel (declarations first), unbind stops everything. |
| 7 | +-- Run after `bun run build` (Lune is pinned in kernel/rokit.toml): |
| 8 | +-- cd ../kernel && lune run ../framework/scripts/test-live-debug.luau ../framework/ |
| 9 | +local fs = require("@lune/fs") |
| 10 | +local luau = require("@lune/luau") |
| 11 | +local process = require("@lune/process") |
| 12 | +local task = require("@lune/task") |
| 13 | +local root = process.args[1] or "./" |
| 14 | + |
| 15 | +local passed, failed = 0, 0 |
| 16 | +local function check(label, condition, detail) |
| 17 | + if condition then |
| 18 | + passed += 1 |
| 19 | + else |
| 20 | + failed += 1 |
| 21 | + print("FAIL " .. label .. (if detail ~= nil then ": " .. tostring(detail) else "")) |
| 22 | + end |
| 23 | +end |
| 24 | +local function throws(fn, fragment) |
| 25 | + local ok, err = pcall(fn) |
| 26 | + return not ok and string.find(tostring(err), fragment, 1, true) ~= nil, err |
| 27 | +end |
| 28 | +local function settle() |
| 29 | + for _ = 1, 3 do |
| 30 | + task.wait() |
| 31 | + end |
| 32 | +end |
| 33 | + |
| 34 | +local env = setmetatable({ task = task, script = {} }, { __index = _G }) |
| 35 | +local L = luau.load(fs.readFile(`{root}out/live-debug.luau`), { debugName = "live-debug", environment = env })() |
| 36 | +local remoteDebug, fleet = L.remoteDebug, L.fleet |
| 37 | + |
| 38 | +-- Names --------------------------------------------------------------------------------------------------------------- |
| 39 | +check("names: valid", L.validDebugName("a") and L.validDebugName("round_state") and L.validDebugName("a" .. string.rep("9", 31))) |
| 40 | +check("names: invalid", not L.validDebugName("") and not L.validDebugName("A") and not L.validDebugName("_x") and not L.validDebugName("1x") and not L.validDebugName("a-b") and not L.validDebugName("a.b") and not L.validDebugName(string.rep("a", 33)) and not L.validDebugName(5)) |
| 41 | + |
| 42 | +-- Edit mode (no kernel): accepted and checked ------------------------------------------------------------------------------ |
| 43 | +local removeEdit = remoteDebug:command("edit_cmd", function() end) |
| 44 | +check("edit mode: a command is accepted (returns a remover); not supported", typeof(removeEdit) == "function" and remoteDebug:supported() == false) |
| 45 | +check("edit mode: columnsChanged does nothing", pcall(function() |
| 46 | + fleet:columnsChanged() |
| 47 | +end)) |
| 48 | +removeEdit() |
| 49 | +check("bad name throws", throws(function() |
| 50 | + remoteDebug:command("Bad", function() end) |
| 51 | +end, "^[a-z][a-z0-9_]{0,31}$")) |
| 52 | +check("no function throws", throws(function() |
| 53 | + remoteDebug:command("x", nil) |
| 54 | +end, "pass a function")) |
| 55 | +check("a label over 40 characters throws", throws(function() |
| 56 | + fleet:column("x", function() end, { label = string.rep("l", 41) }) |
| 57 | +end, "at most 40")) |
| 58 | +check("a description over 200 characters throws", throws(function() |
| 59 | + remoteDebug:command("x", function() end, { description = string.rep("d", 201) }) |
| 60 | +end, "at most 200")) |
| 61 | +check("labels count characters, not bytes (40 x é is fine)", pcall(function() |
| 62 | + fleet:column("accents", function() end, { label = string.rep("é", 40) })() |
| 63 | +end)) |
| 64 | +check("stream: no hz throws", throws(function() |
| 65 | + remoteDebug:stream("s", function() end, {}) |
| 66 | +end, "hz must be")) |
| 67 | +check("stream: hz past 4 or under 0.2 throws", throws(function() |
| 68 | + remoteDebug:stream("s", function() end, { hz = 5 }) |
| 69 | +end, "hz must be") and throws(function() |
| 70 | + remoteDebug:stream("s", function() end, { hz = 0.1 }) |
| 71 | +end, "hz must be")) |
| 72 | +check("stream: a bad kind throws", throws(function() |
| 73 | + remoteDebug:stream("s", function() end, { hz = 1, kind = "dots" }) |
| 74 | +end, "kind must be")) |
| 75 | +check("column: a bad type throws", throws(function() |
| 76 | + fleet:column("c", function() end, { type = "date" }) |
| 77 | +end, "type must be")) |
| 78 | + |
| 79 | +-- Limits and duplicates |
| 80 | +do |
| 81 | + local removers = {} |
| 82 | + for index = 1, 16 do |
| 83 | + table.insert(removers, fleet:column(`c{index}`, function() end)) |
| 84 | + end |
| 85 | + check("limit: a 17th column throws", throws(function() |
| 86 | + fleet:column("c17", function() end) |
| 87 | + end, "at most 16 columns")) |
| 88 | + check("duplicate: throws", throws(function() |
| 89 | + fleet:column("c1", function() end) |
| 90 | + end, "already registered")) |
| 91 | + for _, remove in removers do |
| 92 | + remove() |
| 93 | + end |
| 94 | + for index = 1, 32 do |
| 95 | + table.insert(removers, remoteDebug:command(`m{index}`, function() end)) |
| 96 | + end |
| 97 | + check("limit: a 33rd command throws", throws(function() |
| 98 | + remoteDebug:command("m33", function() end) |
| 99 | + end, "at most 32 commands")) |
| 100 | + for index = 1, 8 do |
| 101 | + table.insert(removers, remoteDebug:stream(`s{index}`, function() end, { hz = 1 })) |
| 102 | + end |
| 103 | + check("limit: a 9th stream throws", throws(function() |
| 104 | + remoteDebug:stream("s9", function() end, { hz = 1 }) |
| 105 | + end, "at most 8 streams")) |
| 106 | + for _, remove in removers do |
| 107 | + remove() |
| 108 | + end |
| 109 | + check("removed entries free their names", pcall(function() |
| 110 | + fleet:column("c1", function() end)() |
| 111 | + end)) |
| 112 | +end |
| 113 | + |
| 114 | +-- A client: server only -------------------------------------------------------------------------------------------------- |
| 115 | +L.bindLiveDebug("client", {}) |
| 116 | +check("client: command, stream, column and columnsChanged throw server-only", throws(function() |
| 117 | + remoteDebug:command("x", function() end) |
| 118 | +end, "server-only") and throws(function() |
| 119 | + remoteDebug:stream("x", function() end, { hz = 1 }) |
| 120 | +end, "server-only") and throws(function() |
| 121 | + fleet:column("x", function() end) |
| 122 | +end, "server-only") and throws(function() |
| 123 | + fleet:columnsChanged() |
| 124 | +end, "server-only")) |
| 125 | +L.unbindLiveDebug() |
| 126 | + |
| 127 | +-- An older kernel (no debugDeclare): silent --------------------------------------------------------------------------------- |
| 128 | +local oldKernel = { kernelVersion = "0.5.6" } |
| 129 | +L.bindLiveDebug("server", oldKernel) |
| 130 | +local removeOld = remoteDebug:command("old_cmd", function() end) |
| 131 | +settle() |
| 132 | +check("kernel 0.5.6: accepted, not supported, nothing called", typeof(removeOld) == "function" and remoteDebug:supported() == false) |
| 133 | +check("kernel 0.5.6: columnsChanged is a no-op", pcall(function() |
| 134 | + fleet:columnsChanged() |
| 135 | +end)) |
| 136 | +removeOld() |
| 137 | +L.unbindLiveDebug() |
| 138 | + |
| 139 | +-- Kernel 0.5.7 ------------------------------------------------------------------------------------------------------------- |
| 140 | +local kernel = { kernelVersion = "0.5.7", declared = {}, changed = 0 } |
| 141 | +function kernel:debugDeclare(declarations) |
| 142 | + table.insert(self.declared, declarations) |
| 143 | + return { ok = true, hash = "h", columns = #declarations.columns, commands = #declarations.commands, streams = #declarations.streams, dropped = 0 } |
| 144 | +end |
| 145 | +function kernel:onDebugCommand(fn) |
| 146 | + self.command = fn |
| 147 | +end |
| 148 | +function kernel:onDebugColumn(fn) |
| 149 | + self.column = fn |
| 150 | +end |
| 151 | +function kernel:onDebugStream(fn) |
| 152 | + self.stream = fn |
| 153 | +end |
| 154 | +function kernel:debugColumnsChanged() |
| 155 | + self.changed += 1 |
| 156 | + return true |
| 157 | +end |
| 158 | + |
| 159 | +-- Registered before the bind (module top level): sent at bind. |
| 160 | +local mapName = "Lobby" |
| 161 | +fleet:column("current_map", function() |
| 162 | + return mapName |
| 163 | +end, { label = "Map", type = "string" }) |
| 164 | +L.bindLiveDebug("server", kernel) |
| 165 | +check("bind: handlers installed and the early registration declared at once", typeof(kernel.command) == "function" and typeof(kernel.column) == "function" and typeof(kernel.stream) == "function" and #kernel.declared == 1 and kernel.declared[1].columns[1].name == "current_map") |
| 166 | +check("supported on 0.5.7", remoteDebug:supported() == true) |
| 167 | + |
| 168 | +local calls = {} |
| 169 | +remoteDebug:command("round_state", function(args, caller) |
| 170 | + table.insert(calls, { args = args, caller = caller }) |
| 171 | + return { kind = "kv", items = { round = 3, n = args.n } } |
| 172 | +end, { label = "Round", description = "The current round" }) |
| 173 | +fleet:column("in_round", function() |
| 174 | + return true |
| 175 | +end, { type = "bool" }) |
| 176 | +local removeLoot = remoteDebug:stream("loot", function() |
| 177 | + return { points = { { x = 1, y = 2, z = 3 } } } |
| 178 | +end, { hz = 2, kind = "points", label = "Loot" }) |
| 179 | +remoteDebug:stream("state", function() |
| 180 | + return { phase = "intermission" } |
| 181 | +end, { hz = 0.5 }) |
| 182 | +check("registrations in one frame are declared once, deferred", #kernel.declared == 1) |
| 183 | +settle() |
| 184 | +check("...then one declaration", #kernel.declared == 2, #kernel.declared) |
| 185 | +local decl = kernel.declared[2] |
| 186 | +check("declaration: columns in order with label and type", #decl.columns == 2 and decl.columns[1].name == "current_map" and decl.columns[1].label == "Map" and decl.columns[1].type == "string" and decl.columns[2].name == "in_round" and decl.columns[2].type == "bool") |
| 187 | +check("declaration: commands with label and description", #decl.commands == 1 and decl.commands[1].name == "round_state" and decl.commands[1].label == "Round" and decl.commands[1].description == "The current round") |
| 188 | +check("declaration: streams with hz, kind (json by default) and label", #decl.streams == 2 and decl.streams[1].hz == 2 and decl.streams[1].kind == "points" and decl.streams[1].label == "Loot" and decl.streams[2].kind == "json") |
| 189 | + |
| 190 | +-- The kernel's handlers reach the game's functions. |
| 191 | +local caller = { kind = "roblox", userId = 1, owner = true, via = "roblox" } |
| 192 | +local answer = kernel.command("round_state", { n = 7 }, caller) |
| 193 | +check("command handler: fn(args, caller), its answer returned", answer.items.round == 3 and answer.items.n == 7 and calls[1].caller == caller) |
| 194 | +check("command handler: non-table args become {}", kernel.command("round_state", "junk", caller).items.n == nil) |
| 195 | +check("command handler: an unknown name throws", throws(function() |
| 196 | + kernel.command("nope", {}, caller) |
| 197 | +end, "unknown command")) |
| 198 | +check("column handler: the getter's value", kernel.column("current_map") == "Lobby" and kernel.column("in_round") == true and kernel.column("nope") == nil) |
| 199 | +mapName = "HedgeMaze" |
| 200 | +check("column handler: read live", kernel.column("current_map") == "HedgeMaze") |
| 201 | +check("stream handler: the sampler's value", kernel.stream("loot").points[1].x == 1 and kernel.stream("state").phase == "intermission" and kernel.stream("nope") == nil) |
| 202 | + |
| 203 | +-- columnsChanged |
| 204 | +fleet:columnsChanged() |
| 205 | +check("columnsChanged reaches the kernel", kernel.changed == 1) |
| 206 | +local before = #kernel.declared |
| 207 | +fleet:column("score", function() |
| 208 | + return 5 |
| 209 | +end) |
| 210 | +fleet:columnsChanged() |
| 211 | +check("columnsChanged: pending declarations go first", #kernel.declared == before + 1 and kernel.changed == 2 and #kernel.declared[#kernel.declared].columns == 3) |
| 212 | +settle() |
| 213 | +check("...and not twice", #kernel.declared == before + 1) |
| 214 | + |
| 215 | +-- Removing re-declares. |
| 216 | +removeLoot() |
| 217 | +settle() |
| 218 | +local last = kernel.declared[#kernel.declared] |
| 219 | +check("removing a stream re-declares without it", #last.streams == 1 and last.streams[1].name == "state" and kernel.stream("loot") == nil) |
| 220 | +removeLoot() |
| 221 | +settle() |
| 222 | +check("a remover is idempotent", #kernel.declared[#kernel.declared].streams == 1) |
| 223 | + |
| 224 | +-- A throwing kernel never breaks the game. |
| 225 | +local angry = { debugDeclare = function() |
| 226 | + error("kernel down") |
| 227 | +end, debugColumnsChanged = function() |
| 228 | + error("kernel down") |
| 229 | +end } |
| 230 | +L.unbindLiveDebug() |
| 231 | +L.bindLiveDebug("server", angry) |
| 232 | +check("a kernel that throws: bind, register and columnsChanged don't", pcall(function() |
| 233 | + remoteDebug:command("extra", function() end) |
| 234 | + settle() |
| 235 | + fleet:columnsChanged() |
| 236 | +end)) |
| 237 | + |
| 238 | +-- Unbind: nothing reaches the kernel any more. |
| 239 | +L.unbindLiveDebug() |
| 240 | +local count = #kernel.declared |
| 241 | +remoteDebug:command("after", function() end) |
| 242 | +settle() |
| 243 | +fleet:columnsChanged() |
| 244 | +check("after unbind: nothing is sent", #kernel.declared == count and kernel.changed == 2 and remoteDebug:supported() == false) |
| 245 | + |
| 246 | +print(`test-live-debug: {passed} passed, {failed} failed`) |
| 247 | +if failed > 0 then |
| 248 | + process.exit(1) |
| 249 | +end |
0 commit comments