Files
liqa/test/session.controller.spec.ts
T
ars9 71bcc8faec feat(scenario): step output, getStepOutput helper, export/import MCP tools, doc restructure
- add output column to ScenarioRunStepEntity; exec step return value is JSON-serialised and stored
- expose helpers.getStepOutput(order) in exec code; negative order is relative to current step
- add get_scenario_run, wait_for_scenario_run, export_scenario, import_scenario MCP tools
- move Architecture, Key descriptors, MCP tools, Scenario, Development docs to docs/
- delete CONTRIBUTING.md (replaced by docs/development.md)
2026-04-08 19:02:36 +03:00

152 lines
5.0 KiB
TypeScript

import { INestApplication } from "@nestjs/common";
import { getRepositoryToken } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import request from "supertest";
import { buildTestApp } from "./app.harness";
import { SessionEntity } from "../src/session/session.entity";
describe("SessionController", () => {
let app: INestApplication;
let repo: Repository<SessionEntity>;
beforeAll(async () => {
app = await buildTestApp();
repo = app.get<Repository<SessionEntity>>(
getRepositoryToken(SessionEntity),
);
});
afterAll(async () => {
await app.close();
});
async function seedSession(name: string) {
return repo.save(
repo.create({
sessionName: name,
token: "tok",
cookies: "[]",
localStorage: "{}",
}),
);
}
// ── GET /sessions ──────────────────────────────────────────────────────────
describe("GET /sessions", () => {
it("returns 200 with a paginated result", async () => {
const res = await request(app.getHttpServer())
.get("/sessions")
.expect(200);
expect(Array.isArray(res.body.data)).toBe(true);
expect(typeof res.body.total).toBe("number");
expect(res.body).toHaveProperty("page");
expect(res.body).toHaveProperty("limit");
});
it("includes seeded sessions", async () => {
await seedSession("visible-session");
const res = await request(app.getHttpServer())
.get("/sessions")
.expect(200);
const names = res.body.data.map(
(s: { sessionName: string }) => s.sessionName,
);
expect(names).toContain("visible-session");
});
it("does not expose token, cookies or localStorage fields", async () => {
await seedSession("private-session");
const res = await request(app.getHttpServer())
.get("/sessions")
.expect(200);
const item = res.body.data.find(
(s: { sessionName: string }) => s.sessionName === "private-session",
) as Record<string, unknown>;
expect(item).toBeDefined();
expect(item.token).toBeUndefined();
expect(item.cookies).toBeUndefined();
expect(item.localStorage).toBeUndefined();
});
it("respects page and limit params", async () => {
await seedSession("paged-session-a");
await seedSession("paged-session-b");
const res = await request(app.getHttpServer())
.get("/sessions?page=1&limit=1")
.expect(200);
expect(res.body.data).toHaveLength(1);
expect(res.body.page).toBe(1);
expect(res.body.limit).toBe(1);
});
it("returns empty data array for out-of-range page", async () => {
const res = await request(app.getHttpServer())
.get("/sessions?page=9999&limit=20")
.expect(200);
expect(res.body.data).toHaveLength(0);
});
it("returns 400 for invalid page param", async () => {
await request(app.getHttpServer()).get("/sessions?page=0").expect(400);
});
it("orders by sessionName ASC", async () => {
await seedSession("zzz-sort-session");
await seedSession("aaa-sort-session");
const res = await request(app.getHttpServer())
.get("/sessions?orderBy=sessionName&orderDir=ASC")
.expect(200);
const names: string[] = res.body.data.map(
(s: { sessionName: string }) => s.sessionName,
);
expect(names).toEqual([...names].sort());
});
it("orders by sessionName DESC", async () => {
const res = await request(app.getHttpServer())
.get("/sessions?orderBy=sessionName&orderDir=DESC")
.expect(200);
const names: string[] = res.body.data.map(
(s: { sessionName: string }) => s.sessionName,
);
expect(names).toEqual([...names].sort().reverse());
});
it("returns 400 for invalid orderDir", async () => {
await request(app.getHttpServer())
.get("/sessions?orderDir=SIDEWAYS")
.expect(400);
});
});
// ── DELETE /sessions/:id ───────────────────────────────────────────────────
describe("DELETE /sessions/:id", () => {
it("deletes an existing session and returns 200", async () => {
const s = await seedSession("delete-me-session");
await request(app.getHttpServer())
.delete(`/sessions/${s.id}`)
.expect(200);
const res = await request(app.getHttpServer())
.get("/sessions")
.expect(200);
const names = res.body.data.map(
(sess: { sessionName: string }) => sess.sessionName,
);
expect(names).not.toContain("delete-me-session");
});
it("returns 404 for unknown id", async () => {
await request(app.getHttpServer()).delete("/sessions/99999").expect(404);
});
it("returns 400 for non-numeric id", async () => {
await request(app.getHttpServer()).delete("/sessions/abc").expect(400);
});
});
});