mirror of
https://github.com/HeyPuter/puter.git
synced 2026-11-23 14:17:06 +00:00
Backend Tests / changes (push) Canceled after 0s
Backend Tests / typecheck (push) Canceled after 0s
Backend Tests / test shard 1/4 (push) Canceled after 0s
Backend Tests / test shard 2/4 (push) Canceled after 0s
Backend Tests / test shard 3/4 (push) Canceled after 0s
Backend Tests / test shard 4/4 (push) Canceled after 0s
Backend Tests / test (main) (push) Canceled after 0s
Backend Tests / coverage (push) Canceled after 0s
Notify HeyPuter / notify (push) Canceled after 0s
* fix(webdav): GET pipes the file without stacking close listeners on the response stream.pipeline put seven close listeners on the response. With metering, compression, the concurrency gate and request tracing on top, every WebDAV download crossed Node's ten-listener warning. GET now uses pipeToResponse, like the other file download routes. * fix(drivers): a streamed result stops its source on unpipe instead of its own close listener pipe already unpipes when the response finishes, closes or fails, so the extra close listener only spent part of the response's listener budget. The routes that hold a response open now have a test that keeps each within that budget. * refactor(http): everything that waits on a response's close shares one listener Egress metering, the concurrency gate, the large-body budget, route lifecycle events, driver call slots, disconnect aborts and SSE writers each put their own close listener on the response. They now register callbacks with onResponseClose, which keeps one listener per response and runs them in the order added, so a streamed response holds three close listeners instead of five or six and request tracing has room for its own. * test(webdav): handle streamed response errors in test sink Co-authored-by: Salazareo <25314977+Salazareo@users.noreply.github.com> * test(webdav): a storage failure mid-download is covered against a real response --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Salazareo <25314977+Salazareo@users.noreply.github.com>
582 lines
20 KiB
TypeScript
582 lines
20 KiB
TypeScript
import bcrypt from 'bcrypt';
|
|
import { mkdtempSync, rmSync } from 'node:fs';
|
|
import { ServerResponse, type IncomingMessage } from 'node:http';
|
|
import { createServer } from 'node:net';
|
|
import type { AddressInfo } from 'node:net';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
import { deepMerge } from '../../tools/lib/configMigration.mjs';
|
|
import { makeActor } from './core/actor';
|
|
import type { Actor } from './core/actor';
|
|
import { runWithContext } from './core/context';
|
|
import { PuterServer } from './server';
|
|
import { IConfig } from './types';
|
|
import { puterClients } from './clients';
|
|
import {
|
|
PostgresDatabaseClient,
|
|
type PostgresPool,
|
|
} from './clients/database/PostgresDatabaseClient';
|
|
import { SqliteDatabaseClient } from './clients/database/SqliteDatabaseClient';
|
|
import type { PoolConfig } from 'pg';
|
|
import { ADMIN_GROUP_UID } from './services/selfhosted/DefaultUserService';
|
|
import { FULL_API_ACCESS } from './services/permission/consts';
|
|
import { WORKER_SUBDOMAIN_PREFIX } from './stores/subdomain/SubdomainStore.js';
|
|
import { generateDefaultFsentries } from './util/userProvisioning';
|
|
|
|
export const POSTGRES_TEST_MIGRATIONS_PATH =
|
|
'src/backend/clients/database/migrations/postgres';
|
|
|
|
type PgMockPostgresHarness = {
|
|
client: PostgresDatabaseClient;
|
|
createClient: () => PostgresDatabaseClient;
|
|
destroy: () => void;
|
|
};
|
|
|
|
const usesPgMockPostgres = (config: IConfig): boolean => {
|
|
const database = config.database;
|
|
if (database?.engine !== 'postgres' || database.inMemory !== true) {
|
|
return false;
|
|
}
|
|
|
|
return !(
|
|
database.connectionString ||
|
|
database.url ||
|
|
database.host ||
|
|
database.port ||
|
|
database.user ||
|
|
database.password ||
|
|
database.database ||
|
|
database.replica
|
|
);
|
|
};
|
|
|
|
export const createPgMockPostgresDatabaseClient = async (
|
|
config: IConfig,
|
|
): Promise<PgMockPostgresHarness> => {
|
|
const [{ PostgresMock }, { Pool }] = await Promise.all([
|
|
import('pgmock'),
|
|
import('pg'),
|
|
]);
|
|
const mock = await PostgresMock.create();
|
|
const pgMockConfig = mock.getNodePostgresConfig();
|
|
const createPgMockStream = pgMockConfig.stream;
|
|
const poolFactory = (poolConfig: PoolConfig): PostgresPool =>
|
|
new Pool({
|
|
...poolConfig,
|
|
database: 'postgres',
|
|
...pgMockConfig,
|
|
stream: () => {
|
|
const stream = createPgMockStream();
|
|
stream.ref = () => stream;
|
|
stream.unref = () => stream;
|
|
return stream;
|
|
},
|
|
}) as unknown as PostgresPool;
|
|
const createClient = () => new PostgresDatabaseClient(config, poolFactory);
|
|
|
|
return {
|
|
client: createClient(),
|
|
createClient,
|
|
destroy: () => mock.destroy(),
|
|
};
|
|
};
|
|
|
|
/**
|
|
* When `PUTER_TEST_DB_ENGINE=postgres` is set, `setupTestServer` swaps its
|
|
* default sqlite test database for an in-memory Postgres backed by pgmock (with
|
|
* the bundled Postgres migrations applied on boot). Tests that explicitly
|
|
* override `database` still win — the env var only affects the implicit default
|
|
* used by callers that don't pass any DB overrides.
|
|
*
|
|
* Recognized values: `postgres` → pgmock. Anything else (including unset) → the
|
|
* original sqlite-in-memory default.
|
|
*/
|
|
const testDatabaseDefault = (): IConfig['database'] => {
|
|
const engine = (process.env.PUTER_TEST_DB_ENGINE ?? '').toLowerCase();
|
|
if (engine === 'postgres') {
|
|
return {
|
|
engine: 'postgres',
|
|
inMemory: true,
|
|
migrationPaths: [POSTGRES_TEST_MIGRATIONS_PATH],
|
|
};
|
|
}
|
|
return { engine: 'sqlite', inMemory: true };
|
|
};
|
|
|
|
export type SetupTestServerOptions = {
|
|
/**
|
|
* Listen on a real HTTP port so external clients (puter.js runners,
|
|
* workerd, browsers) can connect. Default: in-process only, no listener.
|
|
*/
|
|
listen?: boolean;
|
|
};
|
|
|
|
/**
|
|
* Grab a free port by binding to 0 and releasing it. Done up-front (rather than
|
|
* letting the server listen on 0) so the port is known while building config —
|
|
* `origin` / `api_base_url` consumers like LocalWorkerService read it at
|
|
* construction time.
|
|
*/
|
|
export const allocateEphemeralPort = (): Promise<number> =>
|
|
new Promise((resolve, reject) => {
|
|
const probe = createServer();
|
|
probe.unref();
|
|
probe.on('error', reject);
|
|
probe.listen(0, '127.0.0.1', () => {
|
|
const { port } = probe.address() as AddressInfo;
|
|
probe.close(() => resolve(port));
|
|
});
|
|
});
|
|
|
|
// The JSON module's payload lives on `.default` — merging the namespace
|
|
// itself would bury every value under a stray `default` key.
|
|
const loadDefaultConfig = async (): Promise<IConfig> => {
|
|
const { default: defaultConfig } = await import(
|
|
'../../config.default.json',
|
|
{
|
|
with: {
|
|
type: 'json',
|
|
},
|
|
}
|
|
);
|
|
return defaultConfig as unknown as IConfig;
|
|
};
|
|
|
|
export const setupTestServer = async (
|
|
configOverrides?: IConfig,
|
|
options?: SetupTestServerOptions,
|
|
): Promise<PuterServer> => {
|
|
const defaultConfig = await loadDefaultConfig();
|
|
// merge default config with overrides and test defaults
|
|
const config = deepMerge(
|
|
deepMerge(defaultConfig, {
|
|
extensions: [],
|
|
port: 0,
|
|
database: testDatabaseDefault(),
|
|
dynamo: { inMemory: true, bootstrapTables: true },
|
|
redis: { useMock: true },
|
|
// Suites run system jobs themselves, not on the poll timer.
|
|
jobs: { enabled: false },
|
|
// The health loop's timed `SELECT 1` would land in tests that
|
|
// count queries.
|
|
server_health: { disabled_checks: ['database-liveness'] },
|
|
s3: { localConfig: { inMemory: true } },
|
|
no_default_user: true,
|
|
no_devwatch: true,
|
|
no_browser_launch: true,
|
|
import_ts_extensions: true,
|
|
}),
|
|
configOverrides ?? {},
|
|
) as IConfig;
|
|
|
|
// Ollama auto-discovers unless disabled, probing localhost on every boot
|
|
// and logging the failure after the test may have finished.
|
|
config.providers ??= {};
|
|
config.providers.ollama ??= { enabled: false };
|
|
|
|
if (options?.listen) {
|
|
if (!config.port) config.port = await allocateEphemeralPort();
|
|
// Derive from `domain` so the advertised origins match the host
|
|
// the subdomain gates expect (`api.<domain>` for API routes).
|
|
const host = config.domain ?? '127.0.0.1';
|
|
config.origin ??= `http://${host}:${config.port}`;
|
|
config.api_base_url ??= config.domain
|
|
? `http://api.${config.domain}:${config.port}`
|
|
: config.origin;
|
|
}
|
|
|
|
let pgMockClient: PgMockPostgresHarness | undefined;
|
|
if (usesPgMockPostgres(config)) {
|
|
const database = config.database;
|
|
if (!database) {
|
|
throw new Error('Postgres test database config is missing');
|
|
}
|
|
database.migrationPaths ??= [POSTGRES_TEST_MIGRATIONS_PATH];
|
|
pgMockClient = await createPgMockPostgresDatabaseClient(config);
|
|
}
|
|
|
|
const server = new PuterServer(
|
|
config,
|
|
pgMockClient ? { ...puterClients, db: pgMockClient.client } : undefined,
|
|
);
|
|
|
|
if (pgMockClient) {
|
|
const originalShutdown = server.shutdown.bind(server);
|
|
let destroyPgMock: (() => void) | undefined = pgMockClient.destroy;
|
|
server.shutdown = async () => {
|
|
try {
|
|
await originalShutdown();
|
|
} finally {
|
|
destroyPgMock?.();
|
|
destroyPgMock = undefined;
|
|
}
|
|
};
|
|
}
|
|
|
|
try {
|
|
await server.start(!options?.listen);
|
|
} catch (e) {
|
|
// Stops whatever boot already started; also destroys the pgmock.
|
|
try {
|
|
await server.shutdown();
|
|
} catch {
|
|
// the start error is the one worth reporting
|
|
}
|
|
throw e;
|
|
}
|
|
return server;
|
|
};
|
|
|
|
const PORT_ATTEMPTS = 3;
|
|
|
|
/**
|
|
* `allocateEphemeralPort` releases the port before boot listens on it, and
|
|
* another process can take it in between, so boot again on a fresh one.
|
|
*/
|
|
const bootOnFreshPort = async <T>(
|
|
boot: (port: number) => Promise<T>,
|
|
): Promise<T> => {
|
|
for (let attempt = 1; ; attempt++) {
|
|
const port = await allocateEphemeralPort();
|
|
try {
|
|
return await boot(port);
|
|
} catch (e) {
|
|
const inUse =
|
|
(e as NodeJS.ErrnoException | undefined)?.code === 'EADDRINUSE';
|
|
if (!inUse || attempt >= PORT_ATTEMPTS) throw e;
|
|
}
|
|
}
|
|
};
|
|
|
|
export type TestUserCredentials = {
|
|
username: string;
|
|
password: string;
|
|
token: string;
|
|
/**
|
|
* Full-access access token (what the dashboard's "API Token" flow mints).
|
|
* The `/puterai/*` wire routes reject bare session tokens
|
|
* (`noUserSession`), so suites exercising them authenticate with this.
|
|
*/
|
|
apiToken: string;
|
|
/**
|
|
* User-scoped worker session token (what deploying a worker with no app
|
|
* binding mints — `kind='worker'` session row). Never treated as a root
|
|
* token: suites use it to prove worker credentials pass the `noUserSession`
|
|
* gates.
|
|
*/
|
|
workerToken: string;
|
|
};
|
|
|
|
/**
|
|
* Seed a user with a known password directly through the stores (same steps as
|
|
* DefaultUserService's admin bootstrap: bcrypt-hashed password, home directory
|
|
* tree, optional admin-group membership) and mint a session token the same way
|
|
* `POST /login` does.
|
|
*/
|
|
export const createTestUser = async (
|
|
server: PuterServer,
|
|
opts: { username: string; password: string; admin?: boolean },
|
|
): Promise<TestUserCredentials> => {
|
|
const passwordHash = await bcrypt.hash(opts.password, 8);
|
|
const created = await server.stores.user.create({
|
|
username: opts.username,
|
|
uuid: uuidv4(),
|
|
password: passwordHash,
|
|
email: null,
|
|
requires_email_confirmation: false,
|
|
});
|
|
|
|
// Driver permissions no longer come from here (see
|
|
// `default_user_permissions`), but group-issued ACL grants do, and a
|
|
// verified signup is a member of the default user group.
|
|
const { default_user_group } = await loadDefaultConfig();
|
|
if (default_user_group) {
|
|
await server.stores.group.addUsers(default_user_group, [opts.username]);
|
|
}
|
|
if (opts.admin) {
|
|
await server.stores.group.addUsers(ADMIN_GROUP_UID, [opts.username]);
|
|
}
|
|
|
|
await generateDefaultFsentries(
|
|
server.clients.db,
|
|
server.stores.user,
|
|
created,
|
|
);
|
|
const user = (await server.stores.user.getById(created.id)) ?? created;
|
|
|
|
const { token } = await server.services.auth.createSessionToken(user, {
|
|
user_agent: 'puter-test-env',
|
|
});
|
|
|
|
// Mint the delegated credential the same way the dashboard's
|
|
// "API Token" flow does (POST /auth/create-access-token with the
|
|
// full-api-access sentinel).
|
|
const apiToken = await server.services.auth.createAccessToken(
|
|
makeActor({ user }),
|
|
[[FULL_API_ACCESS]],
|
|
{ label: 'puter-test-env' },
|
|
);
|
|
|
|
// Mint a user-scoped worker token the same way deploying an app-less
|
|
// worker does (WorkerDriver falls back to createWorkerSessionToken). The
|
|
// token only lives as long as its worker row, so seed one.
|
|
const workerName = `test-env-worker-${user.id}`;
|
|
const workerRow = await server.stores.subdomain.create({
|
|
userId: user.id,
|
|
subdomain: `${WORKER_SUBDOMAIN_PREFIX}${workerName}`,
|
|
});
|
|
const { token: workerToken } =
|
|
await server.services.auth.createWorkerSessionToken(
|
|
makeActor({ user }),
|
|
user,
|
|
workerName,
|
|
{},
|
|
{ workerUid: workerRow.uuid },
|
|
);
|
|
|
|
return {
|
|
username: opts.username,
|
|
password: opts.password,
|
|
token,
|
|
apiToken,
|
|
workerToken,
|
|
};
|
|
};
|
|
|
|
export type PuterTestEnv = {
|
|
/**
|
|
* Root origin (`http://puter.localhost:<port>`) — GUI and root-only routes
|
|
* like `POST /login` live here.
|
|
*/
|
|
origin: string;
|
|
/**
|
|
* API origin (`http://api.puter.localhost:<port>`) — what puter.js clients
|
|
* use as their APIOrigin. Routes gated on the `api` subdomain (e.g.
|
|
* `/whoami`) only match this host.
|
|
*/
|
|
apiOrigin: string;
|
|
/**
|
|
* Seeded accounts: an admin and two regular (non-privileged) users. `other`
|
|
* exists so suites can exercise cross-user flows (permission grants, access
|
|
* denials) without creating users on the fly.
|
|
*/
|
|
users: {
|
|
admin: TestUserCredentials;
|
|
user: TestUserCredentials;
|
|
other: TestUserCredentials;
|
|
};
|
|
server: PuterServer;
|
|
shutdown: () => Promise<void>;
|
|
};
|
|
|
|
export const TEST_ADMIN_CREDENTIALS = {
|
|
username: 'admin',
|
|
password: 'puter-test-admin-password',
|
|
};
|
|
export const TEST_USER_CREDENTIALS = {
|
|
username: 'testuser',
|
|
password: 'puter-test-user-password',
|
|
};
|
|
export const TEST_OTHER_USER_CREDENTIALS = {
|
|
username: 'otheruser',
|
|
password: 'puter-test-other-password',
|
|
};
|
|
|
|
/**
|
|
* Boot an in-memory Puter server on a real ephemeral port with deterministic
|
|
* credentials, for client test runners (puter.js on node, browsers, workerd).
|
|
* Clients can authenticate with the pre-minted tokens or via a real `POST
|
|
* /login` using the fixed passwords — no stdout scraping.
|
|
*/
|
|
export const setupPuterTestEnv = async (
|
|
configOverrides?: IConfig,
|
|
): Promise<PuterTestEnv> => {
|
|
// Real hostnames rather than 127.0.0.1: API routes are gated on the
|
|
// `api` subdomain, and `*.localhost` resolves to loopback on modern
|
|
// platforms (and in browsers).
|
|
const domain = 'puter.localhost';
|
|
|
|
// Unlike unit-test servers (extensions: []), the client env loads the
|
|
// real extensions — clients depend on endpoints that live there
|
|
// (e.g. /whoami). Resolved from this module so cwd doesn't matter.
|
|
const extensionsDir = fileURLToPath(
|
|
new URL('../../extensions', import.meta.url),
|
|
);
|
|
|
|
const { server, origin, apiOrigin } = await bootOnFreshPort(
|
|
async (port) => {
|
|
const origin = `http://${domain}:${port}`;
|
|
const apiOrigin = `http://api.${domain}:${port}`;
|
|
const server = await setupTestServer(
|
|
deepMerge(
|
|
{
|
|
port,
|
|
domain,
|
|
origin,
|
|
api_base_url: apiOrigin,
|
|
// The browser runner serves its fixture page from the
|
|
// API origin so the SDK runs same-origin, which makes
|
|
// that origin this env's GUI. A browser overrides any
|
|
// `Origin` header a test sets, so without this the
|
|
// credential routes behind `guiOriginGate` (/login,
|
|
// /signup, /session/sync-cookie) 403 in the browser
|
|
// while passing everywhere else.
|
|
allow_gui_origins: [apiOrigin],
|
|
extensions: [extensionsDir],
|
|
},
|
|
configOverrides ?? {},
|
|
) as IConfig,
|
|
{ listen: true },
|
|
);
|
|
return { server, origin, apiOrigin };
|
|
},
|
|
);
|
|
|
|
try {
|
|
const admin = await createTestUser(server, {
|
|
...TEST_ADMIN_CREDENTIALS,
|
|
admin: true,
|
|
});
|
|
const user = await createTestUser(server, TEST_USER_CREDENTIALS);
|
|
const other = await createTestUser(server, TEST_OTHER_USER_CREDENTIALS);
|
|
|
|
return {
|
|
origin,
|
|
apiOrigin,
|
|
users: { admin, user, other },
|
|
server,
|
|
shutdown: () => server.shutdown(),
|
|
};
|
|
} catch (e) {
|
|
await server.shutdown().catch(() => {});
|
|
throw e;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* `controller` with each handler run inside a request scope of its own, as the
|
|
* router runs it — for tests that call handlers directly. The scope carries the
|
|
* request's actor.
|
|
*/
|
|
export const inRequestScope = <T extends object>(controller: T): T =>
|
|
new Proxy(controller, {
|
|
get(target, prop) {
|
|
const value = Reflect.get(target, prop, target);
|
|
if (typeof value !== 'function') return value;
|
|
return (...args: unknown[]) =>
|
|
runWithContext(
|
|
{
|
|
actor: (args[0] as { actor?: Actor } | undefined)
|
|
?.actor,
|
|
},
|
|
() => value.apply(target, args),
|
|
);
|
|
},
|
|
});
|
|
|
|
/** Adds a replica that, for `queries`, is empty, a copy from now, or current. */
|
|
export const lagReadReplica = async (
|
|
server: PuterServer,
|
|
queries: RegExp,
|
|
{ mode = 'missing' }: { mode?: 'missing' | 'frozen' | 'current' } = {},
|
|
) => {
|
|
type Read = (sql: string, params?: unknown[]) => Promise<unknown[]>;
|
|
const db = server.clients.db as unknown as Record<string, unknown> & {
|
|
read: Read;
|
|
};
|
|
let frozen: SqliteDatabaseClient | null = null;
|
|
let frozenDir: string | null = null;
|
|
if (mode === 'frozen') {
|
|
frozenDir = mkdtempSync(join(tmpdir(), 'puter-replica-'));
|
|
const path = join(frozenDir, 'replica.sqlite');
|
|
await server.clients.db.write('VACUUM INTO ?', [path]);
|
|
frozen = new SqliteDatabaseClient({
|
|
port: 0,
|
|
extensions: [],
|
|
database: { engine: 'sqlite', path },
|
|
} as IConfig);
|
|
await frozen.onServerStart();
|
|
}
|
|
|
|
const names = ['read', 'pread', 'hasReadReplica'];
|
|
const own = names.map((name) => Object.getOwnPropertyDescriptor(db, name));
|
|
const read = db.read.bind(db);
|
|
let primaryReads = 0;
|
|
db.hasReadReplica = () => true;
|
|
db.read = async (sql: string, params: unknown[] = []) => {
|
|
if (mode === 'current' || !queries.test(sql)) return read(sql, params);
|
|
return frozen ? frozen.read(sql, params) : [];
|
|
};
|
|
db.pread = async (sql: string, params: unknown[] = []) => {
|
|
if (queries.test(sql)) primaryReads++;
|
|
return read(sql, params);
|
|
};
|
|
return {
|
|
primaryReads: () => primaryReads,
|
|
catchUp: () => {
|
|
names.forEach((name, i) => {
|
|
const descriptor = own[i];
|
|
if (descriptor) Object.defineProperty(db, name, descriptor);
|
|
else delete db[name];
|
|
});
|
|
frozen?.onServerShutdown();
|
|
if (frozenDir) rmSync(frozenDir, { recursive: true, force: true });
|
|
},
|
|
};
|
|
};
|
|
|
|
/**
|
|
* The most `close` listeners the backend may leave on one response. Request
|
|
* tracing adds up to five of its own, and Node warns past ten.
|
|
*/
|
|
export const RESPONSE_CLOSE_LISTENER_BUDGET = 5;
|
|
|
|
/**
|
|
* Records the most `close` listeners each server response held at once. The
|
|
* hook is process-wide, so call `restore` when done.
|
|
*/
|
|
export const trackResponseCloseListeners = () => {
|
|
const peaks = new Map<ServerResponse, number>();
|
|
const proto = ServerResponse.prototype as unknown as Record<
|
|
string,
|
|
(
|
|
this: ServerResponse,
|
|
event: string | symbol,
|
|
listener: unknown,
|
|
) => unknown
|
|
>;
|
|
const methods = ['on', 'addListener', 'prependListener'];
|
|
const own = methods.map((m) => Object.getOwnPropertyDescriptor(proto, m));
|
|
for (const method of methods) {
|
|
const original = proto[method]!;
|
|
proto[method] = function (event, listener) {
|
|
const result = original.call(this, event, listener);
|
|
if (event === 'close') {
|
|
const count = this.listenerCount('close');
|
|
peaks.set(this, Math.max(peaks.get(this) ?? 0, count));
|
|
}
|
|
return result;
|
|
};
|
|
}
|
|
return {
|
|
/** The highest count on any response to a request `match` accepts. */
|
|
peak: (match: (req: IncomingMessage) => boolean): number =>
|
|
Math.max(
|
|
0,
|
|
...[...peaks]
|
|
.filter(([res]) => match(res.req))
|
|
.map(([, count]) => count),
|
|
),
|
|
restore: () => {
|
|
methods.forEach((method, i) => {
|
|
const descriptor = own[i];
|
|
if (descriptor)
|
|
Object.defineProperty(proto, method, descriptor);
|
|
else delete proto[method];
|
|
});
|
|
},
|
|
};
|
|
};
|