mirror of
https://github.com/Studio-Saelix/sencho.git
synced 2026-08-06 08:58:05 +00:00
8ba88755b1
The Empty branch of the Create Stack flow wrote a compose.yaml whose first service bound the host's port 8080 by default. On any workstation already running something on 8080 (traefik, caddy, librespeed, another nginx, etc.) the very first deploy failed at the docker compose networking step with "Bind for 0.0.0.0:8080 failed: port is already allocated", which made the day-one experience feel broken right after F-2 (PR #1168) tightened the dialog itself. The boilerplate in FileSystemService.createStack now emits the ports block commented out plus a one-line hint above it. A fresh deploy binds no host port, so the container starts cleanly on any host; the user uncomments the two-line block when they're ready to expose the container. The deterministic shape (no probe-and-write, no random port, no preflight scan) avoids the TOCTOU race that a free-port probe would have left between template creation and the actual compose up. Adds backend/src/__tests__/file-system-service-create-stack.test.ts (8 cases): directory + file creation, structural YAML assertions via yaml.parse to lock in the no-live-ports invariant, raw-text regex to lock in the commented hint, and the already-exists rejection path. FileSystemService.createStack had no coverage before this change. Adds e2e/default-stack-template-no-fixed-port.spec.ts (1 case): drives the dialog through Create, reads the resulting compose via the in-browser apiFetch, and asserts the live + commented invariants end-to-end. docs/features/stack-management.mdx Empty bullet rewritten to describe the minimal skeleton and the commented ports block instead of calling it "blank". Resolves: F-3 in the v1.0 audit tracker.
99 lines
3.6 KiB
TypeScript
99 lines
3.6 KiB
TypeScript
/**
|
|
* Tests for FileSystemService.createStack().
|
|
*
|
|
* Runs against a real tmpdir-backed compose root so the template is read
|
|
* from actual on-disk bytes (not a writeFile spy). Locks in the default
|
|
* Empty template shape: a minimal nginx skeleton with the host port
|
|
* binding commented out so a fresh deploy never collides with whatever
|
|
* already owns the host's port 8080.
|
|
*/
|
|
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
import { promises as fs } from 'fs';
|
|
import path from 'path';
|
|
import os from 'os';
|
|
import { parse as parseYaml } from 'yaml';
|
|
|
|
const mockState = { composeDir: '' };
|
|
|
|
vi.mock('../services/NodeRegistry', () => ({
|
|
NodeRegistry: {
|
|
getInstance: () => ({
|
|
getComposeDir: () => mockState.composeDir,
|
|
getDefaultNodeId: () => 1,
|
|
}),
|
|
},
|
|
}));
|
|
|
|
import { FileSystemService } from '../services/FileSystemService';
|
|
|
|
describe('FileSystemService.createStack', () => {
|
|
let tmpBase: string;
|
|
|
|
beforeEach(async () => {
|
|
tmpBase = await fs.mkdtemp(path.join(os.tmpdir(), 'sencho-create-stack-'));
|
|
mockState.composeDir = tmpBase;
|
|
});
|
|
|
|
afterEach(async () => {
|
|
await fs.rm(tmpBase, { recursive: true, force: true });
|
|
});
|
|
|
|
async function createAndRead(name: string): Promise<{ text: string; parsed: unknown }> {
|
|
await FileSystemService.getInstance().createStack(name);
|
|
const text = await fs.readFile(path.join(tmpBase, name, 'compose.yaml'), 'utf-8');
|
|
return { text, parsed: parseYaml(text) };
|
|
}
|
|
|
|
it('creates the stack directory under the compose root', async () => {
|
|
await FileSystemService.getInstance().createStack('alpha');
|
|
const stat = await fs.stat(path.join(tmpBase, 'alpha'));
|
|
expect(stat.isDirectory()).toBe(true);
|
|
});
|
|
|
|
it('writes a compose.yaml file inside the stack directory', async () => {
|
|
await FileSystemService.getInstance().createStack('bravo');
|
|
const stat = await fs.stat(path.join(tmpBase, 'bravo', 'compose.yaml'));
|
|
expect(stat.isFile()).toBe(true);
|
|
});
|
|
|
|
it('writes a YAML document that parses as a service map with image and restart', async () => {
|
|
const { parsed } = await createAndRead('charlie');
|
|
expect(parsed).toMatchObject({
|
|
services: {
|
|
app: {
|
|
image: 'nginx:latest',
|
|
restart: 'always',
|
|
},
|
|
},
|
|
});
|
|
});
|
|
|
|
it('ships the ports block commented out (no live host port binding)', async () => {
|
|
const { parsed } = await createAndRead('delta');
|
|
const services = (parsed as { services?: { app?: { ports?: unknown } } }).services;
|
|
expect(services?.app?.ports).toBeUndefined();
|
|
});
|
|
|
|
it('preserves a commented ports hint so the user can uncomment without re-typing the structure', async () => {
|
|
const { text } = await createAndRead('echo');
|
|
expect(text).toMatch(/^[ \t]*#[ \t]*ports:[ \t]*$/m);
|
|
expect(text).toMatch(/^[ \t]*#[ \t]*-[ \t]*"8080:80"[ \t]*$/m);
|
|
});
|
|
|
|
it('includes a one-line hint above the commented block explaining what to uncomment', async () => {
|
|
const { text } = await createAndRead('foxtrot');
|
|
expect(text).toMatch(/^[ \t]*#[ \t]+Uncomment to expose a host port:[ \t]*$/m);
|
|
});
|
|
|
|
it('contains live (uncommented) configuration so the template is not entirely commented out', async () => {
|
|
const { text } = await createAndRead('golf');
|
|
expect(text).toMatch(/^[ \t]*image:[ \t]+nginx:latest[ \t]*$/m);
|
|
expect(text).toMatch(/^[ \t]*restart:[ \t]+always[ \t]*$/m);
|
|
});
|
|
|
|
it('rejects creation when the stack directory already exists', async () => {
|
|
await FileSystemService.getInstance().createStack('hotel');
|
|
await expect(FileSystemService.getInstance().createStack('hotel')).rejects.toThrow(/already exists/);
|
|
});
|
|
});
|