Run and test a station
Check a station on your machine before a line runs it: run it by hand while you write it, then keep tests that run it the same way.
Run it by hand
Run a station with the config in its station file:
npx factory stations run IN1
To try another value, change that key for one run with --set key=value (repeatable; the value is read as JSON, and otherwise kept as text): --set approval_limit=1000. A station with no station file yet takes --empty, which runs it with an empty config. npx factory stations list lists the stations in src/stations/.
A station with a conveyor first gets one conveyor pass, as in a factory session: the finished workpieces in its conveyor.from bins move into its input/. Then it runs.
| What you want to see | Where it is |
|---|---|
| The config the station got, and any error | The Config: lines and the error, in the terminal |
| What the station returned | The terminal, and temp/IN1.json |
| Its report, documents and workpiece outcomes | temp/work_records/local-wr-…/: report.md, workpieces.json and one file per document |
| Its workpieces | The real bins under temp/stations/: al-control lines status IN -f . |
A local run is a real run. It moves workpieces through the bins, and anything it sends to another system is sent. Every option of npx factory is in the lib-factory README.
Test it
The factory template runs tests with Jest (npm test, files under tests/). Factories test a station in three ways.
Run the whole station, as a factory session does
Pass the station's definition to createStationHandler and call the handler with a task. It checks the config against configSchema and runs execute, just as in a factory session. A work record id that starts with local- makes every attach call write its file under temp/ instead of calling the Orchestrator.
Run it in an empty temporary folder. lib-factory fixes the location of temp/ from the current folder when it is first imported, so change folder before you import it or the station. From the factory template's tests/example-line.test.js:
const run = (definition, config) =>
lib.createStationHandler(definition)({ station: { code: definition.code, config }, work_record: { id: 'local-test' } });
beforeAll(async () => {
root = fs.mkdtempSync(path.join(os.tmpdir(), 'factory-acme-'));
fs.cpSync(path.join(repo_root, 'samples'), path.join(root, 'samples'), { recursive: true });
process.chdir(root);
lib = await import('@assemblyline/lib-factory');
intake = (await import(path.join(repo_root, 'src/stations/EX0__intake/station.js'))).default;
});
afterAll(() => {
process.chdir(repo_root);
fs.rmSync(root, { recursive: true, force: true });
});
it('seeds one workpiece per sample, once', async () => {
expect(await run(intake, { source_dir: 'samples' })).toEqual({ seeded_count: 3, skipped_count: 0 });
expect(await run(intake, { source_dir: 'samples' })).toEqual({ seeded_count: 0, skipped_count: 3 });
});
The second call in that test is a rerun: a station must be safe to run again, so it's worth a test. Assert on what the station leaves behind as well as what it returns: the workpieces in each bin (lib.listWorkpieces(lib.bin('EX1', 'failed'))), the files it wrote, and the error.txt of a workpiece that should fail. To test a whole line, stand in for the conveyor by renaming each workpiece from one station's output/ into the next one's input/ (fs.renameSync), as the factory session would.
Check that the station file's config fits the handler
A station file and its handler can drift apart: a renamed setting, a new required one. Parse the station file's config with the handler's own schema:
it('accepts the config in its station file', () => {
const file = JSON.parse(fs.readFileSync('.orchestrator/stations/IN1__check.json', 'utf8'));
expect(() => station.configSchema.parse(file.config)).not.toThrow();
});
Test the logic on its own
Keep the decisions in plain functions, in a file next to station.js, and test them without files or a run:
// src/stations/IN1__check/validate-invoice.js
export function validateInvoice(invoice) { /* returns a list of problems */ }
import { validateInvoice } from '../src/stations/IN1__check/validate-invoice.js';
it('rejects a date that does not exist', () => {
expect(validateInvoice({ ...VALID, date: '2026-02-31' })).not.toHaveLength(0);
});
The factory session imports only station.js from a station's folder, so other files there don't change what runs.
Related
- Write a station: the schema these tests exercise
- Throw errors from a station: the failures worth a test
- Use with AI agents: let an agent run the write, run and fix loop