Files
pi2d/test/cli/helpers.ts
T
yuez 01db49fb38 feat(daemon): add access modes and the local-browser login handshake
`PI2_ACCESS_MODE` (local|lan|tailscale) picks the bind address and the derived
CORS origins, `PI2_PUBLIC_URL` adds the external origin, and `PI2_ORIGINS` is now
additive instead of replacing the derived list. `daemon.json` and
`/api/v1/info` carry the mode and the public URL.

`POST /api/v1/console/session` is the `:PiWeb` handshake: loopback + the local
CLI token only, it mints or reuses the single "local browser" device and answers
`http://127.0.0.1:<port>/#t=<token>`. The token is stored plaintext next to
`devices.json` (0600) so repeated logins return the same credentials instead of
logging the already-open tab out; revoking the device drops it.
2026-10-23 08:34:39 +08:00

58 lines
1.8 KiB
TypeScript

/** Shared helpers for the CLI specs (temp dirs, silent logger, bare config). */
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import type { Config, Limits } from "../../src/config.js";
import type { Logger } from "../../src/core/interfaces.js";
export const TEST_LIMITS: Limits = {
maxBodyBytes: 8 * 1024 * 1024,
maxFrameBytes: 8 * 1024 * 1024,
maxConnections: 64,
};
export function tempDir(prefix = "pi2-daemon-cli-"): string {
return mkdtempSync(join(tmpdir(), prefix));
}
export function removeDir(dir: string): void {
rmSync(dir, { recursive: true, force: true });
}
export function testConfig(overrides: Partial<Config> = {}): Config {
const base: Config = {
host: "127.0.0.1",
port: 0,
staticDir: "/nonexistent-web-dist",
corsOrigins: [],
limits: TEST_LIMITS,
tokenPath: "/tmp/pi2-daemon-cli-test.sock.token",
deviceStorePath: "/tmp/pi2-daemon-cli-test-devices.json",
access: "local",
publicUrl: null,
};
return { ...base, ...overrides, limits: { ...base.limits, ...(overrides.limits ?? {}) } };
}
export const silentLogger: Logger = {
debug: () => undefined,
info: () => undefined,
warn: () => undefined,
error: () => undefined,
child: () => silentLogger,
};
export function setRuntimeEnv(dir: string): void {
process.env.PI2_RUNTIME_DIR = join(dir, "run");
process.env.PI2_DATA_DIR = join(dir, "data");
process.env.PI2_STATE_DIR = join(dir, "state");
process.env.PI2_PORT = "0";
process.env.PI2_STATIC_DIR = join(dir, "web");
}
export function clearRuntimeEnv(): void {
for (const key of ["PI2_RUNTIME_DIR", "PI2_DATA_DIR", "PI2_STATE_DIR", "PI2_PORT", "PI2_STATIC_DIR"]) {
delete process.env[key];
}
}