Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
]
},
"scripts": {
"test": "node --test test/unit.test.js",
"test": "node --test test/unit.test.js test/yard.test.js",
"test:e2e": "node test/e2e/run.js",
"package": "vsce package --out agent-guild.vsix"
},
Expand Down
13 changes: 12 additions & 1 deletion src/yard.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class YardView {
this.loaded = false;
this.ready = false;
this.attempt = 0;
this.rendering = false;
// Goes up each time the view hides, which ends its page.
this.shows = 0;
this.status = null;
// The tab's look, kept for the next time the view opens.
events.look.event((look) => {
Expand All @@ -32,6 +35,10 @@ class YardView {
});
// The tab reached a manager, maybe a new one.
events.connected.event(() => this.render());
// The tab's page reconnected in place, as after an upgrade's restart: a notice may be out of date.
events.ready.event(() => {
if (!this.shown && !this.rendering) this.render();
});
}

look() {
Expand All @@ -45,6 +52,7 @@ class YardView {
view.onDidChangeVisibility(() => {
// A hidden view loses its page; VS Code loads it again when the view shows.
if (!view.visible) {
this.shows++;
this.loaded = false;
this.ready = false;
return;
Expand All @@ -66,13 +74,15 @@ class YardView {
const view = this.view;
if (!view?.visible) return;
const attempt = ++this.attempt;
this.rendering = true;
if (!this.shown) this.notice({ state: 'connecting' });
let state;
try {
state = await locate();
} catch (err) {
state = { state: 'failed', detail: err.message };
}
if (attempt === this.attempt) this.rendering = false;
if (attempt !== this.attempt || this.view !== view || !view.visible) return;
if (state.state !== 'ready' || !newEnough(state.version, VIEW_VERSION)) return this.notice(state);
const src = compactSrc(state, this.look());
Expand All @@ -90,9 +100,10 @@ class YardView {
async check() {
const view = this.view;
const page = this.page;
const shows = this.shows;
this.loaded = true;
if (this.ready || await health(this.url)) return;
if (this.view !== view || this.page !== page || this.ready || !view.visible) return;
if (this.view !== view || this.page !== page || this.shows !== shows || this.ready || !view.visible) return;
this.notice({ state: 'unavailable' });
}

Expand Down
18 changes: 16 additions & 2 deletions test/e2e/suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ async function count(event, ms = 3000) {
* A stand-in manager that answers its health checks as `answers` says, one each and then the last one again,
* and drops every other request, so its page never loads: a manager that answered and then went away.
*/
async function stub(answers) {
async function stub(answers, version = '0.0.0-development') {
const stand = { checks: 0 };
const server = http.createServer(async (req, res) => {
if (!req.url.startsWith('/api/v1/health')) return req.socket.destroy();
const live = await answers[Math.min(stand.checks++, answers.length - 1)]();
res.writeHead(live ? 200 : 503, { 'content-type': 'application/json' });
res.end(JSON.stringify(live ? { ok: true, name: 'agent-guild', version: '0.0.0-development' } : { ok: false }));
res.end(JSON.stringify(live ? { ok: true, name: 'agent-guild', version } : { ok: false }));
});
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
stand.url = `http://127.0.0.1:${server.address().port}`;
Expand Down Expand Up @@ -212,6 +212,20 @@ exports.run = async () => {
assert.deepEqual([tabs, views], [0, 0], 'after a restart, neither page reloads again');
assert.equal(api.yard.status, 'yard');
console.log('After the manager restarts, the tab and the view each reconnect once.');

// An upgrade from the tab: the view showed the update notice, and the tab reconnects to the new manager in place.
const old = await stub([() => true], '1.4.0');
pointAt(old.url);
api.yard.receive({ kind: 'action', action: 'retry' });
await until(() => api.yard.status === 'ready', 'The view asking for an update');
restore();
const upgraded = next(api.onViewReady, 'The view showing the Yard after the upgrade');
await call('POST', '/shutdown', { force: true, restart: true });
await upgraded;
assert.equal(api.yard.status, 'yard');
assert.equal(await count(api.onViewReady), 0, 'the upgraded view loads its page once');
await old.close();
console.log('After an upgrade through the tab, the view shows the Yard without being hidden.');
} finally {
restore();
}
Expand Down
111 changes: 111 additions & 0 deletions test/yard.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// The Yard view's lifecycle, with VS Code and the manager replaced by doubles the test controls.
const test = require('node:test');
const assert = require('node:assert/strict');
const Module = require('module');

class Emitter {
constructor() { this.listeners = []; }
get event() { return (listener) => { this.listeners.push(listener); return { dispose() {} }; }; }
fire(value) { for (const listener of [...this.listeners]) listener(value); }
dispose() {}
}
const vscode = {
EventEmitter: Emitter,
Uri: { parse: (value) => value, file: (value) => value, joinPath: (...parts) => parts.join('/') },
env: { openExternal() {} },
window: {},
};
const load = Module._load;
Module._load = function (request, ...rest) {
return request === 'vscode' ? vscode : load.call(this, request, ...rest);
};

const manager = require('../src/manager');
const doubles = { locate: null, health: null, locates: 0 };
manager.locate = () => { doubles.locates++; return doubles.locate(); };
manager.health = (url) => doubles.health(url);
const { YardView } = require('../src/yard');

const settle = async () => { for (let i = 0; i < 5; i++) await new Promise((resolve) => setImmediate(resolve)); };
const running = (version) => ({ state: 'ready', version, url: 'http://127.0.0.1:47821', token: 'ab'.repeat(24) });

/** A view as VS Code resolves it, recording each page or notice it is given. */
function setup() {
const events = { ready: new Emitter(), viewReady: new Emitter(), look: new Emitter(), connected: new Emitter() };
const store = new Map();
const yard = new YardView({ get: (key) => store.get(key), update: (key, value) => store.set(key, value) }, events);
const visibility = new Emitter();
const messages = new Emitter();
const pages = [];
const view = {
visible: true,
webview: {
options: {},
set html(value) { pages.push(value); },
onDidReceiveMessage: messages.event,
},
onDidChangeVisibility: visibility.event,
onDidDispose: () => {},
};
const show = (visible) => { view.visible = visible; visibility.fire(); };
const yards = () => pages.filter((html) => html.includes('<iframe')).length;
return { yard, events, view, show, pages, yards, send: (message) => messages.fire(message) };
}

test('an upgrade the tab reconnects through replaces the view\'s update notice with the Yard, once', async () => {
doubles.locate = async () => running('1.4.0');
doubles.health = async () => true;
const { yard, events, view, yards } = setup();
await yard.resolveWebviewView(view);
await settle();
assert.equal(yard.status, 'ready', 'a 1.4 manager gets the update notice');

doubles.locate = async () => running('1.5.0');
doubles.locates = 0;
events.ready.fire('1.5.0');
events.ready.fire('1.5.0');
await settle();
assert.equal(yard.status, 'yard');
assert.equal(doubles.locates, 1, 'repeated ready events start one check');
assert.equal(yards(), 1);
});

test('the tab reconnecting leaves a view that shows the Yard alone', async () => {
doubles.locate = async () => running('1.5.0');
doubles.health = async () => true;
const { yard, events, view, pages } = setup();
await yard.resolveWebviewView(view);
await settle();
const before = pages.length;
doubles.locates = 0;
events.ready.fire('1.5.0');
await settle();
assert.equal(doubles.locates, 0);
assert.equal(pages.length, before, 'no reload');
});

test('a frame check that fails replaces the page, unless the view hid and showed again meanwhile', async () => {
doubles.locate = async () => running('1.5.0');
let answer;
doubles.health = () => new Promise((resolve) => { answer = resolve; });

const failed = setup();
await failed.yard.resolveWebviewView(failed.view);
await settle();
failed.send({ kind: 'frame', type: 'load', page: failed.yard.page });
answer(false);
await settle();
assert.equal(failed.yard.status, 'unavailable', 'a failed check on the current page says so');

const restored = setup();
await restored.yard.resolveWebviewView(restored.view);
await settle();
restored.send({ kind: 'frame', type: 'load', page: restored.yard.page });
restored.show(false);
restored.show(true);
await settle();
answer(false);
await settle();
assert.equal(restored.yard.status, 'yard', 'a check from before the view hid changes nothing');
assert.equal(restored.yards(), 1, 'the restored page is kept');
});
Loading