Files
arboretum/packages/vscode/test/rest-client.test.ts
Johan LEROY a7e04278fd
All checks were successful
CI / Build & test (Node 22) (push) Successful in 10m17s
CI / Build & test (Node 24) (push) Successful in 10m13s
CI / No em/en dashes (push) Successful in 4s
Deploy site (production) / build-and-deploy (push) Successful in 24s
Release / Publish to Gitea npm registry (push) Successful in 10m17s
VSCode Release / Package VSIX (push) Successful in 9m39s
Desktop Release / Build Linux (AppImage + deb) (push) Successful in 15m41s
CI / Pack & boot smoke (Node 22) (push) Successful in 10m3s
release: git-arboretum 3.3.0 (« Démarrer le projet » : lancement multi-terminaux), vscode 0.4.0, desktop 0.1.3
« Démarrer le projet » : un repo définit une fois ses commandes de démarrage
(serveur de dev, API, base de données), un clic ouvre un terminal PTY par
commande dans le dock IDE.

Serveur (additif, PROTOCOL_VERSION inchangé) :
- LaunchCommand[] persistées sur repos.launch_commands (migration 13) ; champ additif SessionSummary.launchRunId.
- POST /repos/:id/launch : résolution du worktree côté serveur, cwd de commande borné (anti-traversal), commandIds outrepasse enabled.
- GET /repos/:id/launch/detect : détection package.json / Procfile / docker-compose.
- Shell de login interactif ($SHELL -l -i, charge le PATH nvm/asdf) + auto-type de la commande ; le shell survit à la commande (échec visible).

Web : LaunchProjectModal + actions (ProjectTreeNode, SessionsPanel, CommandPalette), stores sessions/worktrees, i18n EN/FR.

Alignement du reste du projet :
- Extension VS Code 0.4.0 : commande Start Project (repo/worktree), Stop Launch, badge « launch » dans l'arbre, méthode REST startLaunch.
- Site vitrine : 16e feature card (Rocket) + section showcase « Start the project » (mockup fidèle au modal), i18n EN/FR.
- Documentation : README (EN + FR), help-content (EN + FR), CHANGELOGs server + vscode.

Vérifié : 430 tests, typecheck, build (web + site + vscode), acceptance-p13 ALL GREEN, VSIX packagé, garde anti-tirets, vérif visuelle du site (thèmes clair et sombre).
2026-07-21 13:54:16 +02:00

91 lines
4.1 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest';
import { RestClient, RestError } from '../src/api/rest-client.js';
type Captured = { url: string; init: RequestInit };
function fakeFetch(response: { ok: boolean; status: number; body: unknown }, captured: Captured[]) {
return vi.fn(async (url: string, init: RequestInit) => {
captured.push({ url, init });
return {
ok: response.ok,
status: response.status,
json: async () => response.body,
} as unknown as Response;
}) as unknown as typeof fetch;
}
describe('RestClient', () => {
it('appelle /api/v1/auth/me avec le header Bearer', async () => {
const captured: Captured[] = [];
const rest = new RestClient(
{ baseUrl: 'http://127.0.0.1:7317/', token: 'arb_secret' },
fakeFetch({ ok: true, status: 200, body: { ok: true, tokenId: 't', tokenLabel: 'l', serverVersion: '1.8.0' } }, captured),
);
const me = await rest.me();
expect(me.serverVersion).toBe('1.8.0');
expect(captured[0]?.url).toBe('http://127.0.0.1:7317/api/v1/auth/me');
const headers = captured[0]?.init.headers as Record<string, string>;
expect(headers.Authorization).toBe('Bearer arb_secret');
});
it('normalise les erreurs serveur en RestError (code + status)', async () => {
const rest = new RestClient(
{ baseUrl: 'http://127.0.0.1:7317', token: 'bad' },
fakeFetch({ ok: false, status: 401, body: { error: { code: 'BAD_TOKEN', message: 'invalid token' } } }, []),
);
await expect(rest.me()).rejects.toMatchObject({ code: 'BAD_TOKEN', status: 401 });
await expect(rest.me()).rejects.toBeInstanceOf(RestError);
});
it('encode les ids dans les chemins et envoie le corps JSON', async () => {
const captured: Captured[] = [];
const rest = new RestClient(
{ baseUrl: 'http://h:1', token: 't' },
fakeFetch({ ok: true, status: 200, body: { worktree: {} } }, captured),
);
await rest.commitWorktree('repo id', { path: '/w', message: 'msg' });
expect(captured[0]?.url).toBe('http://h:1/api/v1/repos/repo%20id/worktrees/commit');
expect(captured[0]?.init.method).toBe('POST');
expect(JSON.parse(String(captured[0]?.init.body))).toEqual({ path: '/w', message: 'msg' });
});
it('fetch/pull worktree : chemins et corps', async () => {
const captured: Captured[] = [];
const rest = new RestClient(
{ baseUrl: 'http://h:1', token: 't' },
fakeFetch({ ok: true, status: 200, body: { worktree: {} } }, captured),
);
await rest.fetchWorktree('r', { path: '/w' });
await rest.pullWorktree('r', { path: '/w', mode: 'rebase' });
expect(captured[0]?.url).toBe('http://h:1/api/v1/repos/r/worktrees/fetch');
expect(captured[1]?.url).toBe('http://h:1/api/v1/repos/r/worktrees/pull');
expect(JSON.parse(String(captured[1]?.init.body))).toEqual({ path: '/w', mode: 'rebase' });
});
it('startLaunch POST /repos/:id/launch avec le corps (commandIds / worktreePath)', async () => {
const captured: Captured[] = [];
const rest = new RestClient(
{ baseUrl: 'http://h:1', token: 't' },
fakeFetch({ ok: true, status: 200, body: { sessions: [], skipped: [] } }, captured),
);
await rest.startLaunch('repo id', { worktreePath: '/w', commandIds: ['a', 'b'] });
expect(captured[0]?.url).toBe('http://h:1/api/v1/repos/repo%20id/launch');
expect(captured[0]?.init.method).toBe('POST');
expect(JSON.parse(String(captured[0]?.init.body))).toEqual({ worktreePath: '/w', commandIds: ['a', 'b'] });
});
it('listSessions construit la query includeHidden/includeArchived', async () => {
const captured: Captured[] = [];
const rest = new RestClient(
{ baseUrl: 'http://h:1', token: 't' },
fakeFetch({ ok: true, status: 200, body: { sessions: [] } }, captured),
);
await rest.listSessions();
await rest.listSessions({ includeArchived: true });
await rest.listSessions({ includeHidden: true, includeArchived: true });
expect(captured[0]?.url).toBe('http://h:1/api/v1/sessions');
expect(captured[1]?.url).toBe('http://h:1/api/v1/sessions?includeArchived=true');
expect(captured[2]?.url).toBe('http://h:1/api/v1/sessions?includeHidden=true&includeArchived=true');
});
});