-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy paththing.test.ts
More file actions
1146 lines (995 loc) · 35.4 KB
/
thing.test.ts
File metadata and controls
1146 lines (995 loc) · 35.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright 2022 Inrupt Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import { describe, it, expect } from "@jest/globals";
import { DataFactory } from "n3";
import { dataset } from "@rdfjs/dataset";
import {
getThing,
getThingAll,
setThing,
removeThing,
createThing,
asUrl,
isThing,
thingAsMarkdown,
ThingExpectedError,
ValidThingUrlExpectedError,
ValidPropertyUrlExpectedError,
ValidValueUrlExpectedError,
} from "./thing";
import { internal_throwIfNotThing } from "./thing.internal";
import {
Thing,
ThingLocal,
ThingPersisted,
SolidDataset,
SolidClientError,
WithServerResourceInfo,
} from "../interfaces";
import { createSolidDataset } from "../resource/solidDataset";
import { mockThingFrom } from "./mock";
import {
addStringNoLocale,
addInteger,
addStringWithLocale,
addIri,
addBoolean,
addDatetime,
addDecimal,
} from "./add";
import { WithAcl } from "../acl/acl";
import { mockSolidDatasetFrom } from "../resource/mock";
import { internal_setAcl } from "../acl/acl.internal";
import { LocalNodeIri, localNodeSkolemPrefix } from "../rdf.internal";
describe("createThing", () => {
it("automatically generates a unique name for the Thing", () => {
const thing1: ThingLocal = createThing();
const thing2: ThingLocal = createThing();
expect(typeof thing1.url).toBe("string");
expect(thing1.url.length).toBeGreaterThan(localNodeSkolemPrefix.length);
expect(thing1.url).not.toBe(thing2.url);
});
it("uses the given name, if any", () => {
const thing: ThingLocal = createThing({ name: "some-name" });
expect(thing.url).toBe(localNodeSkolemPrefix + "some-name");
});
it("uses the given IRI, if any", () => {
const thing: ThingPersisted = createThing({
url: "https://some.pod/resource#thing",
});
expect(thing.url).toBe("https://some.pod/resource#thing");
});
it("throws an error if the given URL is invalid", () => {
expect(() => createThing({ url: "Invalid IRI" })).toThrow();
});
});
describe("isThing", () => {
it("returns true for a ThingLocal", () => {
expect(isThing(createThing())).toBe(true);
});
it("returns true for a ThingPersisted", () => {
expect(isThing(mockThingFrom("https://arbitrary.pod/resource#thing"))).toBe(
true
);
});
it("returns false for an atomic data type", () => {
expect(isThing("This is not a Thing")).toBe(false);
});
it("returns false for a regular JavaScript object", () => {
expect(isThing({ not: "a Thing" })).toBe(false);
});
it("returns false for a plain RDF/JS Dataset", () => {
expect(isThing(dataset())).toBe(false);
});
it("returns false for a SolidDataset", () => {
expect(isThing(createSolidDataset())).toBe(false);
});
});
describe("getThing", () => {
const mockThing1Iri = "https://some.pod/resource#subject1";
const mockThing2Iri = "https://some.pod/resource#subject2";
const otherGraphIri = "https://some.vocab/graph";
const mockThing1: ThingPersisted = {
type: "Subject",
url: mockThing1Iri,
predicates: {
["https://arbitrary.vocab/predicate"]: {
namedNodes: ["https://arbitrary.vocab/predicate"],
},
},
};
const mockThing2: ThingPersisted = {
type: "Subject",
url: mockThing2Iri,
predicates: {
["https://arbitrary.vocab/predicate"]: {
namedNodes: ["https://arbitrary.vocab/predicate"],
},
},
};
const mockLocalThingIri = (localNodeSkolemPrefix +
"localSubject") as LocalNodeIri;
const mockLocalThing: ThingLocal = {
type: "Subject",
url: mockLocalThingIri,
predicates: {
["https://arbitrary.vocab/predicate"]: {
namedNodes: ["https://arbitrary.vocab/predicate"],
},
},
};
function getMockDataset(
things = [mockThing1, mockThing2],
otherGraphThings = [mockThing1]
): SolidDataset {
const solidDataset: SolidDataset = {
type: "Dataset",
graphs: {
default: {},
[otherGraphIri]: {},
},
};
things.forEach((thing) => {
// The assertion allows writing to what we've declared to be a read-only property:
(solidDataset.graphs.default[thing.url] as any) = thing;
});
otherGraphThings.forEach((thing) => {
// The assertion allows writing to what we've declared to be a read-only property:
(solidDataset.graphs[otherGraphIri][thing.url] as any) = thing;
});
return solidDataset;
}
it("returns a Dataset with just Quads in there with the given Subject", () => {
const thing = getThing(getMockDataset(), mockThing1Iri);
expect(thing).toStrictEqual(mockThing1);
});
it("accepts a Named Node as the Subject identifier", () => {
const thing = getThing(
getMockDataset(),
DataFactory.namedNode(mockThing1Iri)
);
expect(thing).toStrictEqual(mockThing1);
});
it("accepts a LocalNode as the Subject identifier", () => {
const thing = getThing(
getMockDataset([mockLocalThing]),
DataFactory.namedNode(mockLocalThingIri)
);
expect(thing).toStrictEqual(mockLocalThing);
});
it("returns null if the given SolidDataset does not include Quads with the given Subject", () => {
const thing = getThing(
getMockDataset([]),
"https://arbitrary.vocab/subject"
);
expect(thing).toBeNull();
});
it("accepts a LocalNode as the Subject identifier even for Things with resolved IRIs", () => {
const mockDataset = getMockDataset([mockThing1]);
const mockDatasetWithResourceInfo: SolidDataset & WithServerResourceInfo = {
...mockDataset,
internal_resourceInfo: {
isRawData: false,
linkedResources: {},
sourceIri: mockThing1Iri.substring(
0,
mockThing1Iri.length - "subject1".length
),
},
};
const thing = getThing(
mockDatasetWithResourceInfo,
localNodeSkolemPrefix + "subject1"
);
expect(thing).toStrictEqual(mockThing1);
});
it("only returns Quads from the default graph if no scope was specified", () => {
expect(
getThing(getMockDataset([mockThing1], [mockThing2]), mockThing2Iri)
).toBeNull();
expect(
getThing(getMockDataset([mockThing1], [mockThing2]), mockThing1Iri)
).toStrictEqual(mockThing1);
});
it("is able to limit the Thing's scope to a single Named Graph", () => {
expect(
getThing(getMockDataset([mockThing1], [mockThing2]), mockThing2Iri, {
scope: otherGraphIri,
})
).toStrictEqual(mockThing2);
});
it("is able to specify the scope using a Named Node", () => {
expect(
getThing(getMockDataset([mockThing1], [mockThing2]), mockThing2Iri, {
scope: DataFactory.namedNode(otherGraphIri),
})
).toStrictEqual(mockThing2);
});
it("returns null if the given scope does not include the requested Thing", () => {
expect(
getThing(getMockDataset([mockThing1], [mockThing2]), mockThing1Iri, {
scope: otherGraphIri,
})
).toBeNull();
});
it("returns null if the given scope does not include any Things", () => {
expect(
getThing(getMockDataset([mockThing1], [mockThing2]), mockThing2Iri, {
scope: "https://arbitrary.vocab/other-graph",
})
).toBeNull();
});
it("throws an error when given an invalid URL", () => {
expect(() => getThing(getMockDataset(), "not-a-url")).toThrow(
"Expected a valid URL to identify a Thing, but received: [not-a-url]."
);
});
it("throws an instance of ThingUrlExpectedError on invalid URLs", () => {
let thrownError;
try {
getThing(getMockDataset(), "not-a-url");
} catch (e) {
thrownError = e;
}
expect(thrownError).toBeInstanceOf(ValidThingUrlExpectedError);
});
});
describe("getThingAll", () => {
const mockThing1Iri = "https://some.vocab/subject1";
const mockThing2Iri = "https://some.vocab/subject2";
const otherGraphIri = "https://some.vocab/graph";
const mockThing1: ThingPersisted = {
type: "Subject",
url: mockThing1Iri,
predicates: {
["https://arbitrary.vocab/predicate"]: {
namedNodes: ["https://arbitrary.vocab/predicate"],
},
},
};
const mockThing2: ThingPersisted = {
type: "Subject",
url: mockThing2Iri,
predicates: {
["https://arbitrary.vocab/predicate"]: {
namedNodes: ["https://arbitrary.vocab/predicate"],
},
},
};
function getMockDataset(
things = [mockThing1, mockThing2],
otherGraphThings = [mockThing1]
): SolidDataset {
const solidDataset: SolidDataset = {
type: "Dataset",
graphs: {
default: {},
[otherGraphIri]: {},
},
};
things.forEach((thing) => {
// The assertion allows writing to what we've declared to be a read-only property:
(solidDataset.graphs.default[thing.url] as any) = thing;
});
otherGraphThings.forEach((thing) => {
// The assertion allows writing to what we've declared to be a read-only property:
(solidDataset.graphs[otherGraphIri][thing.url] as any) = thing;
});
return solidDataset;
}
it("returns the individual Things", () => {
const things = getThingAll(getMockDataset([mockThing1, mockThing2]));
expect(things).toStrictEqual([mockThing1, mockThing2]);
});
it("does not return Things with a Blank Node as the Subject by default", () => {
const mockDataset = getMockDataset([mockThing1]);
const blankNode = {
predicates: {
["https://arbitrary.predicate"]: {
namedNodes: ["https://arbitrary.value"],
},
},
type: "Subject",
url: "_:blankNodeId",
};
(mockDataset.graphs.default["_:blankNodeId"] as any) = blankNode;
const things = getThingAll(mockDataset);
expect(things).toHaveLength(1);
expect(things).toStrictEqual([mockThing1]);
});
it("returns Things with a Blank Node as the Subject if specified", () => {
const mockDataset = getMockDataset([mockThing1]);
const blankNode = {
predicates: {
["https://arbitrary.predicate"]: {
namedNodes: ["https://arbitrary.value"],
},
},
type: "Subject",
url: "_:blankNodeId",
};
(mockDataset.graphs.default["_:blankNodeId"] as any) = blankNode;
const things = getThingAll(mockDataset, { acceptBlankNodes: true });
expect(things).toHaveLength(2);
expect(things).toStrictEqual([mockThing1, blankNode]);
});
it("returns Quads from the default Graphs if no scope was specified", () => {
const things = getThingAll(getMockDataset([mockThing1], [mockThing2]));
expect(things).toStrictEqual([mockThing1]);
});
it("ignores Quads in the default graph when specifying an explicit scope", () => {
const things = getThingAll(getMockDataset([mockThing1], [mockThing2]), {
scope: otherGraphIri,
});
expect(things).toStrictEqual([mockThing2]);
});
it("is able to specify the scope using a Named Node", () => {
const things = getThingAll(getMockDataset([mockThing1], [mockThing2]), {
scope: DataFactory.namedNode(otherGraphIri),
});
expect(things).toStrictEqual([mockThing2]);
});
it("returns an empty array if the given scope does not include any Things", () => {
const things = getThingAll(getMockDataset([mockThing1], [mockThing2]), {
scope: "https://arbitrary.vocab/other-graph",
});
expect(things).toStrictEqual([]);
});
});
describe("setThing", () => {
const mockThing1Iri = "https://some.vocab/subject1";
const mockThing2Iri = "https://some.vocab/subject2";
const mockThing3Iri = "https://some.vocab/subject3";
const mockThing1: ThingPersisted = {
type: "Subject",
url: mockThing1Iri,
predicates: {
["https://arbitrary.vocab/predicate"]: {
namedNodes: ["https://arbitrary.vocab/predicate"],
},
},
};
const mockThing2: ThingPersisted = {
type: "Subject",
url: mockThing2Iri,
predicates: {
["https://arbitrary.vocab/predicate"]: {
namedNodes: ["https://arbitrary.vocab/predicate"],
},
},
};
const mockThing3: ThingPersisted = {
type: "Subject",
url: mockThing3Iri,
predicates: {
["https://arbitrary.vocab/predicate"]: {
namedNodes: ["https://arbitrary.vocab/object"],
blankNodes: [
{
["https://arbitrary.vocab/blanknode/predicate"]: {
namedNodes: ["https://arbitrary.vocab/blanknode/object"],
},
},
],
},
},
};
function getMockDataset(things = [mockThing1, mockThing2]): SolidDataset {
const solidDataset: SolidDataset = {
type: "Dataset",
graphs: {
default: {},
},
};
things.forEach((thing) => {
// The assertion allows writing to what we've declared to be a read-only property:
(solidDataset.graphs.default[thing.url] as any) = thing;
});
return solidDataset;
}
it("returns a Dataset with the new Thing added to it", () => {
const datasetWithExistingThings = getMockDataset([mockThing1]);
const updatedDataset = setThing(datasetWithExistingThings, mockThing2);
expect(updatedDataset.graphs).toStrictEqual(
getMockDataset([mockThing1, mockThing2]).graphs
);
});
it("keeps track of additions and deletions in the attached change log", () => {
const datasetWithExistingThings = getMockDataset([mockThing1]);
const updatedDataset = setThing(datasetWithExistingThings, mockThing2);
expect(updatedDataset.internal_changeLog.additions).toHaveLength(1);
expect(updatedDataset.internal_changeLog.deletions).toHaveLength(0);
expect(updatedDataset.internal_changeLog.additions[0].subject.value).toBe(
mockThing2Iri
);
});
it("reconciles deletions and additions in the change log", () => {
const datasetWithExistingThings = getMockDataset([mockThing1, mockThing2]);
const datasetWithThing2Removed = removeThing(
datasetWithExistingThings,
mockThing2
);
expect(datasetWithThing2Removed.internal_changeLog.additions).toHaveLength(
0
);
expect(datasetWithThing2Removed.internal_changeLog.deletions).toHaveLength(
1
);
const datasetWithThing2AddedAgain = setThing(
datasetWithThing2Removed,
mockThing2
);
expect(
datasetWithThing2AddedAgain.internal_changeLog.additions
).toHaveLength(0);
expect(
datasetWithThing2AddedAgain.internal_changeLog.deletions
).toHaveLength(0);
});
it("preserves existing change logs", () => {
const datasetWithoutThings = getMockDataset([]);
const datasetWithThing1Added = setThing(datasetWithoutThings, mockThing1);
expect(datasetWithThing1Added.internal_changeLog.additions).toHaveLength(1);
expect(datasetWithThing1Added.internal_changeLog.deletions).toHaveLength(0);
const datasetWithThing2AddedToo = setThing(
datasetWithThing1Added,
mockThing2
);
expect(datasetWithThing2AddedToo.internal_changeLog.additions).toHaveLength(
2
);
expect(datasetWithThing2AddedToo.internal_changeLog.deletions).toHaveLength(
0
);
});
it("does not modify the original SolidDataset", () => {
const datasetWithExistingThings = getMockDataset([mockThing1]);
const updatedDataset = setThing(datasetWithExistingThings, mockThing2);
expect(updatedDataset).not.toStrictEqual(datasetWithExistingThings);
});
it("can reconcile new LocalNodes with existing NamedNodes if the SolidDataset has a resource IRI attached", () => {
let solidDataset = mockSolidDatasetFrom("https://some.pod/resource");
const originalThing: ThingPersisted = {
type: "Subject",
url: "https://some.pod/resource#subjectName",
predicates: {
"https://arbitrary.predicate": {
namedNodes: ["https://arbitrary.value"],
},
},
};
solidDataset = setThing(solidDataset, originalThing);
const updatedThing: ThingLocal = {
type: "Subject",
url: (localNodeSkolemPrefix + "subjectName") as LocalNodeIri,
predicates: {
"https://some.predicate": {
namedNodes: ["https://some.value"],
},
},
};
const updatedDataset = setThing(solidDataset, updatedThing);
expect(
getThing(updatedDataset, "https://some.pod/resource#subjectName")
?.predicates
).toStrictEqual(updatedThing.predicates);
});
it("only updates LocalNodes if the SolidDataset has no known IRI", () => {
let solidDataset = createSolidDataset();
const originalThing: ThingPersisted = {
type: "Subject",
url: "https://some.pod/resource#subjectName",
predicates: {
"https://arbitrary.predicate": {
namedNodes: ["https://arbitrary.value"],
},
},
};
solidDataset = setThing(solidDataset, originalThing);
const updatedThing: ThingLocal = {
type: "Subject",
url: (localNodeSkolemPrefix + "subjectName") as LocalNodeIri,
predicates: {
"https://some.predicate": {
namedNodes: ["https://some.value"],
},
},
};
const updatedDataset = setThing(solidDataset, updatedThing);
expect(
getThing(updatedDataset, "https://some.pod/resource#subjectName")
).toStrictEqual(originalThing);
});
it("will create blank nodes including in a Thing", () => {
const dataset = getMockDataset([]);
const updatedDataset = setThing(dataset, mockThing3);
expect(updatedDataset.internal_changeLog.additions).not.toHaveLength(1);
});
});
describe("removeThing", () => {
const mockThing1Iri = "https://some.vocab/subject1";
const mockThing2Iri = "https://some.vocab/subject2";
const mockThing1: ThingPersisted = {
type: "Subject",
url: mockThing1Iri,
predicates: {
["https://arbitrary.vocab/predicate"]: {
namedNodes: ["https://arbitrary.vocab/predicate"],
},
},
};
const mockThing2: ThingPersisted = {
type: "Subject",
url: mockThing2Iri,
predicates: {
["https://arbitrary.vocab/predicate"]: {
namedNodes: ["https://arbitrary.vocab/predicate"],
},
},
};
const mockLocalThingIri = (localNodeSkolemPrefix +
"localSubject") as LocalNodeIri;
const mockLocalThing: ThingLocal = {
type: "Subject",
url: mockLocalThingIri,
predicates: {
["https://arbitrary.vocab/predicate"]: {
namedNodes: ["https://arbitrary.vocab/predicate"],
},
},
};
function getMockDataset(things = [mockThing1, mockThing2]): SolidDataset {
const solidDataset: SolidDataset = {
type: "Dataset",
graphs: {
default: {},
},
};
things.forEach((thing) => {
// The assertion allows writing to what we've declared to be a read-only property:
(solidDataset.graphs.default[thing.url] as any) = thing;
});
return solidDataset;
}
it("returns a Dataset that excludes Quads with the Thing's Subject", () => {
const datasetWithMultipleThings = getMockDataset([mockThing1, mockThing2]);
const updatedDataset = removeThing(datasetWithMultipleThings, mockThing2);
expect(updatedDataset.graphs).toStrictEqual(
getMockDataset([mockThing1]).graphs
);
});
it("keeps track of deletions in the attached change log", () => {
const datasetWithExistingThings = getMockDataset([mockThing1, mockThing2]);
const updatedDataset = removeThing(datasetWithExistingThings, mockThing2);
expect(updatedDataset.internal_changeLog.additions).toHaveLength(0);
expect(updatedDataset.internal_changeLog.deletions).toHaveLength(1);
expect(updatedDataset.internal_changeLog.deletions[0].subject.value).toBe(
mockThing2Iri
);
});
it("reconciles deletions in the change log with additions", () => {
const datasetWithExistingThings = getMockDataset([mockThing1]);
const datasetWithThing2Added = setThing(
datasetWithExistingThings,
mockThing2
);
expect(datasetWithThing2Added.internal_changeLog.additions).toHaveLength(1);
expect(datasetWithThing2Added.internal_changeLog.deletions).toHaveLength(0);
const datasetWithThing2RemovedAgain = removeThing(
datasetWithExistingThings,
mockThing2
);
expect(
datasetWithThing2RemovedAgain.internal_changeLog.additions
).toHaveLength(0);
expect(
datasetWithThing2RemovedAgain.internal_changeLog.deletions
).toHaveLength(0);
});
it("preserves existing change logs", () => {
const datasetWithoutThings = getMockDataset([mockThing2]);
const datasetWithThing1Added = setThing(datasetWithoutThings, mockThing1);
expect(datasetWithThing1Added.internal_changeLog.additions).toHaveLength(1);
expect(datasetWithThing1Added.internal_changeLog.deletions).toHaveLength(0);
const datasetWithThing2AddedToo = removeThing(
datasetWithThing1Added,
mockThing2
);
expect(datasetWithThing2AddedToo.internal_changeLog.additions).toHaveLength(
1
);
expect(datasetWithThing2AddedToo.internal_changeLog.deletions).toHaveLength(
1
);
});
it("preserves attached ACLs", () => {
const datasetWithFetchedAcls: SolidDataset & WithAcl = internal_setAcl(
mockSolidDatasetFrom("https://some.vocab/"),
{
resourceAcl: null,
fallbackAcl: null,
}
);
// The assertion is to tell the type system we can write to this:
(datasetWithFetchedAcls.graphs.default[mockThing1Iri] as Thing) =
mockThing1;
const updatedDataset = removeThing(datasetWithFetchedAcls, mockThing1);
expect(updatedDataset.internal_acl).toEqual({
resourceAcl: null,
fallbackAcl: null,
});
});
it("returns a Dataset that excludes Quads with a given Subject IRI", () => {
const datasetWithMultipleThings = getMockDataset([mockThing1, mockThing2]);
const updatedDataset = removeThing(
datasetWithMultipleThings,
mockThing2Iri
);
expect(updatedDataset.graphs).toStrictEqual(
getMockDataset([mockThing1]).graphs
);
});
it("does not modify the original SolidDataset", () => {
const datasetWithMultipleThings = getMockDataset([mockThing1, mockThing2]);
const updatedDataset = removeThing(
datasetWithMultipleThings,
mockThing2Iri
);
expect(datasetWithMultipleThings.graphs).not.toStrictEqual(
updatedDataset.graphs
);
});
it("returns a Dataset that excludes Quads with a given NamedNode as their Subject", () => {
const datasetWithMultipleThings = getMockDataset([mockThing1, mockThing2]);
const updatedDataset = removeThing(
datasetWithMultipleThings,
DataFactory.namedNode(mockThing2Iri)
);
expect(updatedDataset.graphs).toStrictEqual(
getMockDataset([mockThing1]).graphs
);
});
it("can recognise LocalNodes", () => {
const solidDataset = getMockDataset([mockLocalThing]);
const updatedDataset = removeThing(solidDataset, mockLocalThingIri);
expect(getThingAll(updatedDataset)).toHaveLength(0);
});
it("can reconcile given LocalNodes with existing NamedNodes if the SolidDataset has a resource IRI attached", () => {
let solidDataset = mockSolidDatasetFrom("https://some.pod/resource");
const thingWithFullIri: ThingPersisted = {
type: "Subject",
url: "https://some.pod/resource#subjectName",
predicates: {
"https://arbitrary.predicate": {
namedNodes: ["https://arbitrary.value"],
},
},
};
solidDataset = setThing(solidDataset, thingWithFullIri);
const updatedDataset = removeThing(
solidDataset,
localNodeSkolemPrefix + "subjectName"
);
expect(getThingAll(updatedDataset)).toHaveLength(0);
});
it("only removes LocalNodes if the SolidDataset has no known IRI", () => {
let solidDataset = createSolidDataset();
const resolvedThing: ThingPersisted = {
type: "Subject",
url: "https://some.pod/resource#subjectName",
predicates: {
"https://arbitrary.predicate": {
namedNodes: ["https://arbitrary.value"],
},
},
};
const localThing: ThingLocal = {
type: "Subject",
url: (localNodeSkolemPrefix + "subjectName") as LocalNodeIri,
predicates: {
"https://some.predicate": {
namedNodes: ["https://some.value"],
},
},
};
solidDataset = setThing(solidDataset, resolvedThing);
solidDataset = setThing(solidDataset, localThing);
const updatedDataset = removeThing(solidDataset, localThing);
expect(
getThing(updatedDataset, "https://some.pod/resource#subjectName")
).toStrictEqual(resolvedThing);
});
});
describe("asIri", () => {
it("returns the IRI of a persisted Thing", () => {
const persistedThing = mockThingFrom("https://some.pod/resource#thing");
expect(asUrl(persistedThing)).toBe("https://some.pod/resource#thing");
});
it("returns the IRI of a local Thing relative to a given base IRI", () => {
const localThing: ThingLocal = {
type: "Subject",
predicates: {},
url: (localNodeSkolemPrefix + "some-name") as LocalNodeIri,
};
expect(asUrl(localThing, "https://some.pod/resource")).toBe(
"https://some.pod/resource#some-name"
);
});
it("accepts a Thing of which it is not known whether it is persisted yet", () => {
const thing = mockThingFrom("https://some.pod/resource#thing");
expect(asUrl(thing as Thing, "https://arbitrary.url")).toBe(
"https://some.pod/resource#thing"
);
});
it("triggers a TypeScript error when passed a ThingLocal without a base IRI", () => {
const localThing: ThingLocal = createThing();
// @ts-expect-error
expect(() => asUrl(localThing)).toThrow();
});
// This currently fails because a plain `Thing` always has a `url` property that is a string,
// and is therefore indistinguishable from a `ThingPersisted`. Not sure what the solution is yet.
// Meanwhile TS users won't get a build-time error if they're passing a plain `Thing`,
// which is annoying but not a major issue.
// eslint-disable-next-line jest/no-disabled-tests
it.skip("triggers a TypeScript error when passed a Thing without a base IRI", () => {
const plainThing = createThing() as Thing;
// @ts-expect<disabled because it does not work yet>-error
expect(() => asUrl(plainThing)).toThrow();
});
it("does not trigger a TypeScript error when passed a ThingPersisted without a base IRI", () => {
// We're only checking for the absence TypeScript errors:
expect.assertions(0);
const resolvedThing: ThingPersisted = mockThingFrom(
"https://some.pod/resource#thing"
);
// This should not error:
asUrl(resolvedThing);
});
it("throws an error when a local Thing was given without a base IRI", () => {
const localThing: ThingLocal = {
type: "Subject",
predicates: {},
url: (localNodeSkolemPrefix + "some-name") as LocalNodeIri,
};
expect(() => asUrl(localThing, undefined as any)).toThrow(
"The URL of a Thing that has not been persisted cannot be determined without a base URL."
);
});
});
describe("thingAsMarkdown", () => {
it("returns a readable version of an empty, unsaved Thing", () => {
const emptyThing = createThing({ name: "empty-thing" });
expect(thingAsMarkdown(emptyThing)).toBe(
"## Thing (no URL yet — identifier: `#empty-thing`)\n\n<empty>\n"
);
});
it("returns a readable version of an empty Thing with a known URL", () => {
const emptyThing = mockThingFrom("https://some.pod/resource#thing");
expect(thingAsMarkdown(emptyThing)).toBe(
"## Thing: https://some.pod/resource#thing\n\n<empty>\n"
);
});
it("returns a readable version of a Thing with just one property", () => {
let thingWithValue = createThing({ name: "with-one-value" });
thingWithValue = addStringNoLocale(
thingWithValue,
"https://some.vocab/predicate",
"Some value"
);
expect(thingAsMarkdown(thingWithValue)).toBe(
"## Thing (no URL yet — identifier: `#with-one-value`)\n" +
"\n" +
"Property: https://some.vocab/predicate\n" +
'- "Some value" (string)\n'
);
});
it("returns a readable version of a Thing with multiple properties and values", () => {
const mockThingObject = createThing({ name: "local-node-object" });
let thingWithValues = createThing({ name: "with-values" });
thingWithValues = addStringWithLocale(
thingWithValues,
"https://some.vocab/predicate",
"Some value",
"en-gb"
);
thingWithValues = addStringNoLocale(
thingWithValues,
"https://some.vocab/predicate",
"Some other value"
);
thingWithValues = addBoolean(
thingWithValues,
"https://some.vocab/predicate",
true
);
thingWithValues = addDatetime(
thingWithValues,
"https://some.vocab/predicate",
new Date(Date.UTC(1990, 10, 12, 13, 37, 42, 0))
);
thingWithValues = addDecimal(
thingWithValues,
"https://some.vocab/predicate",
13.37
);
thingWithValues = addInteger(
thingWithValues,
"https://some.vocab/predicate",
42
);
thingWithValues = addIri(
thingWithValues,
"https://some.vocab/other-predicate",
"https://some.url"
);
thingWithValues = addIri(
thingWithValues,
"https://some.vocab/other-predicate",
mockThingObject
);
expect(thingAsMarkdown(thingWithValues)).toBe(
"## Thing (no URL yet — identifier: `#with-values`)\n" +
"\n" +
"Property: https://some.vocab/predicate\n" +
'- "Some value" (en-gb string)\n' +
'- "Some other value" (string)\n' +
"- true (boolean)\n" +
"- Mon, 12 Nov 1990 13:37:42 GMT (datetime)\n" +
"- 13.37 (decimal)\n" +
"- 42 (integer)\n" +
"\n" +
"Property: https://some.vocab/other-predicate\n" +
"- <https://some.url> (URL)\n" +
"- <#local-node-object> (URL)\n"
);
});
it("returns a readable version of a Thing that points to other Things", () => {
let thing1 = createThing({ name: "thing1" });
const thing2 = mockThingFrom("https://some.pod/resource#thing2");
const thing3 = createThing({ name: "thing3" });
thing1 = addIri(thing1, "https://some.vocab/predicate", thing2);
thing1 = addIri(thing1, "https://some.vocab/predicate", thing3);
expect(thingAsMarkdown(thing1)).toBe(
"## Thing (no URL yet — identifier: `#thing1`)\n" +