Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108import { describe, expect, it } from "vitest";import type { Datetime, Nsid, ResourceUri } from "@atcute/lexicons/syntax";import { foldLabelOps, type LabelDefinition, type LabelView, type OpView } from "$lib/api/labels";
const owner = "did:plc:owner";const collaborator = "did:plc:collab";const stranger = "did:plc:stranger";const subject = `at://${owner}/sh.tangled.repo.issue/3aaaaaaaaaa22`;
const definition = (rkey: string, overrides: Partial<LabelDefinition> = {}): LabelView => ({ uri: `at://${owner}/sh.tangled.label.definition/${rkey}`, value: { $type: "sh.tangled.label.definition", name: rkey, color: "#000000", scope: ["sh.tangled.repo.issue" as Nsid], valueType: { type: "null", format: "any" }, createdAt: "2026-01-01T00:00:00Z" as Datetime, ...overrides }});
const bug = definition("bug");const docs = definition("docs");const reviewer = definition("reviewer", { multiple: true, valueType: { type: "string", format: "did" }});const pullOnly = definition("pull-only", { scope: ["sh.tangled.repo.pull" as Nsid] });const definitions = [bug, docs, reviewer, pullOnly];
let rkey = 0;const op = ( did: string, performedAt: string, change: { add?: [LabelView, string?][]; delete?: [LabelView, string?][] }): OpView => { const operands = (entries: [LabelView, string?][] = []) => entries.map(([label, value = "null"]) => ({ key: label.uri as ResourceUri, value })); return { uri: `at://${did}/sh.tangled.label.op/3op${String(rkey++).padStart(10, "a")}`, value: { $type: "sh.tangled.label.op", subject: subject as ResourceUri, performedAt: performedAt as Datetime, add: operands(change.add), delete: operands(change.delete) } };};
const authors = new Set([owner, collaborator]);
describe("foldLabelOps", () => { it("applies adds and deletes in performedAt order, not list order", () => { const ops = [ op(owner, "2026-01-03T00:00:00Z", { delete: [[bug]] }), op(owner, "2026-01-01T00:00:00Z", { add: [[bug], [docs]] }) ];
expect(foldLabelOps(subject, definitions, ops, authors)).toEqual({ [docs.uri]: ["null"] }); });
it("ignores ops from authors without push access", () => { const ops = [op(stranger, "2026-01-01T00:00:00Z", { add: [[bug]] })];
expect(foldLabelOps(subject, definitions, ops, authors)).toEqual({}); });
it("keeps every value of a multiple label and replaces a single one", () => { const single = definition("priority", { valueType: { type: "string", format: "any" } }); const ops = [ op(owner, "2026-01-01T00:00:00Z", { add: [ [reviewer, owner], [single, "low"] ] }), op(collaborator, "2026-01-02T00:00:00Z", { add: [ [reviewer, collaborator], [single, "high"] ] }) ];
expect(foldLabelOps(subject, [...definitions, single], ops, authors)).toEqual({ [reviewer.uri]: [owner, collaborator], [single.uri]: ["high"] }); });
it("skips labels out of scope or without a definition", () => { const gone = definition("gone"); const ops = [op(owner, "2026-01-01T00:00:00Z", { add: [[pullOnly], [gone]] })];
expect(foldLabelOps(subject, definitions, ops, authors)).toEqual({}); });
it("drops a whole record when one operand has an invalid value", () => { const ops = [op(owner, "2026-01-01T00:00:00Z", { add: [[bug], [reviewer, "alice"]] })];
expect(foldLabelOps(subject, definitions, ops, authors)).toEqual({}); });});