1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
-- 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'
.. 'Inactive\tLegacy\t100%\t1970-01-01T00:00:00Z\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 == '1w' and limits[1].used_percent == 27)
assert(limits[2].label == '5h' and limits[2].used_percent == 0)
local rows = assert(parse(sample))
assert(#rows == 5 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[5].used_percent == nil and rows[5].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)
hooks.render_footer[1]({has_tui = false, get_component = function() error('footer wrapped in print mode') end})
local component
hooks.render_footer[1]({
has_tui = true,
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
hooks.session_ready[1]({has_tui = false})
refresh()
assert(rendered() == 'native footer' and probes == 0, 'headless quota read or placeholder')
local model = 'other:test'
ext.config={providers={alias={protocol='antigravity-acp'}}}; ext.session_info=function() return {model=model} end
local new_timer, clock = uv.new_timer
uv.new_timer=function() clock=new_timer(); return clock end
hooks.session_ready[1]({has_tui = true})
uv.new_timer=new_timer
assert(clock:get_repeat()==180000)
footer.publish(ext, 'peer', 'peer usage footer')
assert(table.concat(component:render(132), '\n'):find('\27[34ma\27[31mn\27[33mt\27[34mi\27[32mg\27[31mr\27[34ma\27[31mv\27[33mi\27[34mt\27[32my',1,true), 'missing per-letter footer colors')
assert(rendered():find('antigravity usage: loading',1,true) and rendered():find('peer usage footer',1,true) and probes == 1)
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(not line:find('Legacy',1,true) and not line:find('stale',1,true),'expired CLI buckets tainted a fresh sample')
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))
-- Exercise the actual timer callback, without waiting three minutes or sending a turn.
advance=advance+180*10^9
clock:set_repeat(1); clock:again(); clock:ref(); uv.sleep(3); uv.run('nowait'); assert(probes==1)
model='alias:neon'; uv.sleep(3); uv.run('nowait'); clock:stop(); assert(probes==2)
verified('/fake-agy',profile,env); uv.run(); assert(spawns==2 and rendered():find('Flash 0%',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 == 7 and spawns == 6)
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 == 6 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')
|