// ABOUTME: Integration tests against the live Minesweeper Rails API.
// ABOUTME: Exercises authenticated and public endpoints with real requests.
import assert from "node:assert/strict";
import { test } from "node:test";
import { loadConfig } from "../../src/config.js";
import { RailsClient } from "../../src/rails/client.js";
const baseUrl = process.env.MINESWEEPER_BASE_URL;
const bearerToken = process.env.MINESWEEPER_BEARER_TOKEN;
const userSlug = process.env.MINESWEEPER_USER_SLUG;
function requireEnv(value, name) {
if (!value) {
throw new Error(`Missing required env var: ${name}`);
}
return value;
}
test("Rails API start/state/open/end flow", async () => {
const config = loadConfig({
MINESWEEPER_BASE_URL: requireEnv(baseUrl, "MINESWEEPER_BASE_URL"),
MINESWEEPER_BEARER_TOKEN: requireEnv(bearerToken, "MINESWEEPER_BEARER_TOKEN"),
});
const client = new RailsClient(config);
const start = await client.startGame(requireEnv(userSlug, "MINESWEEPER_USER_SLUG"));
assert.ok(start.public_id);
const state = await client.getState(start.public_id);
assert.equal(state.game.status, "playing");
const opened = await client.openCell(start.public_id, 0, 0);
assert.ok(opened.game.board.length > 0);
const ended = await client.endGame(start.public_id);
assert.ok(["playing", "won", "lost", "ended"].includes(ended.game.status));
});
test("Rails API can list games without auth", async () => {
const config = loadConfig({
MINESWEEPER_BASE_URL: requireEnv(baseUrl, "MINESWEEPER_BASE_URL"),
});
const client = new RailsClient(config);
const games = await client.listGames(requireEnv(userSlug, "MINESWEEPER_USER_SLUG"));
assert.ok(Array.isArray(games.games));
});