Files
puter/src/puter-js/tests/api/README.md
T
Daniel Salazar 9ca1acf634 Benchmarks that gate PRs on I/O regressions, wider test baseline, and npm run check (#4644)
* feat(bench): backend benchmarks that fail a PR for waterfalled or uncached I/O

A scenario runs a request against the in-memory backend and records every
SQL, key-value, Redis and object-storage round trip on a modeled clock, then
reports round trips per destination, serial stages and modeled latency.
tools/bench.mjs runs the scenarios on a base commit and on the head and fails
on a regression the pull request description does not accept; benchmarks.yaml
runs that on every pull request.

Also adds npm run check (lint, type check, related tests and the benchmark
comparison for the files a branch changed) and describes the distributed
deployment in AGENTS.md.

* test(bench): scenarios for fs, kv, auth, apps, permissions, sharing and hosted sites

* test(puter-js): count the SDK unit tests in the coverage report

Only the bundle the API runners load was instrumented, so the unit tests next
to the SDK source never counted. In coverage mode the vitest config now
instruments SDK source for them and writes one shard per test file.

* test(puter-js): unit tests for ui messaging, dialogs, components, uploads, peer and smaller modules

* test(backend): cover fs, permission, app, worker, sharing, broadcast and user edge cases

* fix(backend): fs write flags are stored as booleans, app stats weeks are ISO weeks

- A write's boolean flags reach the store only once read as booleans; any
  positive number reads as true.
- App open stats grouped by week use ISO 8601 weeks for both the buckets
  and their labels on every database engine, so the two agree across a
  year boundary.

* fix(puter-js): dialog token source, legacy upload cancel, submenu state, typed-array views

- The sign-in dialog takes the token only from the popup it opened, and
  stops listening when it leaves the page.
- Cancelling a legacy upload rejects with `upload_aborted` and no longer
  runs the `error` callback.
- A context menu's submenu stays in step with its parent when the parent
  item is pressed or the submenu closes itself.
- The FileReader polyfill reads a typed-array view through its own offset
  and length.

* ci(bench): benchmark a pull request as merged into its base

The comparison ran the branch's own head against the tip of the base
branch, so a branch behind its base was charged for (or credited with)
what the base gained since. The head side is now the merge commit and
the base side its first parent. Shards also get a 15 minute timeout.

* test(broadcast): shutdown drain tests send boot's own events first

Boot queues cache updates that flush 10 ms later. A test body that
started after that flush counted it as its own request, which failed
'bounds a hung flush in onServerShutdown' on a slow runner.
2026-11-14 13:57:03 -07:00

4.0 KiB

puter.js API test environment

Client-agnostic test suites for puter.js, run against a self-contained in-memory Puter server — no external server, no stdout password scraping, no .env. The same suite files execute on three platforms through thin adapters:

Runner Platform How the SDK runs
runners/node.test.ts node.js Built SDK bundle loaded into a fresh vm context per test (like src/init.cjs)
runners/browser.test.ts headless Chromium (playwright) Fixture page served same-origin on the API host loads /puter.js/v2 from the server itself
runners/workerd.test.ts local workerd (Miniflare) Suite bundle deployed as a real Puter worker via puter.workers.create, dispatched through the local worker proxy

Running

Build the SDK bundle and worker preamble once (repeat after SDK changes):

npm run build:workerLib

Then, from the package root:

npm run test:puterjs            # all three platforms
npm run test:puterjs:node
npm run test:puterjs:browser    # needs `npx playwright install chromium` once
npm run test:puterjs:workerd

Coverage

npm run test:puterjs:coverage

This builds an istanbul-instrumented SDK bundle, runs the three runners and the SDK unit tests (src/puter-js/src/**/*.test.js), and merges everything into src/puter-js/coverage/ (index.html locally, coverage-summary.json for CI). The unit tests import SDK source directly, so in coverage mode the vitest config instruments that source the same way and each unit test file writes a shard of its own next to the runners'.

To see what one unit test file covers without the full run:

PUTER_COVERAGE=1 npx vitest run --config src/puter-js/tests/api/vitest.config.ts src/puter-js/src/modules/kv
node tools/puterjsCoverageReport.mjs

How it works

Each runner boots setupPuterTestEnv() (from src/backend/testUtil.ts) in beforeAll: a fully in-memory backend (sqlite / dynalite / redis-mock / fauxqs S3) listening on a real ephemeral port, with the production extensions loaded and two deterministic users seeded:

  • admin — member of the admin group,
  • testuser — a regular, non-privileged user (what suites run as).

The env manifest ({ origin, apiOrigin, users } with fixed passwords and pre-minted session tokens) is JSON-serializable and crosses into whatever runtime executes the tests. Root-only routes (e.g. POST /login) live on origin; the SDK talks to apiOrigin (the api. subdomain host).

Adding tests

Tests are added once and run on all three platforms — never write a per-platform test here.

  1. Existing area (apps, auth, fs, kv): add a test to the matching suites/<name>.suite.ts — one entry in the object, key is the test name, value gets the context t.
  2. New area (e.g. hosting): create suites/hosting.suite.ts and register it in suites/index.ts (explicit list, no globbing — esbuild bundles exactly this list for the browser/workerd runners).
import { suite } from '../harness/types.ts';

export default suite('example', {
    'does the thing': async (t) => {
        await t.puter.fs.write(`/${t.env.users.user.username}/x.txt`, 'hi');
        t.assert.ok(await t.puter.fs.stat(/* … */));
    },
});

Rules that keep a suite runnable everywhere:

  • Platform-agnostic only. No node/browser/workerd-specific imports — a suite may use the SDK instance (t.puter, authed as the regular user), global fetch, and t.assert (ok/equal/deepEqual/rejects).
  • Admin or cross-user assertions go through plain fetch with t.env.users.admin.token (see auth.suite.ts) — that works identically on every platform.
  • Unique resource names per test (file paths, kv keys): tests in a suite share one server and one user, so don't reuse names across tests.
  • The runners in runners/ enumerate suites automatically — adding a suite requires no runner changes.

Iterate fast with npm run test:puterjs:node (boots in ~3s); run npm run test:puterjs before pushing to cover browser and workerd too.