ChartnautDocs

Testing

@chartnaut/charts/testing gives you a terminal without a frame, for your own tests. createMockTerminal() returns the same terminal object your code gets from <Terminal> or createTerminal. It records every command your code sends, answers it, and emits events when your test tells it to.

A first test

Write your terminal code as functions that take a terminal. Your page passes the real one; your tests pass the mock.

import { expect, test } from "vitest";
import type { Terminal } from "@chartnaut/charts";
import { createMockTerminal } from "@chartnaut/charts/testing";

// Your code: lock trading once open losses pass 300.
function lockOnLoss(terminal: Terminal) {
  return terminal.account.on("change", (e) => {
    if ((e.data.open_pnl?.amount ?? 0) < -300) void terminal.orders.lock("Daily loss limit reached");
  });
}

test("locks orders past a 300 loss", async () => {
  const terminal = createMockTerminal();
  lockOnLoss(terminal);
  terminal.emit("ready");
  terminal.emit("account_changed", { at: 1789471800, open_pnl: { amount: -320, currency: "USD" } });
  await Promise.resolve();
  expect(terminal.commands).toContainEqual(expect.objectContaining({ name: "lock_orders", args: { reason: "Daily loss limit reached" } }));
});

On the mock

What it does

emit(type, fields)

Emits an event as the terminal would, with seq, event_id and time filled in. Fields you pass win

commands

Every command sent, in order: { id, name, args }

events

Every event emitted, in order

handle(name, fn)

Answers a command your way from now on: return the result, or throw a ChartnautCommandError

reload()

A new frame load: a new boot, seq back to 1, hello emitted

setTime(seconds)

The time events get when you don't give one

The mock is booting until you emit("ready"), as a real terminal is. Unlike a real one, it answers commands at once, ready or not.

Refusals

import { expect, test } from "vitest";
import { ChartnautCommandError } from "@chartnaut/charts";
import { createMockTerminal } from "@chartnaut/charts/testing";

test("an order the session's rules refuse", async () => {
  const terminal = createMockTerminal({
    handlers: {
      place_order: () => {
        throw new ChartnautCommandError("rule_blocked", "The daily loss rule stops new orders today.", "place_order");
      },
    },
  });
  terminal.emit("ready");
  await expect(terminal.orders.buy("NAS100", { quantity: { quantity: 1, unit: "contracts" } })).rejects.toMatchObject({ code: "rule_blocked" });
});

What the user may do

The mock starts as a Pro user in a partner session who may place orders and drive the replay. Change any part with capabilities:

import { expect, test } from "vitest";
import { createMockTerminal } from "@chartnaut/charts/testing";

test("hides the Buy button when orders aren't allowed", async () => {
  const terminal = createMockTerminal({ capabilities: { orders: { place: { yes: false, reason: "Orders are off in this session" } } } });
  terminal.emit("ready");
  await terminal.ready;
  expect(terminal.can("orders.place")).toEqual({ yes: false, reason: "Orders are off in this session" });
});

Charts, scripts and orders

The mock keeps a small model of the terminal. The layout and charts you give it are laid out at once, and chart commands answer with their events (setSymbol sends symbol_changed with cause: "command"). Scripts, drawings, bars, the replay and orders answer with plausible results, so code that reads them runs end to end.

import { expect, test } from "vitest";
import { createMockTerminal } from "@chartnaut/charts/testing";

test("follows the chart's symbol", async () => {
  const terminal = createMockTerminal({ charts: { main: { symbol: "NAS100", timeframe: "5m" } } });
  const seen: (string | null)[] = [];
  terminal.chart("main").on("symbolChange", (e) => seen.push(e.data.symbol));
  terminal.emit("ready");
  await terminal.chart("main").setSymbol("EURUSD");
  expect(seen).toEqual(["EURUSD"]);
});

React

Hooks take the terminal as their first argument, so a component under test can use the mock where your page uses useTerminal(). Pass it in as a prop, or through your own context.

The examples on these docs pages are checked the same way: each one compiles against the SDK's types, and the plain TypeScript ones run against createMockTerminal().