Files
factorio-clusterio/test/lib/data/InstanceDetails.test.js
T
bbassieandClaude Opus 5.5 b5c372f109 Rename bases to templates and add per property inheritance
Review changes for #1142:
- base becomes template everywhere (is_template, template_id,
  --template, --is-template, --templates, InstanceDetails.isTemplate).
- Object fields inherit per property. inherited_fields lists the
  overridden properties for object fields and the template object is
  merged with them.
- Linking a template also inherits fields that hold the default value.
  The server name stays per instance.
- Ports are never inherited.
- InstanceDetails carries templateId.

Web UI: inherited values are locked until unlocked, overridden values
can be inherited again, templates are listed separately, the create
modal can create templates and instances from templates, and the
instance page has "Create template" which can link the source.

Part of #200

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 11:54:37 +00:00

155 lines
4.7 KiB
JavaScript

import assert from "node:assert/strict";
import { InstanceDetails } from "@clusterio/lib";
import { testMatrix, testRoundTripJsonSerialisable } from "../../common.js";
describe("lib/data/InstanceDetails", function () {
it("should be round trip serialisable", function () {
testRoundTripJsonSerialisable(InstanceDetails, testMatrix(
["TestInstance"], // name (required)
[1], // id (required)
[undefined, 5], // assignedHost
[undefined, 34197], // gamePort
["running", "stopped"], // status (required)
[undefined, "1.0.0"], // factorioVersion
[undefined, 1234], // startedAtMs
[undefined, 9999], // updatedAtMs
[undefined, true], // excludeFromStartAll
[undefined, true], // isTemplate
[undefined, 7], // templateId
));
});
describe("constructor", function () {
it("should set defaults correctly", function () {
const instance = new InstanceDetails("TestInstance", 1, undefined, undefined, "running");
assert.equal(instance.name, "TestInstance");
assert.equal(instance.id, 1);
assert.equal(instance.assignedHost, undefined);
assert.equal(instance.gamePort, undefined);
assert.equal(instance.status, "running");
assert.equal(instance.factorioVersion, undefined);
assert.equal(instance.startedAtMs, 0);
assert.equal(instance.updatedAtMs, 0);
assert.equal(instance.excludeFromStartAll, false);
});
it("should accept all constructor parameters", function () {
const instance = new InstanceDetails(
"FullInstance", 42, 10, 34197,
"starting", "1.0.0", 1000, 2000, true
);
assert.equal(instance.name, "FullInstance");
assert.equal(instance.id, 42);
assert.equal(instance.assignedHost, 10);
assert.equal(instance.gamePort, 34197);
assert.equal(instance.status, "starting");
assert.equal(instance.factorioVersion, "1.0.0");
assert.equal(instance.startedAtMs, 1000);
assert.equal(instance.updatedAtMs, 2000);
assert.equal(instance.excludeFromStartAll, true);
});
});
describe(".toJSON()", function () {
it("should only include required fields when optional fields are not given", function () {
const instance = new InstanceDetails("TestInstance", 1, undefined, undefined, "running");
assert.deepEqual(instance.toJSON(), {
name: "TestInstance",
id: 1,
status: "running",
});
});
it("should include all optional fields when set", function () {
const instance = new InstanceDetails(
"FullInstance", 99, 5, 34197,
"running", "1.0.0", 500, 1000, true
);
assert.deepEqual(instance.toJSON(), {
name: "FullInstance",
id: 99,
status: "running",
assignedHost: 5,
gamePort: 34197,
factorioVersion: "1.0.0",
startedAtMs: 500,
updatedAtMs: 1000,
excludeFromStartAll: true,
});
});
it("should omit optional fields when equal to defaults", function () {
const instance = new InstanceDetails(
"TestInstance", 1, undefined, undefined,
"stopped", undefined, 0, 0, false
);
assert.deepEqual(instance.toJSON(), {
name: "TestInstance",
id: 1,
status: "stopped",
});
});
});
describe("static fromJSON()", function () {
it("should construct minimal object and apply constructor defaults", function () {
const instance = InstanceDetails.fromJSON({
name: "TestInstance",
id: 1,
status: "running",
});
assert.equal(instance.name, "TestInstance");
assert.equal(instance.id, 1);
assert.equal(instance.assignedHost, undefined);
assert.equal(instance.gamePort, undefined);
assert.equal(instance.factorioVersion, undefined);
assert.equal(instance.startedAtMs, 0);
assert.equal(instance.updatedAtMs, 0);
assert.equal(instance.excludeFromStartAll, false);
});
it("should preserve optional fields exactly when provided", function () {
const instance = InstanceDetails.fromJSON({
name: "RemoteInstance",
id: 7,
status: "starting",
assignedHost: 2,
gamePort: 34197,
factorioVersion: "1.0.0",
startedAtMs: 100,
updatedAtMs: 200,
excludeFromStartAll: true,
});
assert.equal(instance.assignedHost, 2);
assert.equal(instance.gamePort, 34197);
assert.equal(instance.factorioVersion, "1.0.0");
assert.equal(instance.startedAtMs, 100);
assert.equal(instance.updatedAtMs, 200);
assert.equal(instance.excludeFromStartAll, true);
});
});
describe("get .isDeleted", function () {
it("should return true when status is 'deleted'", function () {
const instance = new InstanceDetails("TestInstance", 1, undefined, undefined, "deleted");
assert.equal(instance.isDeleted, true);
});
it("should return false when status is not 'deleted'", function () {
const instance = new InstanceDetails("TestInstance", 1, undefined, undefined, "running");
assert.equal(instance.isDeleted, false);
});
});
});