summaryrefslogtreecommitdiff
path: root/_quota_selfcheck.lua
diff options
context:
space:
mode:
authort <t@tjp.lol>2026-11-27 08:46:14 -0700
committert <t@tjp.lol>2026-11-30 08:34:38 -0700
commit47039f21a170e29875a2676f7a2eb5a779144b74 (patch)
tree495ffa7dab8de455a8d2ebfafd45dfcce5da52b7 /_quota_selfcheck.lua
Add Antigravity ACP provider for Panto
Diffstat (limited to '_quota_selfcheck.lua')
-rw-r--r--_quota_selfcheck.lua96
1 files changed, 96 insertions, 0 deletions
diff --git a/_quota_selfcheck.lua b/_quota_selfcheck.lua
new file mode 100644
index 0000000..4d76bd4
--- /dev/null
+++ b/_quota_selfcheck.lua
@@ -0,0 +1,96 @@
+-- Run with lua _quota_selfcheck.lua. No installed CLI, account data or network.
+local uv = require('luv')
+local quota, footer = dofile('quota.lua'), dofile('_usage_footer.lua')
+local wall = 2000000000
+os.time = function() return wall end
+local reset = os.date('!%Y-%m-%dT%H:%M:%SZ', wall + 60)
+local sample = 'Gemini\tPro\t72%\t' .. reset .. '\n'
+ .. 'Gemini\tFlash\t100%\t\nOther\tLimited\t0%\t\n'
+ .. 'Credits\tCount only\t150\t' .. reset .. '\nGemini\tOff\tdisabled\t\n'
+local function parse(s) return quota.parse(s, footer.reset_time) end
+local limits = assert(parse('Gemini Models\tWeekly Limit Remaining\t73%\t\nGemini Models\tFive Hour Limit Remaining\t100%\t\n'))
+assert(limits[1].label == 'Gemini Models / Weekly Limit Used' and limits[1].used_percent == 27)
+assert(limits[2].label == 'Gemini Models / Five Hour Limit Used' and limits[2].used_percent == 0)
+local rows = assert(parse(sample))
+assert(#rows == 4 and rows[1].label == 'Gemini / Pro' and rows[1].used_percent == 28)
+assert(rows[1].resets_at == wall + 60 and rows[2].used_percent == 0 and rows[3].used_percent == 100)
+assert(rows[4].used_percent == nil and rows[4].resets_at == wall + 60)
+assert(#assert(parse('')) == 0 and #assert(parse('Gemini\tOff\tdisabled\t\n')) == 0)
+for _, bad in ipairs({
+ 'Gemini\tPro\t101%\t\n', 'Gemini\tPro\t072%\t\n', 'Gemini\tPro\t0.72\t\n', 'Gemini\tPro\tused 72%\t\n',
+ 'Gemini\tPro\t72%\t2030-02-30T12:00:00Z\n', 'Gemini\tPro\t72%\t2030-02-26 12:00 PST\n',
+ 'Gemini\tPro\t72%\t', '\27[31mGemini\tPro\t72%\t\n', sample .. sample,
+ 'account login required\n' .. sample, 'Gemini\tPro\t7%\t\t\n', string.rep('x', 32769),
+}) do assert(parse(bad) == nil) end
+
+local hooks, ext = {}, {}
+function ext.on(event, handler)
+ hooks[event] = hooks[event] or {}
+ table.insert(hooks[event], handler)
+end
+local probes, spawns, cancelled, verified = 0, 0, 0, nil
+local acp = { probe = function(run)
+ probes, verified = probes + 1, run
+ return function() cancelled = cancelled + 1 end
+end }
+local profile, env = '/fake-profile', {'HOME=/fake-profile'}
+local spawn, hrtime, output, exit_status = uv.spawn, uv.hrtime, sample, 0
+local advance = 0
+uv.hrtime = function() return hrtime() + advance end
+uv.spawn = function(bin, opts, callback)
+ assert(bin == '/fake-agy' and opts.cwd == profile and opts.env == env and opts.detached)
+ assert(table.concat(opts.args, '|') == '--gemini_dir|/fake-profile|--sandbox|-p|/usage')
+ assert(opts.stdio[1] == nil and opts.stdio[2] and opts.stdio[3] == nil)
+ spawns = spawns + 1
+ opts.cwd = '/'
+ opts.args = {'-c', 'test ! -t 0 && test ! -t 1 || exit 9; if (: </dev/tty) 2>/dev/null; then exit 9; fi; printf "%s" "$1"; exit "$2"', 'fake-agy', output, tostring(exit_status)}
+ return spawn('/bin/sh', opts, callback)
+end
+
+local refresh = quota.activate(ext, footer, acp)
+assert(quota.activate(ext, footer, acp) == refresh and #hooks.render_footer == 1)
+refresh() -- no footer attached: no headless launch
+assert(probes == 0)
+local component
+hooks.render_footer[1]({
+ get_component = function() return {render = function() return {'native footer'} end} end,
+ set_component = function(_, c) component = c; return {invalidate = function() end} end,
+})
+local function rendered()
+ return table.concat(component:render(132), '\n'):gsub('\27%[[%d;]*m', '')
+end
+footer.publish(ext, 'peer', 'peer usage footer')
+assert(rendered():find('antigravity usage: awaiting first turn',1,true) and rendered():find('peer usage footer',1,true) and probes == 0)
+refresh(); refresh(); assert(probes == 1 and spawns == 0)
+verified('/fake-agy', profile, env); refresh(); uv.run(); refresh()
+assert(probes == 1 and spawns == 1)
+local line = rendered()
+assert(line:find('antigravity usage: Gemini / Pro 28%', 1, true) and line:find('Count only ?%', 1, true))
+assert(line:find('reset in 1m', 1, true) and line:find('peer usage footer', 1, true))
+assert(select(2, line:gsub('antigravity usage:', '')) == 1)
+wall = wall + 60
+line = rendered()
+assert(line:find('stale', 1, true) and not line:find('Pro', 1, true) and line:find('Flash 0%', 1, true))
+wall = wall + 240
+assert(rendered():find('antigravity usage: stale', 1, true))
+assert(not rendered():find('Flash', 1, true))
+
+local function retry(body, code)
+ output, exit_status, advance = body, code, advance + 61 * 10^9
+ refresh(); verified('/fake-agy', profile, env); uv.run()
+end
+retry('Gemini\tOther\t72%\t\n' .. 'unexpected stderr-like text\n', 0)
+assert(rendered():find('antigravity usage: unavailable', 1, true) and not rendered():find('unexpected', 1, true))
+retry(sample, 1)
+assert(rendered():find('antigravity usage: unavailable', 1, true) and not rendered():find('Flash', 1, true))
+retry(string.rep('x', 32769), 0)
+assert(rendered():find('antigravity usage: unavailable', 1, true))
+retry('', 0)
+assert(rendered():find('antigravity usage: unknown', 1, true))
+advance = advance + 61 * 10^9
+refresh(); assert(probes == 6 and spawns == 5)
+for _, end_session in ipairs(hooks.session_end) do end_session() end
+verified('/fake-agy', profile, env); refresh(); uv.run()
+assert(cancelled == 1 and spawns == 5 and not uv.loop_alive())
+assert(rendered() == 'native footer\npeer usage footer')
+print('PASS: exact /usage TSV, fake CLI gate and coalescing, stale/errors and shared footer')