X-Git-Url: https://git.ladys.computer/Etiquette/blobdiff_plain/6365487166d73a75e1c66b7c8d5683ac46741ed2..1042458c16601ea32b871819f79a6ba78b1e90ae:/model.test.js?ds=sidebyside diff --git a/model.test.js b/model.test.js index 15a2f54..254dd4c 100644 --- a/model.test.js +++ b/model.test.js @@ -105,13 +105,13 @@ describe("TagSystem", () => { }); it("[[Construct]] defaults the preferred label to the empty string", () => { - assertStrictEquals(new Tag().prefLabel, ""); + assertEquals({ ...new Tag().prefLabel }, { "@value": "" }); }); it("[[Construct]] correctly sets the preferred label to a simple string", () => { - assertStrictEquals( - new Tag("RelationshipTag", "Shadow, Me").prefLabel, - "Shadow, Me", + assertEquals( + { ...new Tag("RelationshipTag", "Shadow, Me").prefLabel }, + { "@value": "Shadow, Me" }, ); }); @@ -164,6 +164,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(); @@ -177,17 +183,19 @@ 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, ); }); it("[[Call]] throws if passed an invalid I·R·I", () => { - assertThrows(() => {Tag.fromIRI(`bad iri`)}); + assertThrows(() => { + Tag.fromIRI(`bad iri`); + }); }); }); @@ -204,8 +212,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", () => { @@ -231,14 +239,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, ); }); @@ -254,12 +262,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* () { @@ -278,6 +280,12 @@ describe("TagSystem", () => { }); }); + describe(".system", () => { + it("[[Call]] returns the `TagSystem`", () => { + assertEquals(Tag.system, system); + }); + }); + // `.[Storage.toInstance]` is tested by `.fromIdentifier`. describe("::addAltLabel", () => { @@ -295,13 +303,10 @@ describe("TagSystem", () => { { "@value": "three", "@language": "en" }, ); assertEquals( - Array.from( - tag.altLabels(), - ($) => typeof $ == "string" ? $ : { ...$ }, - ), + Array.from(tag.altLabels(), ($) => ({ ...$ })), [ - "one", - "two", + { "@value": "one" }, + { "@value": "two" }, { "@value": "three", "@language": "en" }, ], ); @@ -371,13 +376,10 @@ describe("TagSystem", () => { { "@value": "three", "@language": "en" }, ); assertEquals( - Array.from( - tag.hiddenLabels(), - ($) => typeof $ == "string" ? $ : { ...$ }, - ), + Array.from(tag.hiddenLabels(), ($) => ({ ...$ })), [ - "one", - "two", + { "@value": "one" }, + { "@value": "two" }, { "@value": "three", "@language": "en" }, ], ); @@ -415,8 +417,10 @@ describe("TagSystem", () => { }); it("[[Call]] throws when this is not a tag which can be placed in canon", () => { + const canon = new Tag("CanonTag"); + canon.persist(); assertThrows(() => { - new Tag().addInCanonTag(); + new Tag().addInCanonTag(canon); }); }); @@ -473,8 +477,10 @@ describe("TagSystem", () => { }); it("[[Call]] throws when this is not a conceptual tag", () => { + const involved = new Tag(); + involved.persist(); assertThrows(() => { - new Tag().addInvolvesTag(); + new Tag().addInvolvesTag(involved); }); }); @@ -553,7 +559,10 @@ describe("TagSystem", () => { const tag = new Tag(); tag.addAltLabel("etaoin"); tag.deleteAltLabel(); - assertEquals([...tag.altLabels()], ["etaoin"]); + assertEquals( + Array.from(tag.altLabels(), ($) => ({ ...$ })), + [{ "@value": "etaoin" }], + ); }); it("[[Call]] deletes only the provided hidden labels", () => { @@ -570,7 +579,10 @@ describe("TagSystem", () => { { "@value": "three", "@language": "en" }, { "@value": "four", "@language": "en" }, ); - assertEquals([...tag.altLabels()], ["four"]); + assertEquals( + Array.from(tag.altLabels(), ($) => ({ ...$ })), + [{ "@value": "four" }], + ); }); it("[[Call]] returns this", () => { @@ -621,7 +633,10 @@ describe("TagSystem", () => { const tag = new Tag(); tag.addHiddenLabel("etaoin"); tag.deleteHiddenLabel(); - assertEquals([...tag.hiddenLabels()], ["etaoin"]); + assertEquals( + Array.from(tag.hiddenLabels(), ($) => ({ ...$ })), + [{ "@value": "etaoin" }], + ); }); it("[[Call]] deletes only the provided alternative labels", () => { @@ -638,7 +653,10 @@ describe("TagSystem", () => { { "@value": "three", "@language": "en" }, { "@value": "four", "@language": "en" }, ); - assertEquals([...tag.hiddenLabels()], ["four"]); + assertEquals( + Array.from(tag.hiddenLabels(), ($) => ({ ...$ })), + [{ "@value": "four" }], + ); }); it("[[Call]] returns this", () => { @@ -916,9 +934,9 @@ describe("TagSystem", () => { it("[[Set]] sets the preferred label", () => { const tag = new Tag(); tag.prefLabel = "one"; - assertStrictEquals(tag.prefLabel, "one"); + assertEquals({ ...tag.prefLabel }, { "@value": "one" }); tag.prefLabel = { "@value": "two" }; - assertStrictEquals(tag.prefLabel, "two"); + assertEquals({ ...tag.prefLabel }, { "@value": "two" }); tag.prefLabel = { "@value": "three", "@language": "en" }; assertEquals( { ...tag.prefLabel }, @@ -927,6 +945,12 @@ describe("TagSystem", () => { }); }); + describe("::system", () => { + it("[[Get]] returns the tag system", () => { + assertStrictEquals(new Tag().system, system); + }); + }); + // `::tagURI` is tested by a `.fromTagURI`. describe("::taggingEntity", () => { @@ -949,6 +973,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");