forked from longtao.wu/droboticflow
109 lines
3.3 KiB
JavaScript
109 lines
3.3 KiB
JavaScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { projectDictionaryNameKey, reuseOrCreateDictionaryLabel } from "../../src/lib/project-dictionary.js";
|
|
const existing = { id: "existing", kind: "slice", name: "表面清洁", status: "active", version: 3 };
|
|
test("reuse an organization term without changing its text or revision", async () => {
|
|
let creates = 0;
|
|
const source = Object.freeze({ ...existing });
|
|
const result = await reuseOrCreateDictionaryLabel({
|
|
kind: "slice",
|
|
name: " 表面清洁 ",
|
|
list: async () => [source],
|
|
create: async () => {
|
|
creates++;
|
|
},
|
|
});
|
|
assert.equal(result, source);
|
|
assert.equal(creates, 0);
|
|
assert.equal(result.version, 3);
|
|
});
|
|
test("name normalization and dictionary kinds are independent", async () => {
|
|
assert.equal(projectDictionaryNameKey("slice", " A B "), "slice:a b");
|
|
let created = false;
|
|
await reuseOrCreateDictionaryLabel({
|
|
kind: "action",
|
|
name: "表面清洁",
|
|
list: async () => [existing],
|
|
create: async () => {
|
|
created = true;
|
|
return { id: "action" };
|
|
},
|
|
});
|
|
assert.ok(created);
|
|
});
|
|
test("historical inactive terms allow a new record without reactivating old references", async () => {
|
|
const old = Object.freeze({ ...existing, status: "inactive" });
|
|
const result = await reuseOrCreateDictionaryLabel({
|
|
kind: "slice",
|
|
name: existing.name,
|
|
list: async () => [old],
|
|
create: async () => ({ ...existing, id: "new" }),
|
|
});
|
|
assert.equal(result.id, "new");
|
|
assert.equal(old.status, "inactive");
|
|
assert.equal(old.version, 3);
|
|
});
|
|
|
|
test("concurrent creator is reread after unique constraint rejection", async () => {
|
|
let lists = 0;
|
|
const result = await reuseOrCreateDictionaryLabel({
|
|
kind: "slice",
|
|
name: existing.name,
|
|
list: async () => (++lists === 1 ? [] : [existing]),
|
|
create: async () => {
|
|
throw Object.assign(new Error("conflict"), { code: "dictionary_name_conflict" });
|
|
},
|
|
});
|
|
assert.equal(result.id, existing.id);
|
|
assert.equal(lists, 2);
|
|
});
|
|
test("unrelated failures are not hidden by reuse", async () => {
|
|
const failure = Object.assign(new Error("network unavailable"), { code: "network" });
|
|
await assert.rejects(
|
|
() =>
|
|
reuseOrCreateDictionaryLabel({
|
|
kind: "slice",
|
|
name: "new",
|
|
list: async () => [],
|
|
create: async () => {
|
|
throw failure;
|
|
},
|
|
}),
|
|
(e) => e === failure,
|
|
);
|
|
});
|
|
test("editing an unchanged legacy duplicate preserves its own ID", async () => {
|
|
const preferred = { ...existing, id: "selected" };
|
|
const result = await reuseOrCreateDictionaryLabel({
|
|
kind: "slice",
|
|
name: existing.name,
|
|
preferredId: "selected",
|
|
list: async () => [existing, preferred],
|
|
create: async () => {
|
|
throw Error("must reuse");
|
|
},
|
|
});
|
|
assert.equal(result.id, "selected");
|
|
});
|
|
test("switching project while lookup is pending prevents a stale create", async () => {
|
|
let current = true,
|
|
created = false;
|
|
await assert.rejects(
|
|
() =>
|
|
reuseOrCreateDictionaryLabel({
|
|
kind: "slice",
|
|
name: "new",
|
|
isCurrent: () => current,
|
|
list: async () => {
|
|
current = false;
|
|
return [];
|
|
},
|
|
create: async () => {
|
|
created = true;
|
|
},
|
|
}),
|
|
(e) => e.name === "AbortError",
|
|
);
|
|
assert.equal(created, false);
|
|
});
|