Files
sencho/backend/src/__tests__/webhooks-git-source.test.ts
T
Anso f23b7e1bac feat: ordered multi-file Compose for Git sources (#1380)
* feat: ordered multi-file Compose for Git sources

Extend Git sources to deploy an ordered list of compose files merged with
docker compose -f base.yaml -f override.yaml ..., plus an optional project
directory.

- Pick and reorder compose files from the repository tree (drag to reorder on
  desktop, up/down arrows on phones); manual path entry is also supported.
- The ordered set drives every stack-scoped compose command (deploy, update,
  start/stop/restart/down, image scans, Compose Doctor) and the container
  lookup, so a service or image declared only in an override is handled too.
- Runtime keys off the materialized set, not the saved configuration: saving a
  source does not change deploy args until the pull is applied, and apply
  materializes from the pending snapshot rather than live config.
- The project directory is passed as --project-directory, with -p <stack>
  pinning the Compose project so container labels stay stable.
- The Mesh override is layered last; single-file sources are byte-identical to
  before, and existing rows keep working via the single-path fallback.

Docs cover the picker, ordering, project directory, and the new troubleshooting
and limitations (referenced files are not materialized; the dependency graph,
drift, and networking views read the primary file).

* fix: harden multi-file Git source (hash, unlink, collisions, node id)

- hashContent folds ordered file CONTENTS (not paths) so a clean multi-file
  stack is not flagged as locally edited: create/apply hash the fetched files
  (repo paths) while pull hashes the on-disk files (materialized paths), which
  previously disagreed and showed a false "local edits detected".
- Block unlinking a multi-file or project-directory Git source (409): the deploy
  spec lives on the source row, so removing it would silently revert deploys to
  root compose.yaml. Single-file sources still unlink.
- Reject materialized-path collisions in the selection validator: an additional
  file equal to or nested under compose.yaml, an ancestor/descendant overlap
  between selected files, and a project directory nested under a compose file
  (previously a 500 at materialization).
- DockerController.getContainersByStack uses the controller's node compose dir
  and passes its node id to the authored prefix, instead of the process default.

* fix: CI failures on multi-file Git source (test crash, aria query, path barrier)

- GitSourceFields no longer crashes when repoUrl/branch are falsy: the canBrowse
  trim() is optional-chained, so a reusable field component tolerates partial
  props. Fixes the apply-binding panel test, which feeds a minimal source object.
- GitSourcePanel tests query the footer Remove button by its exact name, so the
  picker's per-file "Remove <path>" buttons no longer collide with the broad
  /remove/i match (the test intent, footer Remove present/absent, is unchanged).
- validateCompose uses an inline resolve + startsWith barrier at the context-dir
  mkdir sink (CodeQL does not credit the wrapped isPathWithinBase helper),
  clearing the js/path-injection alert. The containment check is equivalent and
  contextDir is also validated upstream.

* test: update Git source E2E spec for the multi-file compose picker

The compose-file picker replaced the single #git-source-path input and added
per-file Remove buttons, so the E2E spec drove selectors that no longer exist:

- Drop the redundant compose.yaml fills (the picker defaults to compose.yaml).
- Select the footer Remove button by exact name so the picker's per-file
  "Remove <path>" buttons no longer make the locator ambiguous.
- Set a custom compose path through the picker (add via the manual input, press
  Enter, then remove the default compose.yaml).

* test: match the footer Remove button with an exact Playwright name

Playwright's getByRole name option is a substring match by default, so
{ name: 'Remove' } also matched the picker's "Remove <path>" buttons. Require an
exact match so only the footer Remove button is selected.
2026-06-17 13:24:55 -04:00

240 lines
8.9 KiB
TypeScript

import { describe, it, expect, beforeAll, afterAll, beforeEach, vi } from 'vitest';
import request from 'supertest';
import jwt from 'jsonwebtoken';
import { setupTestDb, cleanupTestDb, TEST_USERNAME, TEST_JWT_SECRET } from './helpers/setupTestDb';
let tmpDir: string;
let app: import('express').Express;
let DatabaseService: typeof import('../services/DatabaseService').DatabaseService;
let LicenseService: typeof import('../services/LicenseService').LicenseService;
let WebhookService: typeof import('../services/WebhookService').WebhookService;
function adminToken(): string {
return jwt.sign({ username: TEST_USERNAME, role: 'admin' }, TEST_JWT_SECRET, { expiresIn: '1m' });
}
function seedGitSource(stackName: string): void {
DatabaseService.getInstance().upsertGitSource({
stack_name: stackName,
repo_url: 'https://github.com/example/repo.git',
branch: 'main',
compose_path: 'compose.yaml',
compose_paths: ['compose.yaml'],
context_dir: null,
sync_env: false,
env_path: null,
auth_type: 'none',
encrypted_token: null,
auto_apply_on_webhook: false,
auto_deploy_on_apply: false,
last_applied_commit_sha: null,
last_applied_content_hash: null,
pending_commit_sha: null,
pending_compose_content: null,
pending_env_content: null,
pending_fetched_at: null,
last_debounce_at: null,
});
}
beforeAll(async () => {
tmpDir = await setupTestDb();
({ app } = await import('../index'));
({ DatabaseService } = await import('../services/DatabaseService'));
({ LicenseService } = await import('../services/LicenseService'));
({ WebhookService } = await import('../services/WebhookService'));
});
afterAll(() => {
cleanupTestDb(tmpDir);
});
beforeEach(() => {
vi.restoreAllMocks();
// Webhooks are free; run at the Community tier.
vi.spyOn(LicenseService.getInstance(), 'getTier').mockReturnValue('community');
});
describe('node-aware Git source webhooks', () => {
it('persists node_id when creating a webhook', async () => {
const db = DatabaseService.getInstance();
const nodeId = db.getDefaultNode()!.id;
seedGitSource('webhook-local-git');
const res = await request(app)
.post('/api/webhooks')
.set('Authorization', `Bearer ${adminToken()}`)
.send({
node_id: nodeId,
name: 'local git webhook',
stack_name: 'webhook-local-git',
action: 'git-pull',
});
expect(res.status).toBe(201);
const row = db.getWebhook(res.body.id);
expect(row?.node_id).toBe(nodeId);
});
it('checks remote git-source existence through the target node', async () => {
const db = DatabaseService.getInstance();
const remoteNodeId = db.addNode({
name: 'remote-git-webhook',
type: 'remote',
compose_dir: '/tmp',
is_default: false,
api_url: 'http://remote.example',
api_token: 'remote-token',
});
const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue(new Response('{}', { status: 200 }));
const res = await request(app)
.post('/api/webhooks')
.set('Authorization', `Bearer ${adminToken()}`)
.send({
node_id: remoteNodeId,
name: 'remote git webhook',
stack_name: 'remote-stack',
action: 'git-pull',
});
expect(res.status).toBe(201);
expect(fetchSpy).toHaveBeenCalledWith(
'http://remote.example/api/stacks/remote-stack/git-source',
expect.objectContaining({ method: 'GET' }),
);
});
it('rejects malformed webhook node_id values', async () => {
const res = await request(app)
.post('/api/webhooks')
.set('Authorization', `Bearer ${adminToken()}`)
.send({
node_id: 'remote',
name: 'bad node id webhook',
stack_name: 'remote-stack',
action: 'deploy',
});
expect(res.status).toBe(400);
expect(res.body.error).toMatch(/node_id/i);
});
it('rejects retargeting an existing git-pull webhook without a Git source', async () => {
const db = DatabaseService.getInstance();
const nodeId = db.getDefaultNode()!.id;
seedGitSource('retarget-source-stack');
const webhookId = db.addWebhook({
node_id: nodeId,
name: 'retarget git webhook',
stack_name: 'retarget-source-stack',
action: 'git-pull',
secret: WebhookService.getInstance().generateSecret(),
enabled: true,
});
const res = await request(app)
.put(`/api/webhooks/${webhookId}`)
.set('Authorization', `Bearer ${adminToken()}`)
.send({ stack_name: 'retarget-no-source-stack' });
expect(res.status).toBe(400);
expect(res.body.error).toMatch(/Git source/i);
});
it('records failure when remote node disconnects before execution', async () => {
const db = DatabaseService.getInstance();
const remoteNodeId = db.addNode({
name: 'remote-disconnected-webhook',
type: 'remote',
compose_dir: '/tmp',
is_default: false,
api_url: '',
api_token: '',
});
const webhookId = db.addWebhook({
node_id: remoteNodeId,
name: 'disconnected remote git',
stack_name: 'remote-stack',
action: 'git-pull',
secret: WebhookService.getInstance().generateSecret(),
enabled: true,
});
const result = await WebhookService.getInstance().execute(db.getWebhook(webhookId)!, 'git-pull', 'test');
expect(result.success).toBe(false);
expect(result.error).toMatch(/unreachable|configured/i);
const history = db.getWebhookExecutions(webhookId);
expect(history[0].status).toBe('failure');
expect(history[0].error).toMatch(/unreachable|configured/i);
});
it('records failure when remote node request times out', async () => {
vi.useFakeTimers();
const db = DatabaseService.getInstance();
const remoteNodeId = db.addNode({
name: 'remote-timeout-webhook',
type: 'remote',
compose_dir: '/tmp',
is_default: false,
api_url: 'http://remote-timeout.example',
api_token: 'remote-token',
});
const webhookId = db.addWebhook({
node_id: remoteNodeId,
name: 'timeout remote git',
stack_name: 'remote-stack',
action: 'git-pull',
secret: WebhookService.getInstance().generateSecret(),
enabled: true,
});
vi.spyOn(globalThis, 'fetch').mockImplementation((_url, init) => new Promise<Response>((_resolve, reject) => {
const signal = (init as RequestInit | undefined)?.signal;
signal?.addEventListener('abort', () => reject(new Error('aborted')));
}));
const pending = WebhookService.getInstance().execute(db.getWebhook(webhookId)!, 'git-pull', 'test');
await vi.advanceTimersByTimeAsync(30_000);
const result = await pending;
expect(result.success).toBe(false);
expect(result.error).toMatch(/timed out/i);
const history = db.getWebhookExecutions(webhookId);
expect(history[0].status).toBe('failure');
expect(history[0].error).toMatch(/timed out/i);
vi.useRealTimers();
});
it('records a debounced (202 skipped) remote git-pull as success, not failure', async () => {
const db = DatabaseService.getInstance();
const remoteNodeId = db.addNode({
name: 'remote-debounce-webhook',
type: 'remote',
compose_dir: '/tmp',
is_default: false,
api_url: 'http://remote-debounce.example',
api_token: 'remote-token',
});
const webhookId = db.addWebhook({
node_id: remoteNodeId,
name: 'debounced remote git',
stack_name: 'remote-stack',
action: 'git-pull',
secret: WebhookService.getInstance().generateSecret(),
enabled: true,
});
// 202 Accepted + status "skipped" is a debounce, not a failure.
vi.spyOn(globalThis, 'fetch').mockResolvedValue(
new Response(JSON.stringify({ status: 'skipped', message: 'Rate limited (debounced).' }), { status: 202 }),
);
const result = await WebhookService.getInstance().execute(db.getWebhook(webhookId)!, 'git-pull', 'test');
expect(result.success).toBe(true);
const history = db.getWebhookExecutions(webhookId);
expect(history[0].status).toBe('success');
expect(history[0].error).toMatch(/debounced/i);
});
});