X-Git-Url: https://git.ladys.computer/Etiquette/blobdiff_plain/e20983aca1b36dd368d52e02ee018c2eaf0ca55f..f8903c5b3bd12d02af174e5043d906d63da0b0d1:/model.test.js diff --git a/model.test.js b/model.test.js index 74f73da..b3a1b1b 100644 --- a/model.test.js +++ b/model.test.js @@ -1,11 +1,14 @@ -// 📧🏷️ Étiquette ∷ model.test.js -// ==================================================================== -// -// Copyright © 2023 Lady [@ Lady’s Computer]. -// -// This Source Code Form is subject to the terms of the Mozilla Public -// License, v. 2.0. If a copy of the MPL was not distributed with this -// file, You can obtain one at . +// SPDX-FileCopyrightText: 2023, 2025 Lady +// SPDX-License-Identifier: MPL-2.0 +/** + * ⁌ 📧🏷️ Étiquette ∷ model.test.js + * + * Copyright © 2023, 2025 Lady [@ Ladys Computer]. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at . + */ import { assert, @@ -140,12 +143,6 @@ describe("TagSystem", () => { }); }); - describe(".For", () => { - it("[[Has]] is not present", () => { - assertFalse("For" in Tag); - }); - }); - describe(".all", () => { it("[[Call]] yields all the persisted tags", () => { const tags = new Set(function* () { @@ -170,6 +167,12 @@ describe("TagSystem", () => { }); }); + describe(".constructor", () => { + it("[[Get]] is `Function`", () => { + assertStrictEquals(Tag.constructor, Function); + }); + }); + describe(".fromIRI", () => { it("[[Call]] returns the persisted tag with the given I·R·I", () => { const tag = new Tag(); @@ -183,12 +186,12 @@ describe("TagSystem", () => { assertStrictEquals(retrieved.identifier, identifier); }); - it("[[Call]] returns null if no tag with the given I·R·I has been persisted", () => { + it("[[Call]] returns undefined if no tag with the given I·R·I has been persisted", () => { assertStrictEquals( Tag.fromIRI( `https://${system.authorityName}/tag:${system.taggingEntity}:000-0000`, ), - null, + undefined, ); }); @@ -212,8 +215,8 @@ describe("TagSystem", () => { assertStrictEquals(retrieved.identifier, identifier); }); - it("[[Call]] returns null if no tag with the given identifier has been persisted", () => { - assertStrictEquals(Tag.fromIdentifier("000-0000"), null); + it("[[Call]] returns undefined if no tag with the given identifier has been persisted", () => { + assertStrictEquals(Tag.fromIdentifier("000-0000"), undefined); }); it("[[Call]] throws if passed an invalid identifier", () => { @@ -239,14 +242,14 @@ describe("TagSystem", () => { assertStrictEquals(retrieved.identifier, identifier); }); - it("[[Call]] returns null if no tag with the given Tag U·R·I has been persisted", () => { + it("[[Call]] returns undefined if no tag with the given Tag U·R·I has been persisted", () => { assertStrictEquals( Tag.fromTagURI(`tag:${system.taggingEntity}:`), - null, + undefined, ); assertStrictEquals( Tag.fromTagURI(`tag:${system.taggingEntity}:000-0000`), - null, + undefined, ); }); @@ -262,12 +265,6 @@ describe("TagSystem", () => { }); }); - describe(".getSystem", () => { - it("[[Has]] is not present", () => { - assertFalse("getSystem" in Tag); - }); - }); - describe(".identifiers", () => { it("[[Call]] yields all the persisted identifiers", () => { const tags = new Set(function* () { @@ -286,6 +283,12 @@ describe("TagSystem", () => { }); }); + describe(".system", () => { + it("[[Call]] returns the `TagSystem`", () => { + assertEquals(Tag.system, system); + }); + }); + // `.[Storage.toInstance]` is tested by `.fromIdentifier`. describe("::addAltLabel", () => { @@ -945,6 +948,12 @@ describe("TagSystem", () => { }); }); + describe("::system", () => { + it("[[Get]] returns the tag system", () => { + assertStrictEquals(new Tag().system, system); + }); + }); + // `::tagURI` is tested by a `.fromTagURI`. describe("::taggingEntity", () => { @@ -967,6 +976,208 @@ describe("TagSystem", () => { // `::[Storage.toObject]` is tested by `::persist`. }); + describe("::apply", () => { + let system; + let Tag; + + beforeEach(() => { + system = new TagSystem("example", "1972-12-31"); + Tag = system.Tag; + }); + + it("[[Call]] throws if no activity is provided", () => { + assertThrows(() => { + system.apply(); + }); + }); + + it("[[Call]] throws with an invalid activity", () => { + assertThrows(() => { + system.apply({}); + }); + }); + + it("[[Call]] throws when specifying an invalid object", () => { + assertThrows(() => { + system.apply({ + object: "", + }); + }); + assertThrows(() => { + system.apply({ + object: `${system.iriSpace}000-0000`, + }); + }); + }); + + it("[[Call]] returns the tag being modified", () => { + const tag = new Tag(); + tag.persist(true); + const applied = system.apply({ object: tag.iri }); + assertStrictEquals( + Object.getPrototypeOf(applied), + system.Tag.prototype, + ); + assertStrictEquals(tag.identifier, applied.identifier); + }); + + it("[[Call]] applies the changes", () => { + const broaderTag = new Tag(); + const broaderActivity = broaderTag.persist(); + const otherBroaderTag = new Tag(); + const otherBroaderActivity = otherBroaderTag.persist(); + const tag = new Tag("EntityTag", "my pref label"); + tag.addHiddenLabel("label"); + tag.addBroaderTag(broaderTag); + const createActivity = tag.persist(); + tag.prefLabel = "new pref label"; + tag.addAltLabel("alternative label"); + tag.deleteHiddenLabel("label"); + tag.addBroaderTag(otherBroaderTag); + tag.deleteBroaderTag(broaderTag); + const updateActivity = tag.persist(); + const otherSystem = new TagSystem( + system.authorityName, + system.date, + ); + otherSystem.apply(broaderActivity); + otherSystem.apply(otherBroaderActivity); + const appliedCreate = otherSystem.apply(createActivity); + assertStrictEquals(appliedCreate.kind, "EntityTag"); + assertStrictEquals(appliedCreate.identifier, tag.identifier); + assertEquals( + { ...appliedCreate.prefLabel }, + { "@value": "my pref label" }, + ); + assertEquals( + [...appliedCreate.hiddenLabels()].map(($) => ({ ...$ })), + [{ "@value": "label" }], + ); + assertEquals( + [...appliedCreate.broaderTags()].map(($) => $.identifier), + [broaderTag.identifier], + ); + const appliedUpdate = otherSystem.apply(updateActivity); + assertEquals( + { ...appliedUpdate.prefLabel }, + { "@value": "new pref label" }, + ); + assertEquals( + [...appliedUpdate.altLabels()].map(($) => ({ ...$ })), + [{ "@value": "alternative label" }], + ); + assertEquals([...appliedUpdate.hiddenLabels()], []); + assertEquals( + [...appliedUpdate.broaderTags()].map(($) => $.identifier), + [otherBroaderTag.identifier], + ); + }); + + it("[[Call]] silently fails deleting preflabels", () => { + const tag = new system.Tag("Tag", "my pref label"); + tag.persist(true); + const applied = system.apply({ + object: tag.iri, + unstates: [{ + predicate: "prefLabel", + object: "my pref label", + }], + }); + assertEquals( + { ...applied.prefLabel }, + { "@value": "my pref label" }, + ); + }); + + it("[[Call]] silently fails deleting unrecognized statements", () => { + const tag = new Tag(); + tag.persist(true); + const otherTag = new Tag(); + otherTag.persist(true); + const applied = system.apply({ + object: tag.iri, + unstates: [{ + predicate: "bad_statement", + object: otherTag.iri, + }], + }); + assert(applied); + }); + + it("[[Call]] silently fails deleting immutable statements", () => { + const tag = new Tag(); + tag.persist(true); + const applied = system.apply({ + object: tag.iri, + unstates: [{ predicate: "a", object: "Tag" }], + }); + assertStrictEquals(applied.kind, "Tag"); + }); + + it("[[Call]] silently fails deleting inverse statements", () => { + const tag = new Tag(); + tag.persist(true); + const otherTag = new Tag(); + otherTag.addBroaderTag(tag); + otherTag.persist(true); + const applied = system.apply({ + object: tag.iri, + unstates: [{ predicate: "narrower", object: otherTag.iri }], + }); + assertStrictEquals( + [...applied.narrowerTags()][0].identifier, + otherTag.identifier, + ); + }); + + it("[[Call]] sets preflabels", () => { + const tag = new Tag("Tag", "my pref label"); + tag.persist(true); + const applied = system.apply({ + object: tag.iri, + states: [{ predicate: "prefLabel", object: "new pref label" }], + }); + assertEquals( + { ...applied.prefLabel }, + { "@value": "new pref label" }, + ); + }); + + it("[[Call]] silently fails setting unrecognized statements", () => { + const tag = new Tag(); + tag.persist(true); + const otherTag = new Tag(); + otherTag.persist(true); + const applied = system.apply({ + object: tag.iri, + states: [{ predicate: "bad_statement", object: otherTag.iri }], + }); + assert(applied); + }); + + it("[[Call]] silently fails setting immutable statements", () => { + const tag = new Tag(); + tag.persist(true); + const applied = system.apply({ + object: tag.iri, + states: [{ predicate: "a", object: "RelationshipTag" }], + }); + assertStrictEquals(applied.kind, "Tag"); + }); + + it("[[Call]] silently fails setting inverse statements", () => { + const tag = new Tag(); + tag.persist(true); + const otherTag = new Tag(); + otherTag.persist(true); + const applied = system.apply({ + object: tag.iri, + unstates: [{ predicate: "narrower", object: otherTag.iri }], + }); + assertEquals([...applied.narrowerTags()], []); + }); + }); + describe("::authorityName", () => { it("[[Get]] returns the authority name", () => { const system = new TagSystem("etaoin.example", "1972-12-31");