Files
crank/docs/epic-1-completion/evidence/agent-probe.cjs
T

41 lines
2.9 KiB
JavaScript

const assert = require('node:assert/strict');
const { chromium } = require(process.env.CRANK_PROBE_PLAYWRIGHT || require('node:path').resolve(__dirname, '../../../apps/ui/node_modules/@playwright/test'));
(async () => {
const browser = await chromium.launch();
try {
const page = await browser.newPage();
await page.goto('http://127.0.0.1:3300/login');
await page.locator('#email').fill('owner@crank.local');
await page.locator('#password').fill('change-me-admin-password');
await page.locator('.btn-signin').click();
await page.waitForURL('http://127.0.0.1:3300/');
const result = await page.evaluate(async () => {
const api = window.CrankApi;
const session = await api.getSession();
const csrf = await api.refreshSessionCsrf();
session.csrf_token = csrf.csrf_token; window.CrankAuth.replaceSession(session);
const ws = session.current_workspace_id || session.memberships[0].workspace.id;
const agents = await api.listAgents(ws);
const list = Array.isArray(agents) ? agents : (agents.agents || agents.items);
const source = list.find(a => a.latest_published_version != null);
if (!source) throw new Error('No published fixture Agent');
const sid = source.agent_id || source.id;
const version = await fetch('/api/admin/workspaces/'+ws+'/agents/'+sid+'/versions/'+source.latest_published_version).then(r=>r.json());
const bindings = version.bindings.map(b => ({operation_id:b.operation_id,operation_version:b.operation_version,tool_name:b.tool_name,tool_title:b.tool_title,tool_description_override:b.tool_description_override,enabled:b.enabled}));
const created = await api.createAgent(ws,{slug:'retro-probe-'+Date.now(),display_name:'Retro probe',description:'Synthetic regression probe',instructions:{},tool_selection_policy:{}});
const id = created.agent_id || created.id;
await api.saveAgentBindings(ws,id,bindings);
await api.publishAgent(ws,id,{version:1});
const before=await api.getAgent(ws,id);
const publishedBefore=await fetch('/api/admin/workspaces/'+ws+'/agents/'+id+'/versions/1').then(r=>r.json());
let errorCode=null; let saved=null;
try { saved=await api.saveAgentBindings(ws,id,bindings); } catch(e) { errorCode=e.errorCode || e.code || e.message; }
const after=await api.getAgent(ws,id);
const publishedAfter=await fetch('/api/admin/workspaces/'+ws+'/agents/'+id+'/versions/1').then(r=>r.json());
return {before_draft:before.current_draft_version,after_draft:after.current_draft_version,saved_version:saved && saved.version,published_unchanged:JSON.stringify(publishedBefore)===JSON.stringify(publishedAfter),error:errorCode};
});
console.log(JSON.stringify(result));
assert.equal(result.error,null); assert.equal(result.before_draft,1); assert.equal(result.after_draft,2); assert.equal(result.saved_version,2); assert.equal(result.published_unchanged,true);
} finally { await browser.close(); }
})().catch(e=>{console.error(e.message);process.exitCode=1;});