Files
sencho/backend/src/__tests__/composePreview.test.ts
T
Anso 06b25262cc feat(stacks): guided first stack import flow (#1285)
* feat(stacks): add guided first stack import flow

Add an Import mode to the Create Stack dialog and a zero-stacks empty
state so a new user who already has compose files on disk can land their
first stack without reading the docs first.

A read-only scan of the compose directory (GET /api/stacks/import/scan)
lists the compose files it finds with a dry preview of each file's
services, ports, volumes, and env files. Each result is labelled by
placement: already a stack, loose at the root of the compose directory,
or one folder too deep, with the exact path to move misplaced files to.
The scan never writes, moves, or changes any files.

Manual stack creation (Empty, From Git, From Docker Run) is unchanged.

* fix(stacks): read import-scan candidates via a single file handle

Open the compose file once and stat plus read on the same descriptor so
the size check and the read observe the same inode, instead of resolving
the path twice (stat then readFile), which is a time-of-check/time-of-use
race. Mirrors the existing handle-based readers in FileSystemService.

* fix(stacks): confine import scan to the compose dir and refine the empty state

Harden the read-only import scan:
- Resolve symlinks and confirm the real target stays inside the compose
  directory before reading a candidate, and reject non-regular files, so a
  symlinked compose file or parent cannot expose a file outside the compose
  directory through the preview (matches resolveSafeStackPath).
- Read at most the stat-reported size (bounded by the 1 MiB cap) from the open
  handle, so a file that grows after the size check cannot exceed the cap.
- Log when the compose directory or a subdirectory cannot be read, so an access
  failure is not silently reported as "no compose files found".

Only show the first-run "No stacks yet" prompt when no filter chip is active, so
a filter that matches nothing is not mistaken for an empty fleet.
2026-06-02 16:10:05 -04:00

87 lines
2.7 KiB
TypeScript

/**
* Unit tests for parseComposePreview: the pure compose-to-preview parser behind
* the guided import scan. Covers port/volume/env normalization, the relative-
* volume warning (1:1 path rule), and the non-throwing error paths.
*/
import { describe, it, expect } from 'vitest';
import { parseComposePreview } from '../helpers/composePreview';
describe('parseComposePreview', () => {
it('extracts services, short-syntax ports, volumes, and env_file', () => {
const yaml = `
services:
web:
image: nginx:1.27
ports:
- "8080:80"
- "53:53/udp"
volumes:
- ./data:/usr/share/nginx/html
env_file: web.env
`;
const result = parseComposePreview(yaml);
expect(result.parseError).toBeUndefined();
expect(result.services).toHaveLength(1);
const web = result.services[0];
expect(web.name).toBe('web');
expect(web.image).toBe('nginx:1.27');
expect(web.ports).toEqual(['8080->80', '53->53']);
expect(web.volumes).toEqual(['./data:/usr/share/nginx/html']);
expect(web.envFiles).toEqual(['web.env']);
// Relative bind source triggers the 1:1 path-rule warning.
expect(result.warnings.some((w) => w.includes('1:1 path rule'))).toBe(true);
});
it('normalizes long-syntax ports and ip-prefixed short ports', () => {
const yaml = `
services:
api:
ports:
- target: 3001
published: 2283
- "127.0.0.1:9000:9000"
- "5000"
`;
const result = parseComposePreview(yaml);
expect(result.services[0].ports).toEqual(['2283->3001', '9000->9000', '5000']);
});
it('handles env_file as an array of strings and {path} objects', () => {
const yaml = `
services:
app:
env_file:
- common.env
- path: secrets.env
`;
const result = parseComposePreview(yaml);
expect(result.services[0].envFiles).toEqual(['common.env', 'secrets.env']);
});
it('does not warn for named or absolute volumes', () => {
const yaml = `
services:
db:
volumes:
- pgdata:/var/lib/postgresql/data
- /opt/host/conf:/etc/conf
volumes:
pgdata:
`;
const result = parseComposePreview(yaml);
expect(result.services[0].volumes).toEqual(['pgdata:/var/lib/postgresql/data', '/opt/host/conf:/etc/conf']);
expect(result.warnings).toHaveLength(0);
});
it('reports a parseError for invalid YAML without throwing', () => {
const result = parseComposePreview('services: [unclosed');
expect(result.parseError).toBeDefined();
expect(result.services).toHaveLength(0);
});
it('reports a parseError when there are no services', () => {
const result = parseComposePreview("name: just-a-file\nversion: '3'");
expect(result.parseError).toBe('No services found in this file.');
});
});