- list the snippets a scenario uses in the export dialog, checked by default, with the versions the scenario uses - export each selected snippet once per used version (latest and pinned) via the new snippetIds query parameter - resolve used snippets from the exported version's steps - show the scenario version in the export dialog - document the export query parameters - bump version to 1.22.0
435 lines
16 KiB
TypeScript
435 lines
16 KiB
TypeScript
/**
|
|
* Integration tests for scenario and snippet versioning:
|
|
* - versions bump on content changes only
|
|
* - runs record and execute the scenario version they were created with
|
|
* - runSnippet("alias@N") pins a snippet version
|
|
* - export/import carry and restore exact versions
|
|
*/
|
|
|
|
import { INestApplication } from "@nestjs/common";
|
|
import request from "supertest";
|
|
import { DataSource } from "typeorm";
|
|
import { buildTestApp } from "./app.harness";
|
|
import { CredentialEntity } from "../src/credential/credential.entity";
|
|
import { EnvironmentEntity } from "../src/environment/environment.entity";
|
|
import { ScenarioSchedulerService } from "../src/scenario/scenario-scheduler.service";
|
|
import { ScenarioService } from "../src/scenario/scenario.service";
|
|
import { SnippetService } from "../src/snippet/snippet.service";
|
|
|
|
describe("Versioning", () => {
|
|
let app: INestApplication;
|
|
let dataSource: DataSource;
|
|
let scenarioService: ScenarioService;
|
|
let snippetService: SnippetService;
|
|
let scheduler: ScenarioSchedulerService;
|
|
|
|
beforeAll(async () => {
|
|
app = await buildTestApp();
|
|
dataSource = app.get(DataSource);
|
|
scenarioService = app.get(ScenarioService);
|
|
snippetService = app.get(SnippetService);
|
|
scheduler = app.get(ScenarioSchedulerService);
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await app.close();
|
|
});
|
|
|
|
// ── helpers ────────────────────────────────────────────────────────────────
|
|
|
|
const uniq = () => Math.random().toString(36).slice(2, 8);
|
|
|
|
async function createEnvironment(): Promise<string> {
|
|
const repo = dataSource.getRepository(EnvironmentEntity);
|
|
const env = await repo.save(
|
|
repo.create({ name: `env-${uniq()}`, data: {} }),
|
|
);
|
|
return env.id;
|
|
}
|
|
|
|
async function versionNumbers(scenarioId: string): Promise<number[]> {
|
|
return (await scenarioService.listVersions(scenarioId)).map(
|
|
(v) => v.version,
|
|
);
|
|
}
|
|
|
|
/** Creates and processes a run; returns it with its step runs. */
|
|
async function runToCompletion(scenarioId: string, version?: number) {
|
|
const run = await scenarioService.createRun(
|
|
scenarioId,
|
|
await createEnvironment(),
|
|
false,
|
|
undefined,
|
|
version,
|
|
);
|
|
await scheduler.pickUpPendingRuns();
|
|
return scenarioService.waitForRun(scenarioId, run.id, 10_000);
|
|
}
|
|
|
|
const outputs = (run: { stepRuns: { output: string | null }[] }) =>
|
|
run.stepRuns.map((s) => (s.output ? JSON.parse(s.output) : null));
|
|
|
|
// ── scenario versions ─────────────────────────────────────────────────────
|
|
|
|
describe("scenario versions", () => {
|
|
it("starts at version 1 with a stored snapshot", async () => {
|
|
const sc = await scenarioService.create({ name: `v-${uniq()}` });
|
|
expect(sc.version).toBe(1);
|
|
expect(await versionNumbers(sc.id)).toEqual([1]);
|
|
});
|
|
|
|
it("bumps on scenario and step changes, not on no-op updates", async () => {
|
|
const sc = await scenarioService.create({ name: `bump-${uniq()}` });
|
|
await scenarioService.update(sc.id, { name: `${sc.name}-renamed` });
|
|
expect((await scenarioService.findOne(sc.id)).version).toBe(2);
|
|
|
|
// Same content again: no new version
|
|
await scenarioService.update(sc.id, { name: `${sc.name}-renamed` });
|
|
expect((await scenarioService.findOne(sc.id)).version).toBe(2);
|
|
|
|
const step = await scenarioService.createStep(sc.id, {
|
|
execCode: "return 1;",
|
|
});
|
|
expect((await scenarioService.findOne(sc.id)).version).toBe(3);
|
|
await scenarioService.updateStep(sc.id, step.id, {
|
|
execCode: "return 2;",
|
|
});
|
|
expect((await scenarioService.findOne(sc.id)).version).toBe(4);
|
|
await scenarioService.removeStep(sc.id, step.id);
|
|
expect((await scenarioService.findOne(sc.id)).version).toBe(5);
|
|
|
|
expect(await versionNumbers(sc.id)).toEqual([5, 4, 3, 2, 1]);
|
|
const v4 = await scenarioService.findVersion(sc.id, 4);
|
|
expect(v4.steps.map((s) => s.execCode)).toEqual(["return 2;"]);
|
|
});
|
|
|
|
it("does not bump on credential assignment", async () => {
|
|
const sc = await scenarioService.create({ name: `cred-${uniq()}` });
|
|
const credRepo = dataSource.getRepository(CredentialEntity);
|
|
const cred = await credRepo.save(
|
|
credRepo.create({ name: "c", data: null }),
|
|
);
|
|
await scenarioService.addScenarioCredential(sc.id, {
|
|
credentialId: cred.id,
|
|
alias: "c",
|
|
});
|
|
expect((await scenarioService.findOne(sc.id)).version).toBe(1);
|
|
});
|
|
|
|
it("exposes versions over HTTP", async () => {
|
|
const sc = await scenarioService.create({ name: `http-${uniq()}` });
|
|
await scenarioService.createStep(sc.id, { execCode: "return 1;" });
|
|
|
|
const list = await request(app.getHttpServer())
|
|
.get(`/scenarios/${sc.id}/versions`)
|
|
.expect(200);
|
|
expect(list.body.map((v: { version: number }) => v.version)).toEqual([
|
|
2, 1,
|
|
]);
|
|
|
|
const one = await request(app.getHttpServer())
|
|
.get(`/scenarios/${sc.id}/versions/2`)
|
|
.expect(200);
|
|
expect(one.body.steps).toHaveLength(1);
|
|
|
|
await request(app.getHttpServer())
|
|
.get(`/scenarios/${sc.id}/versions/99`)
|
|
.expect(404);
|
|
});
|
|
});
|
|
|
|
// ── runs ──────────────────────────────────────────────────────────────────
|
|
|
|
describe("runs", () => {
|
|
it("runs the latest version by default and records it", async () => {
|
|
const sc = await scenarioService.create({ name: `run-${uniq()}` });
|
|
await scenarioService.createStep(sc.id, { execCode: "return 'latest';" });
|
|
|
|
const run = await runToCompletion(sc.id);
|
|
expect(run.scenarioVersion).toBe(2);
|
|
expect(outputs(run)).toEqual(["latest"]);
|
|
});
|
|
|
|
it("runs an older version's steps when asked", async () => {
|
|
const sc = await scenarioService.create({ name: `old-${uniq()}` });
|
|
const step = await scenarioService.createStep(sc.id, {
|
|
title: "first",
|
|
execCode: "return 'v2';",
|
|
});
|
|
await scenarioService.updateStep(sc.id, step.id, {
|
|
execCode: "return 'v3';",
|
|
});
|
|
await scenarioService.createStep(sc.id, { execCode: "return 'extra';" });
|
|
|
|
const run = await runToCompletion(sc.id, 2);
|
|
expect(run.scenarioVersion).toBe(2);
|
|
expect(run.stepRuns).toHaveLength(1);
|
|
expect(run.stepRuns[0].title).toBe("first");
|
|
expect(run.stepRuns[0].execCode).toBe("return 'v2';");
|
|
expect(outputs(run)).toEqual(["v2"]);
|
|
});
|
|
|
|
it("rejects a version that does not exist", async () => {
|
|
const sc = await scenarioService.create({ name: `nov-${uniq()}` });
|
|
const environmentId = await createEnvironment();
|
|
await request(app.getHttpServer())
|
|
.post(`/scenarios/${sc.id}/run`)
|
|
.send({ environmentId, version: 7 })
|
|
.expect(404);
|
|
});
|
|
|
|
it("keeps run history when a step is deleted", async () => {
|
|
const sc = await scenarioService.create({ name: `hist-${uniq()}` });
|
|
const step = await scenarioService.createStep(sc.id, {
|
|
execCode: "return 1;",
|
|
});
|
|
const run = await runToCompletion(sc.id);
|
|
await scenarioService.removeStep(sc.id, step.id);
|
|
|
|
const after = await scenarioService.findRun(sc.id, run.id);
|
|
expect(after.stepRuns).toHaveLength(1);
|
|
expect(after.stepRuns[0].scenarioStepId).toBeNull();
|
|
expect(after.stepRuns[0].execCode).toBe("return 1;");
|
|
});
|
|
});
|
|
|
|
// ── snippet versions ──────────────────────────────────────────────────────
|
|
|
|
describe("snippet versions", () => {
|
|
it("bumps on change only and keeps every version", async () => {
|
|
const alias = `snip-${uniq()}`;
|
|
const sn = await snippetService.create({
|
|
alias,
|
|
title: "t",
|
|
code: "return 1;",
|
|
});
|
|
expect(sn.version).toBe(1);
|
|
|
|
await snippetService.update(sn.id, { code: "return 2;" });
|
|
await snippetService.update(sn.id, { code: "return 2;" }); // no-op
|
|
expect((await snippetService.findOne(sn.id)).version).toBe(2);
|
|
|
|
const versions = await snippetService.listVersions(sn.id);
|
|
expect(versions.map((v) => [v.version, v.code])).toEqual([
|
|
[2, "return 2;"],
|
|
[1, "return 1;"],
|
|
]);
|
|
});
|
|
|
|
it("runSnippet uses the latest by default and alias@N pins a version", async () => {
|
|
const alias = `pin-${uniq()}`;
|
|
const sn = await snippetService.create({
|
|
alias,
|
|
title: "t",
|
|
code: "return 'one';",
|
|
});
|
|
await snippetService.update(sn.id, { code: "return 'two';" });
|
|
|
|
const sc = await scenarioService.create({ name: `pinned-${uniq()}` });
|
|
await scenarioService.createStep(sc.id, {
|
|
execCode: `return [await context.runSnippet("${alias}"), await context.runSnippet("${alias}@1")];`,
|
|
});
|
|
|
|
const run = await runToCompletion(sc.id);
|
|
expect(run.status).toBe("pass");
|
|
expect(outputs(run)).toEqual([["two", "one"]]);
|
|
|
|
const used = await scenarioService.getUsedSnippets(sc.id);
|
|
expect(used.map((u) => [u.alias, u.pinnedVersion])).toEqual([
|
|
[alias, null],
|
|
[alias, 1],
|
|
]);
|
|
});
|
|
|
|
it("fails the step for a snippet version that does not exist", async () => {
|
|
const alias = `missing-${uniq()}`;
|
|
await snippetService.create({ alias, title: "t", code: "return 1;" });
|
|
const sc = await scenarioService.create({ name: `miss-${uniq()}` });
|
|
await scenarioService.createStep(sc.id, {
|
|
execCode: `return context.runSnippet("${alias}@5");`,
|
|
});
|
|
const run = await runToCompletion(sc.id);
|
|
expect(run.status).toBe("fail");
|
|
});
|
|
});
|
|
|
|
// ── export / import ───────────────────────────────────────────────────────
|
|
|
|
describe("scenario export/import", () => {
|
|
it("exports the latest version, or the one asked for", async () => {
|
|
const sc = await scenarioService.create({ name: `exp-${uniq()}` });
|
|
await scenarioService.createStep(sc.id, { execCode: "return 1;" });
|
|
|
|
const latest = await scenarioService.exportScenario(sc.id, {
|
|
includeEnvironment: false,
|
|
credentialIds: [],
|
|
});
|
|
expect(latest[latest.length - 1]).toMatchObject({
|
|
version: 2,
|
|
steps: [{ execCode: "return 1;" }],
|
|
});
|
|
|
|
const first = await scenarioService.exportScenario(sc.id, {
|
|
includeEnvironment: false,
|
|
credentialIds: [],
|
|
version: 1,
|
|
});
|
|
expect(first[first.length - 1]).toMatchObject({ version: 1, steps: [] });
|
|
});
|
|
|
|
it("includes selected used snippets at the versions the scenario uses", async () => {
|
|
const alias = `exp-snip-${uniq()}`;
|
|
const sn = await snippetService.create({
|
|
alias,
|
|
title: "t",
|
|
code: "return 'one';",
|
|
});
|
|
await snippetService.update(sn.id, { code: "return 'two';" });
|
|
const other = await snippetService.create({
|
|
alias: `exp-other-${uniq()}`,
|
|
title: "t",
|
|
code: "return 3;",
|
|
});
|
|
|
|
const sc = await scenarioService.create({ name: `exp-sn-${uniq()}` });
|
|
await scenarioService.createStep(sc.id, {
|
|
execCode: `await context.runSnippet("${alias}"); await context.runSnippet("${alias}@1"); await context.runSnippet("${alias}"); return context.runSnippet("${other.alias}");`,
|
|
});
|
|
|
|
const withSnippet = await scenarioService.exportScenario(sc.id, {
|
|
includeEnvironment: false,
|
|
credentialIds: [],
|
|
snippetIds: [sn.id],
|
|
});
|
|
expect(withSnippet.map((e) => e.kind)).toEqual([
|
|
"snippet",
|
|
"snippet",
|
|
"scenario",
|
|
]);
|
|
expect(withSnippet.slice(0, 2)).toMatchObject([
|
|
{ id: sn.id, version: 2, code: "return 'two';" },
|
|
{ id: sn.id, version: 1, code: "return 'one';" },
|
|
]);
|
|
|
|
const without = await scenarioService.exportScenario(sc.id, {
|
|
includeEnvironment: false,
|
|
credentialIds: [],
|
|
});
|
|
expect(without.map((e) => e.kind)).toEqual(["scenario"]);
|
|
});
|
|
|
|
it("restores exact versions by id + version", async () => {
|
|
const id = crypto.randomUUID();
|
|
const base = { kind: "scenario" as const, id, name: "imported" };
|
|
|
|
// New scenario: stored under the given version
|
|
let sc = await scenarioService.importScenario({
|
|
...base,
|
|
version: 3,
|
|
steps: [{ title: null, execCode: "return 3;" }],
|
|
});
|
|
expect(sc.version).toBe(3);
|
|
expect(await versionNumbers(id)).toEqual([3]);
|
|
|
|
// Same version again: left untouched
|
|
sc = await scenarioService.importScenario({
|
|
...base,
|
|
name: "changed",
|
|
version: 3,
|
|
steps: [],
|
|
});
|
|
expect(sc.name).toBe("imported");
|
|
expect(sc.steps).toHaveLength(1);
|
|
|
|
// Lower, missing version: stored, latest unchanged
|
|
sc = await scenarioService.importScenario({
|
|
...base,
|
|
version: 1,
|
|
steps: [{ title: null, execCode: "return 1;" }],
|
|
});
|
|
expect(sc.version).toBe(3);
|
|
expect(await versionNumbers(id)).toEqual([3, 1]);
|
|
expect((await scenarioService.findVersion(id, 1)).steps[0].execCode).toBe(
|
|
"return 1;",
|
|
);
|
|
|
|
// Higher version: becomes the latest
|
|
sc = await scenarioService.importScenario({
|
|
...base,
|
|
name: "v5",
|
|
version: 5,
|
|
steps: [{ title: null, execCode: "return 5;" }],
|
|
});
|
|
expect(sc.version).toBe(5);
|
|
expect(sc.name).toBe("v5");
|
|
expect(sc.steps.map((s) => s.execCode)).toEqual(["return 5;"]);
|
|
expect(await versionNumbers(id)).toEqual([5, 3, 1]);
|
|
});
|
|
});
|
|
|
|
describe("snippet export/import", () => {
|
|
it("export includes the version and import requires it", async () => {
|
|
const alias = `sexp-${uniq()}`;
|
|
const sn = await snippetService.create({
|
|
alias,
|
|
title: "t",
|
|
code: "return 1;",
|
|
});
|
|
await snippetService.update(sn.id, { code: "return 2;" });
|
|
|
|
const latest = await request(app.getHttpServer())
|
|
.get(`/snippets/${sn.id}/export`)
|
|
.expect(200);
|
|
expect(latest.body).toMatchObject({ version: 2, code: "return 2;" });
|
|
|
|
const first = await request(app.getHttpServer())
|
|
.get(`/snippets/${sn.id}/export?version=1`)
|
|
.expect(200);
|
|
expect(first.body).toMatchObject({ version: 1, code: "return 1;" });
|
|
|
|
await request(app.getHttpServer())
|
|
.post("/snippets/import")
|
|
.send({ alias: `noversion-${uniq()}`, code: "return 0;" })
|
|
.expect(400);
|
|
});
|
|
|
|
it("restores exact versions by id + version", async () => {
|
|
const id = crypto.randomUUID();
|
|
const alias = `simp-${uniq()}`;
|
|
const base = { kind: "snippet" as const, id, alias, title: "t" };
|
|
|
|
let sn = await snippetService.importSnippet({
|
|
...base,
|
|
version: 2,
|
|
code: "return 2;",
|
|
});
|
|
expect(sn.version).toBe(2);
|
|
|
|
sn = await snippetService.importSnippet({
|
|
...base,
|
|
version: 2,
|
|
code: "changed",
|
|
});
|
|
expect(sn.code).toBe("return 2;");
|
|
|
|
sn = await snippetService.importSnippet({
|
|
...base,
|
|
version: 1,
|
|
code: "return 1;",
|
|
});
|
|
expect(sn.version).toBe(2);
|
|
|
|
sn = await snippetService.importSnippet({
|
|
...base,
|
|
version: 4,
|
|
code: "return 4;",
|
|
});
|
|
expect(sn.version).toBe(4);
|
|
expect(sn.code).toBe("return 4;");
|
|
expect(
|
|
(await snippetService.listVersions(id)).map((v) => v.version),
|
|
).toEqual([4, 2, 1]);
|
|
});
|
|
});
|
|
});
|