summaryrefslogtreecommitdiff
path: root/tests/fake-agy.lua
blob: cd4f4608b4c9655fc4a5229858cceb3bd31299eb (plain)
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
-- Offline agy fixture. check.lua installs this with Panto's embedded Lua shebang.
local uv, json = require('luv'), require('panto').ext.json
local home = assert(os.getenv('HOME'))
if arg[1] == '--version' then error('unexpected version check') end
if arg[1] ~= 'acp' then
    assert(json.encode({table.unpack(arg)}) == json.encode({
        '--gemini_dir', home, '--sandbox', '-p', '/usage',
    }), 'unrecognized fake CLI invocation')
    if uv.fs_stat(home .. '/usage-failure') then os.exit(3) end
    assert(uv.cwd() == home and home:match('/google$') and uv.guess_handle(0) ~= 'tty')
    print('Gemini\tWeekly\t73%\t2099-02-03T12:00:00Z'); return
end

local function read() return json.decode(assert(io.read('l'), 'ACP input closed')) end
local function send(msg)
    msg.jsonrpc = '2.0'; assert(io.write(json.encode(msg), '\n')); assert(io.flush())
end
local function request(method)
    local q = read(); assert(q.method == method, 'expected ' .. method); return q
end
local function reply(q, result) send({id=q.id, result=result or {}}) end
local function prompt(text)
    local q = request('session/prompt')
    assert(#q.params.prompt == 1 and q.params.prompt[1].type == 'text' and q.params.prompt[1].text == text)
    return q
end
local function finish(q, text)
    send({method='session/update', params={sessionId='test-session', update={
        sessionUpdate='agent_message_chunk', content={type='text', text=text},
    }}})
    reply(q, {stopReason='end_turn'})
end

local q = request('initialize')
reply(q, {protocolVersion=1, authMethods={{id='oauth-personal'}}, agentCapabilities={
    sessionCapabilities={resume={}}, _meta={antigravity={systemPrompt={append=true}}},
}})
q = request('authenticate'); assert(json.encode(q.params) == '{"methodId":"oauth-personal"}'); reply(q)
q = read()
local setup, resumed = q.params, q.method == 'session/resume'
assert(#setup.mcpServers == 1 and setup.mcpServers[1].name=='pantograph' and setup._meta.disableBuiltInTools == true)
local settings=home..'/antigravity-cli/settings.json'
local f=assert(io.open(settings,'r')); local policy=json.decode(f:read('a')); f:close()
if resumed or uv.fs_stat(home..'/usage-failure') then
    assert(policy.permissions.ask[1]=='mcp(pantograph/*)', 'existing settings replaced')
else
    assert(json.encode(policy)=='{"permissions":{"allow":["mcp(pantograph/*)"]}}', 'missing scoped agy default')
    f=assert(io.open(settings,'w')); assert(f:write('{"permissions":{"ask":["mcp(pantograph/*)"]}}')); assert(f:close())
end
if resumed then assert(setup.sessionId == 'test-session' and not setup._meta.antigravity)
else assert(q.method == 'session/new' and setup._meta.antigravity.systemPrompt.append:find('Short answers', 1, true)) end
if resumed then -- Older ACP pickers may expose suffixed models instead.
    reply(q, {sessionId='test-session', configOptions={{id='model', options={{name='Gemini test', value='gemini-test-high'}}}}})
    q=request('session/set_config_option'); assert(q.params.configId=='model' and q.params.value=='gemini-test-high')
    reply(q, {configOptions={{id='model', currentValue=q.params.value}}})
end
local function split(model, level, levels)
    local choices={}; for _,v in ipairs(levels) do choices[#choices+1]={value=v,name=v} end
    return {{id='model', currentValue=model, options={{value='gemini-test',name='Gemini test'}}},
        {id='thought_level', currentValue=level, options=choices}}
end
local function set_level(value)
    local req=request('session/set_config_option'); assert(req.params.configId=='thought_level' and req.params.value==value)
    local opts=split('gemini-test',value,{'low','high'})
    send({method='session/update',params={sessionId='test-session',update={sessionUpdate='config_option_update',configOptions=opts}}})
    reply(req,{configOptions=opts})
end
if not resumed then
    reply(q,{sessionId='test-session',configOptions=split('gemini-other','high',{'high','xhigh'})})
    q=request('session/set_config_option'); assert(q.params.configId=='model' and q.params.value=='gemini-test')
    reply(q,{configOptions=split('gemini-test','high',{'low','high'})})
    set_level('low')
end

local function check_relay(server)
    local input, output = assert(uv.pipe()), assert(uv.pipe())
    local proc = assert(uv.spawn(server.command, {args=server.args, stdio={input.read, output.write, 2}}, function() end))
    uv.fs_close(input.read); uv.fs_close(output.write)
    local function rpc(id, method, params)
        local line = json.encode({jsonrpc='2.0', id=id, method=method, params=params or {}}) .. '\n'
        assert(uv.fs_write(input.write, line, -1) == #line)
        local buf = ''
        repeat
            local chunk = assert(uv.fs_read(output.read, 4096, -1)); assert(chunk ~= '', 'relay closed'); buf = buf .. chunk
        until buf:sub(-1) == '\n'
        local r = json.decode(buf); assert(r.id == id); return r
    end
    assert(rpc(1, 'initialize').result.serverInfo.name == 'pantograph')
    local tools = rpc(2, 'tools/list').result.tools
    local schemas={}; for _,t in ipairs(tools) do schemas[t.name]=t.inputSchema end
    assert(schemas.std__read and schemas.std__edit and schemas.fixture__echo.properties.rows.items.required[1]=='value')
    assert(rpc(3, 'tools/call', {name='not_exported', arguments={}}).error.code == -32602)
    assert(rpc(5, 'tools/call', {name='fixture__echo', arguments={rows={{value='bad'}}}}).error.code == -32602)
    local echo=rpc(6, 'tools/call', {name='fixture__echo', arguments={rows={{value=42}}}}).result
    assert(echo.isError==false and echo.content[1].text=='42')
    local r = rpc(4, 'tools/call', {name='std__read', arguments={path=uv.cwd() .. '/.panto/SYSTEM.md'}}).result
    assert(r.isError == false and r.content[1].text:find('Short answers', 1, true))
    proc:kill('sigkill'); uv.fs_close(input.write); uv.fs_close(output.read); uv.run(); proc:close()
end

if resumed then finish(prompt('resume'), 'resumed-ok') else
    q = prompt('hello')
    send({id='perm', method='session/request_permission', params={sessionId='test-session',
        toolCall={title='Read local file'}, options={{kind='allow_once', optionId='once'}},
    }})
    assert(json.encode(read().result.outcome) == '{"outcome":"cancelled"}')
    check_relay(setup.mcpServers[1]); finish(q, 'primary-tool-ok')
    set_level('high')
    finish(prompt('again'), 'same-session-ok')
end
for _ in io.lines() do end -- keep the session alive until Panto closes stdin