Testing
How to run Carrier's test suite and write tests for your own modules, display codes and call flows. The suite uses Pest.
Run the suite
composer testcomposer test runs pest (the test script in composer.json). Run part
of the suite with Pest directly:
vendor/bin/pest --testsuite=Unit
vendor/bin/pest tests/Unit/AcsTest.php
vendor/bin/pest --filter='drop files'
composer test -- tests/Unit/AcsTest.phpphpunit.xml defines two suites, Unit (tests/Unit) and Feature
(tests/Feature), and sets these variables for every run:
| Variable | Value |
|---|---|
APP_ENV |
testing |
LOG_LEVEL |
warning |
DB_DATABASE |
storage/testing.sqlite |
Tests never touch storage/carrier.sqlite. The feature tests start their own server on free
ports, so a board running on the default ports doesn't get in the way. Two test runs in the
same checkout at once do collide: both delete and rebuild storage/testing.sqlite and
storage/testing-feature.sqlite.
Test the starter project
composer test:skeletonThis runs tools/skeleton/test.sh. It builds a new board from
skeleton/ and packages/framework/ in a temporary folder, the way composer create-project
will. Then it installs the games and amber kits, checks that reinstalling reports existing
files as kept, starts the board on ports 2391 (telnet) and 8091 (web), applies as the first caller over
telnet, and loads the web terminal. Set KEEP=1 to keep the folder.
== board and web installed, database migrated
== games and amber installed; reinstalling keeps existing files
== web terminal answers on :8091
== applied over telnet; the first caller became the sysop
== reached the main menu
== logged off
== skeleton OKThe suite below lives in this repository. The starter project ships no test setup yet: to test
your own board, add Pest to it and copy the helpers you need from tests/.
Layout
tests/
Pest.php helpers: container(), fake_connection(), server()
Support/
TestServer.php runs `php carrier serve` on free ports, once per run
BbsClient.php a scripted telnet caller with a virtual screen
Unit/ CP437, keys, pipe codes, telnet, SAUCE, ANSI, ACS, SSH, drop files, kits,
addresses, the input buffer, login throttling
Feature/ TelnetFlowTest (apply, post, log off, two nodes), TransportsTest (SSH, rlogin, web),
HardeningTest (input floods, connection caps)Helpers in tests/Pest.php
| Helper | Returns | Use it for |
|---|---|---|
container() |
the Tempest Container, booted once per run |
getting framework and app services (Acs, Mci, ModuleRunner, NodeManager …) in unit tests |
fake_connection(string $protocol = 'telnet', bool $secure = false) |
a BufferedConnection that records output |
building a Session without a socket |
server() |
the shared TestServer |
feature tests that call in over the network |
container() deletes storage/testing.sqlite, boots Tempest against it with internal storage
in .carrier/, runs every migration (no seeders), and restores PHPUnit's error and exception
handlers. Discovery runs as it does in production, so your #[DisplayCode], #[AccessCheck],
#[Door] and menu classes are registered.
The fake connection has two extras:
| Member | Does |
|---|---|
feed(string $bytes) |
queues input, as if the caller typed it |
$written |
every byte the BBS wrote, escape sequences included |
Pass $protocol and $secure to test ACS codes that look at the transport, such as
PR[ssh] or SC.
TestServer
TestServer::shared() starts one server for the whole run:
- picks four free ports on 127.0.0.1;
- deletes
storage/testing-feature.sqliteand runsmigrate --fresh --seedagainst it; - creates the account
Sysop/sysop123withuser:create --sysop; - starts
php carrier serve --quietwithBBS_NODES=8, writing stderr tostorage/logs/test-server.log; - waits up to 10 seconds for the web port, and stops the server at shutdown.
| Member | |
|---|---|
$telnet, $ssh, $rlogin, $web |
the ports it chose |
telnet(int $cols = 80, int $rows = 25, bool $utf8 = true) |
a connected BbsClient |
stop() |
send SIGTERM (also registered as a shutdown function) |
If the server doesn't come up, the error points at storage/logs/test-server.log.
BbsClient
BbsClient behaves like a telnet terminal. It negotiates
NAWS and TTYPE, answers the cursor-position probes, and keeps everything it receives in
$output.
| Method | Does |
|---|---|
send(string $text) |
write raw bytes |
line(string $text) |
write $text followed by CR |
key(string $name) |
send up, down, left, right, enter, escape, backspace, or the string itself |
expect(string $pattern, float $timeout = 5.0) |
wait until the output, with escape sequences stripped, matches the regex; returns the match or throws with a dump of the screen |
clear() |
drain pending input and forget output so far, so the next expect() only sees new output |
plain() |
output so far without escape sequences |
screen() / screenText() |
the screen since the last clear-screen, as a Canvas or as text |
pump(float $seconds = 0.1) |
read from the socket for a while |
isClosed(), close() |
connection state |
expect() throws a RuntimeException that includes the rendered screen and the last 800
characters of output, so a failed step shows what the caller saw.
Write a unit test for a display code
This test covers the app's |NM code (unread mail, from
BoardCodes.php). Put it in tests/Unit/:
<?php
declare(strict_types=1);
use App\Models\Mail;
use Carrier\Art\Mci;
use Carrier\Auth\User;
use Carrier\Session\NodeManager;
use Carrier\Session\Session;
use Carrier\Terminal\Terminal;
/** A session on node 1 with a fake connection, logged in as $user. */
function caller(User $user): Session
{
$connection = fake_connection();
$session = new Session(1, new Terminal($connection), $connection, container()->get(NodeManager::class));
$session->user = $user;
return $session;
}
it('counts unread mail in |NM', function () {
$mci = container()->get(Mci::class);
$user = User::register('Reader' . random_int(1000, 9999), 'secret99', 20);
$session = caller($user);
expect($mci->resolve('NM', $session))->toBe('0');
Mail::create(toUserId: $user->key(), fromName: 'Sysop', subject: 'Hi', body: 'Welcome aboard', createdAt: new DateTimeImmutable());
expect($mci->resolve('NM', $session))->toBe('1');
});- Call
container()before touching the database: it creates and migrates the test database. Mci::resolve()returns the code's value as a string,''when the resolver throws, andnullfor an unknown code.- Pest loads every test file into one process, so helper function names like
caller()must be unique across the suite.
ACS checks work the same way, through container()->get(Acs::class)->check('NM1', $session).
AcsTest.php builds sessions with an unsaved User this way, for
checks that don't need the database.
Write a unit test for a module
Run the module through ModuleRunner, as a menu would, and feed the keys it waits for before
running it:
use App\Modules\WhosOnline;
use Carrier\Menu\ModuleRunner;
it("lists free nodes in Who's Online", function () {
$modules = container()->get(ModuleRunner::class);
$session = caller(User::register('Watcher' . random_int(1000, 9999), 'secret99', 20));
$session->connection->feed(' '); // answers the "Press any key" at the end
$modules->run(WhosOnline::class, $session);
$screen = preg_replace('/\e\[[0-9;?]*[A-Za-z]/', '', $session->connection->written);
expect($screen)->toContain("Who's online")
->toContain('waiting for a call');
});run() resolves the module from the container, so its constructor dependencies are injected
as they would be on a live board. It returns the module's Next, or null when the module
wants the menu redrawn. If a module waits for more input than you fed, the test fails with
Revolt's "Event loop terminated without resuming the current suspension" error. Feed every key
the flow needs.
A session built this way is not registered with NodeManager, so it doesn't appear in node
lists, Who's Online or broadcasts.
Write a feature test that calls in
Feature tests run against the real server. Put them in tests/Feature/:
<?php
declare(strict_types=1);
it('logs the sysop in over telnet', function () {
$client = server()->telnet();
$client->expect('/Press any key/', 8); // ShowConnectArt
$client->clear()->send(' ');
$client->expect('/Hang up/'); // the matrix menu
$client->clear()->send('L');
$client->expect('/Password/');
$client->send("Sysop\r");
usleep(150_000);
$client->clear()->send("sysop123\r"); // TestServer creates Sysop / sysop123
$client->expect('/Welcome back/', 8);
expect($client->plain())->toContain('Welcome back, Sysop');
$client->close();
});- Call
clear()before the key that triggers the next screen. Otherwiseexpect()can match text that was already on screen. - The first
expect()gets a longer timeout because the connection probe and the connect art come first. - TelnetFlowTest.php has reusable steps:
matrix()(past the connect art),login()andtoMain()(through new mail, bulletins, oneliners and last callers to the main menu). Those functions are defined in that file. Copy them into a shared file such astests/Pest.phpbefore using them elsewhere. - Two callers at once: open two clients with
server()->telnet(). The teleconference test does this to check that a line typed on one node arrives on the other. - TransportsTest.php shows SSH (phpseclib against
server()->ssh), rlogin and websocket clients against the same server.
The feature server shares one database for the whole run, so give accounts you create unique
handles (the apply test uses 'Newbie' . random_int(100, 999)).
Both example tests above passed with vendor/bin/pest against a copy of this repository.