-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecord_test.go
More file actions
1232 lines (1087 loc) · 35.3 KB
/
Copy pathrecord_test.go
File metadata and controls
1232 lines (1087 loc) · 35.3 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 (c) 2026 Z5Labs and Contributors
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
package dfcad
import (
"slices"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// The model the claim-authoring tests change: a vocabulary with a predicate of
// each shape worth writing from a caller, and rooms carrying claims in each of
// the states a correction has to tell apart — one which nothing has ever said
// anything about, one stated once by a claim which wrote no id, and one stated
// twice by claims which did.
const (
recordRegistry = `(project
(label "Recording fixture")
(globalid-namespace "https://example.org/models/record"))
(namespace frame (description "Coordinate frames declared by this model."))
(namespace method (description "Measurement methods used on this project."))
(namespace site (description "Semantic nodes minted by this model."))
(frame frame:site-grid (label "Site survey grid") (unit m))
(type MeetingRoom
(kind Space)
(geometry area)
(description "An enclosed room used for meetings."))
(predicate area
(unit m2)
(shape scalar)
(description "How much floor a space has."))
(predicate centroid
(unit m)
(shape coordinate)
(dimension 3)
(description "Where the middle of a thing is."))
(predicate finish
(shape text)
(description "What the floor is finished in."))
(predicate occupancy
(shape scalar)
(strict #t)
(description "How many people it seats."))
(predicate nominal-area
(unit m2)
(shape scalar)
(claim-bearing #f)
(description "The area the brief asked for, which is not a measurement."))
`
recordModel = `(node site:S-101
(label "Meeting Room A")
(kind Space)
(type MeetingRoom)
(geometry area)
(frame frame:site-grid))
(node site:S-102
(label "Meeting Room B")
(kind Space)
(type MeetingRoom)
(geometry area)
(frame frame:site-grid)
(area
(value 24.2 m2)
(source "Design drawing DR-2026-004, Acme Architects")
(method method:estimate)
(accuracy (independent 0.5 m2))
(date "2026-01-12")))
(node site:S-103
(label "Meeting Room C")
(kind Space)
(type MeetingRoom)
(geometry area)
(frame frame:site-grid)
(area
(id site:M-0001)
(value 31.0 m2)
(source "Design drawing DR-2026-004, Acme Architects")
(method method:estimate)
(accuracy (independent 0.5 m2))
(date "2026-01-12"))
(area
(id site:M-0002)
(value 31.4 m2)
(source "As-built check AB-2026-011, Acme Surveys")
(method method:total-station)
(accuracy (independent 0.05 m2))
(date "2026-05-06")))
(node site:S-104
(label "Meeting Room D")
(kind Space)
(type MeetingRoom)
(geometry area)
(frame frame:site-grid)
(area
(id site:M-0003)
(value 12.0 m2)
(source "Design drawing DR-2026-004, Acme Architects")
(method method:estimate)
(accuracy (independent 0.5 m2))
(date "2026-01-12")))
`
)
// recordFixture writes the model the claim-authoring tests change and returns
// its root.
func recordFixture(t *testing.T) string {
t.Helper()
return tree(t, map[string]string{
"registry.dfc": recordRegistry,
"entities/site.dfc": recordModel,
})
}
// aClaim is a well-formed claim of the fixture's scalar predicate, which every
// case below starts from and changes the one axis it is about.
func aClaim() ClaimSpec {
return ClaimSpec{
Subject: "site:S-101",
Predicate: "area",
Value: ScalarValue(24.2, "m2"),
Source: "As-built check AB-2026-009, Acme Surveys",
Method: "method:total-station",
Accuracy: []AccuracyTerm{{Kind: TermIndependent, Magnitude: 0.05, Unit: "m2"}},
Date: time.Date(2026, 5, 6, 0, 0, 0, 0, time.UTC),
}
}
// onlyClaim is the one claim written on a subject under a predicate, requiring
// the model to hold exactly one.
func onlyClaim(t *testing.T, graph *Graph, subject ID, predicate string) *Claim {
t.Helper()
claims := slices.Collect(graph.Claims().Under(subject, predicate))
require.Len(t, claims, 1)
return claims[0]
}
func TestTxAddClaim(t *testing.T) {
testCases := []struct {
name string
spec func(spec ClaimSpec) ClaimSpec
expected string
}{
{
name: "writes a claim carrying every axis",
spec: func(spec ClaimSpec) ClaimSpec { return spec },
expected: `(area
(value 24.2 m2)
(source "As-built check AB-2026-009, Acme Surveys")
(method method:total-station)
(accuracy (independent 0.05 m2))
(date "2026-05-06"))`,
},
{
name: "writes the least a claim may say, which carries no accuracy",
spec: func(spec ClaimSpec) ClaimSpec {
spec.Accuracy = nil
return spec
},
expected: `(area
(value 24.2 m2)
(source "As-built check AB-2026-009, Acme Surveys")
(method method:total-station)
(date "2026-05-06"))`,
},
{
name: "writes an id where one was asked for",
spec: func(spec ClaimSpec) ClaimSpec {
spec.ID = "site:M-0100"
return spec
},
expected: `(area
(id site:M-0100)
(value 24.2 m2)`,
},
{
name: "writes a systematic term beside an independent one",
spec: func(spec ClaimSpec) ClaimSpec {
spec.Accuracy = append(spec.Accuracy, AccuracyTerm{
Kind: TermSystematic,
Magnitude: 0.002,
Unit: "m2",
Source: "method:total-station",
})
return spec
},
expected: `(accuracy (independent 0.05 m2) (systematic 0.002 m2 method:total-station))`,
},
{
name: "writes a coordinate value with its components in order",
spec: func(spec ClaimSpec) ClaimSpec {
spec.Predicate = "centroid"
spec.Value = CoordinateValue([]float64{3.5, 1.25, 0}, "m")
spec.Accuracy = []AccuracyTerm{{Kind: TermIndependent, Magnitude: 0.01, Unit: "m"}}
return spec
},
expected: `(value (3.5 1.25 0.0) m)`,
},
{
name: "writes a text value, which carries no unit",
spec: func(spec ClaimSpec) ClaimSpec {
spec.Predicate = "finish"
spec.Value = TextValue("sealed concrete")
spec.Accuracy = nil
return spec
},
expected: `(value "sealed concrete")`,
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
root := recordFixture(t)
spec := testCase.spec(aClaim())
graph := authored(t, root, func(tx *Tx) error {
_, _, err := tx.AddClaim(spec)
return err
})
claim := onlyClaim(t, graph, spec.Subject, spec.Predicate)
assert.Equal(t, spec.Value.Shape(), claim.Value().Shape())
assert.Equal(t, spec.Value.Unit(), claim.Value().Unit())
assert.Equal(t, spec.Source, claim.Source())
assert.Equal(t, spec.Method, claim.Method())
assert.Equal(t, spec.Date, claim.Date())
assert.Equal(t, RankNormal, claim.Rank())
assert.Equal(t, len(spec.Accuracy) > 0, claim.Rankable())
assert.Contains(t, entityFile(t, root), testCase.expected)
})
}
}
// TestTxAddClaimReportsWhatItLeftBehind is its own function because it asserts
// about the notices rather than about what was written: a claim which cannot be
// ranked and a claim which disagrees with one already there are both legitimate,
// and both are things to hear about now rather than to find out later.
func TestTxAddClaimReportsWhatItLeftBehind(t *testing.T) {
testCases := []struct {
name string
spec func(spec ClaimSpec) ClaimSpec
expectedKinds []NoticeKind
expectedCompeting int
}{
{
name: "says nothing about a rankable claim on a subject nothing states",
spec: func(spec ClaimSpec) ClaimSpec { return spec },
expectedKinds: nil,
},
{
name: "reports a claim with no accuracy as unrankable",
spec: func(spec ClaimSpec) ClaimSpec {
spec.Accuracy = nil
return spec
},
expectedKinds: []NoticeKind{NoticeUnrankable},
},
{
name: "reports a second claim on one pair as a conflict, naming the competing claim",
spec: func(spec ClaimSpec) ClaimSpec {
spec.Subject = "site:S-102"
return spec
},
expectedKinds: []NoticeKind{NoticeConflict},
expectedCompeting: 1,
},
{
name: "reports both where both hold",
spec: func(spec ClaimSpec) ClaimSpec {
spec.Subject, spec.Accuracy = "site:S-103", nil
return spec
},
expectedKinds: []NoticeKind{NoticeUnrankable, NoticeConflict},
expectedCompeting: 2,
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
spec := testCase.spec(aClaim())
tx := begin(t, recordFixture(t))
defer func() { _ = tx.Close() }()
_, notices, err := tx.AddClaim(spec)
require.NoError(t, err)
kinds := make([]NoticeKind, 0, len(notices))
for _, notice := range notices {
kinds = append(kinds, notice.Kind)
assert.Equal(t, spec.Subject, notice.Subject)
assert.Equal(t, spec.Predicate, notice.Predicate)
assert.NotEmpty(t, notice.Message())
}
assert.Equal(t, testCase.expectedKinds, sliceOrNil(kinds))
for _, notice := range notices {
if notice.Kind == NoticeConflict {
assert.Len(t, notice.Competing, testCase.expectedCompeting)
}
}
})
}
}
func TestTxAddClaimChecksEveryAxisAgainstTheRegistry(t *testing.T) {
testCases := []struct {
name string
spec func(spec ClaimSpec) ClaimSpec
expected error
}{
{
name: "refuses a predicate nothing declares",
spec: func(spec ClaimSpec) ClaimSpec {
spec.Predicate = "aera"
return spec
},
expected: UnknownAxisError{Axis: "predicate", Value: "aera"},
},
{
name: "refuses a claim under a predicate declared to take a plain value",
spec: func(spec ClaimSpec) ClaimSpec {
spec.Predicate = "nominal-area"
return spec
},
expected: NotClaimBearingError{Predicate: "nominal-area"},
},
{
name: "refuses a value of another shape than the predicate declares",
spec: func(spec ClaimSpec) ClaimSpec {
spec.Value = TextValue("about twenty-four square metres")
return spec
},
expected: ValueShapeError{Predicate: "area", Want: ShapeScalar, Got: ShapeText},
},
{
name: "refuses a claim carrying no value at all",
spec: func(spec ClaimSpec) ClaimSpec {
spec.Value = Value{}
return spec
},
expected: ValueShapeError{Predicate: "area", Want: ShapeScalar},
},
{
name: "refuses a coordinate of the wrong dimension",
spec: func(spec ClaimSpec) ClaimSpec {
spec.Predicate = "centroid"
spec.Value = CoordinateValue([]float64{3.5, 1.25}, "m")
return spec
},
expected: DimensionError{Predicate: "centroid", Want: 3, Got: 2},
},
{
name: "refuses a unit other than the declared one",
spec: func(spec ClaimSpec) ClaimSpec {
spec.Value = ScalarValue(24.2, "ft")
return spec
},
expected: UnitError{Predicate: "area", Want: "m2", Got: "ft"},
},
{
name: "refuses a dimensional value written with no unit",
spec: func(spec ClaimSpec) ClaimSpec {
spec.Value = ScalarValue(24.2, "")
return spec
},
expected: UnitError{Predicate: "area", Want: "m2"},
},
{
name: "refuses a unit on a predicate which declares none",
spec: func(spec ClaimSpec) ClaimSpec {
spec.Predicate = "occupancy"
spec.Value = ScalarValue(12, "m2")
return spec
},
expected: UnitError{Predicate: "occupancy", Got: "m2"},
},
{
name: "refuses a claim with nothing evidencing it",
spec: func(spec ClaimSpec) ClaimSpec {
spec.Source = ""
return spec
},
expected: MissingChildError{Predicate: "area", Child: "source"},
},
{
name: "refuses a claim which does not say how the value was obtained",
spec: func(spec ClaimSpec) ClaimSpec {
spec.Method = ""
return spec
},
expected: MissingChildError{Predicate: "area", Child: "method"},
},
{
name: "refuses a method whose namespace nobody declared",
spec: func(spec ClaimSpec) ClaimSpec {
spec.Method = "instrument:total-station"
return spec
},
expected: UnknownAxisError{Axis: "namespace", Value: "instrument"},
},
{
name: "refuses a systematic term whose namespace nobody declared",
spec: func(spec ClaimSpec) ClaimSpec {
spec.Accuracy = []AccuracyTerm{{
Kind: TermSystematic, Magnitude: 0.002, Unit: "m2", Source: "instrument:baseline",
}}
return spec
},
expected: UnknownAxisError{Axis: "namespace", Value: "instrument"},
},
{
name: "refuses a claim about nothing",
spec: func(spec ClaimSpec) ClaimSpec {
spec.Subject = ""
return spec
},
expected: ErrNoSubject,
},
{
name: "refuses a claim under no predicate",
spec: func(spec ClaimSpec) ClaimSpec {
spec.Predicate = ""
return spec
},
expected: ErrNoPredicate,
},
{
name: "refuses a subject the model does not hold, naming the nearest",
spec: func(spec ClaimSpec) ClaimSpec {
spec.Subject = "site:S-1O1"
return spec
},
expected: UnknownEntityError{ID: "site:S-1O1", Nearest: "site:S-101"},
},
{
name: "refuses an id something already holds",
spec: func(spec ClaimSpec) ClaimSpec {
spec.ID = "site:M-0001"
return spec
},
expected: TakenIDError{ID: "site:M-0001"},
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
root := recordFixture(t)
spec := testCase.spec(aClaim())
err := rejected(t, root, func(tx *Tx) error {
_, _, err := tx.AddClaim(spec)
return err
})
assertRefusal(t, err, testCase.expected)
})
}
}
// assertRefusal requires err to be the same refusal as expected, compared by
// type and by the fields which say what was refused.
//
// The message is never compared. What was expected, what was found and which
// axis they are about are the things a caller acts on, and the sentence around
// them is presentation.
func assertRefusal(t *testing.T, err, expected error) {
t.Helper()
switch want := expected.(type) {
case UnknownAxisError:
var got UnknownAxisError
require.ErrorAs(t, err, &got)
assert.Equal(t, want.Axis, got.Axis)
assert.Equal(t, want.Value, got.Value)
assert.NotEmpty(t, got.Permitted, "the refusal names what would have been permitted")
case NotClaimBearingError:
var got NotClaimBearingError
require.ErrorAs(t, err, &got)
assert.Equal(t, want.Predicate, got.Predicate)
assert.NotEmpty(t, got.Written, "the refusal spells the plain value it wanted instead")
case ValueShapeError:
var got ValueShapeError
require.ErrorAs(t, err, &got)
assert.Equal(t, want, got)
case DimensionError:
var got DimensionError
require.ErrorAs(t, err, &got)
assert.Equal(t, want, got)
case UnitError:
var got UnitError
require.ErrorAs(t, err, &got)
assert.Equal(t, want, got)
case MissingChildError:
var got MissingChildError
require.ErrorAs(t, err, &got)
assert.Equal(t, want.Predicate, got.Predicate)
assert.Equal(t, want.Child, got.Child)
case UnknownEntityError:
var got UnknownEntityError
require.ErrorAs(t, err, &got)
assert.Equal(t, want, got)
case TakenIDError:
var got TakenIDError
require.ErrorAs(t, err, &got)
assert.Equal(t, want.ID, got.ID)
default:
assert.ErrorIs(t, err, expected)
}
}
func TestTxDeprecateClaim(t *testing.T) {
root := recordFixture(t)
graph := authored(t, root, func(tx *Tx) error {
_, err := tx.DeprecateClaim("site:M-0001", "site:M-0002")
return err
})
retracted, ok := graph.Claims().Claim("site:M-0001")
require.True(t, ok)
assert.Equal(t, RankDeprecated, retracted.Rank())
replacement, ok := retracted.SupersededBy()
require.True(t, ok)
assert.Equal(t, ID("site:M-0002"), replacement)
// Everything else it said is exactly what it said. Retracting is not
// editing: the record of what was believed is the thing being kept.
value, ok := retracted.Value().Scalar()
require.True(t, ok)
assert.Equal(t, 31.0, value)
assert.Equal(t, "Design drawing DR-2026-004, Acme Architects", retracted.Source())
assert.Equal(t, ID("method:estimate"), retracted.Method())
assert.Equal(t, "2026-01-12", retracted.Date().Format(dateLayout))
// And the chain is walkable in both directions from either end.
current, ok := graph.Claims().Current(retracted)
require.True(t, ok)
id, ok := current.ID()
require.True(t, ok)
assert.Equal(t, ID("site:M-0002"), id)
}
// TestTxDeprecateClaimReportsASubjectLeftWithNoValue is its own function
// because the assertion is about what the model no longer says rather than
// about the claim which was retracted.
func TestTxDeprecateClaimReportsASubjectLeftWithNoValue(t *testing.T) {
root := recordFixture(t)
// Room C states its area twice, so retracting one of the two leaves the
// pair with something still asserted and there is nothing to report.
tx := begin(t, root)
notices, err := tx.DeprecateClaim("site:M-0001", "site:M-0002")
require.NoError(t, err)
assert.Empty(t, notices, "a pair with a live claim left has a resolvable value")
require.NoError(t, tx.Close())
// Room D states its area once, and the claim standing in its place is a
// measurement of another room — so the pair is left with nothing at all.
after := authored(t, root, func(tx *Tx) error {
notices, err = tx.DeprecateClaim("site:M-0003", "site:M-0002")
return err
})
require.Len(t, notices, 1)
assert.Equal(t, NoticeUnresolvable, notices[0].Kind)
assert.Equal(t, ID("site:S-104"), notices[0].Subject)
assert.Equal(t, "area", notices[0].Predicate)
assert.Equal(t, ID("site:M-0003"), notices[0].Claim)
assert.Empty(t, after.Claims().Live("site:S-104", "area"))
}
func TestTxDeprecateClaimRefusesWhatItCannotRetract(t *testing.T) {
testCases := []struct {
name string
id ID
supersededBy ID
expected error
}{
{
name: "refuses a deprecation which names nothing to stand in its place",
id: "site:M-0001",
supersededBy: "",
expected: MissingReplacementError{ID: "site:M-0001"},
},
{
name: "refuses a claim named as its own replacement",
id: "site:M-0001",
supersededBy: "site:M-0001",
expected: SelfSupersessionError{ID: "site:M-0001"},
},
{
name: "refuses a claim id nothing carries",
id: "site:M-0009",
supersededBy: "site:M-0002",
expected: UnknownClaimError{ID: "site:M-0009"},
},
{
name: "refuses a replacement which names no claim",
id: "site:M-0001",
supersededBy: "site:M-0009",
expected: UnknownClaimError{ID: "site:M-0009"},
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
err := rejected(t, recordFixture(t), func(tx *Tx) error {
_, err := tx.DeprecateClaim(testCase.id, testCase.supersededBy)
return err
})
switch want := testCase.expected.(type) {
case MissingReplacementError:
var got MissingReplacementError
require.ErrorAs(t, err, &got)
assert.Equal(t, want.ID, got.ID)
case SelfSupersessionError:
var got SelfSupersessionError
require.ErrorAs(t, err, &got)
assert.Equal(t, want.ID, got.ID)
case UnknownClaimError:
var got UnknownClaimError
require.ErrorAs(t, err, &got)
assert.Equal(t, want.ID, got.ID)
}
})
}
}
// TestTxDeprecateClaimRefusesARetractionOfARetraction is its own function
// because the state it refuses is one the model is already in rather than one
// the arguments describe.
func TestTxDeprecateClaimRefusesARetractionOfARetraction(t *testing.T) {
root := recordFixture(t)
tx := begin(t, root)
_, err := tx.DeprecateClaim("site:M-0001", "site:M-0002")
require.NoError(t, err)
_, _, err = tx.Commit()
require.NoError(t, err)
err = rejected(t, root, func(tx *Tx) error {
_, err := tx.DeprecateClaim("site:M-0001", "site:M-0002")
return err
})
var already AlreadyDeprecatedError
require.ErrorAs(t, err, &already)
assert.Equal(t, ID("site:M-0001"), already.ID)
assert.Equal(t, ID("site:M-0002"), already.SupersededBy)
}
func TestTxSupersede(t *testing.T) {
root := recordFixture(t)
spec := aClaim()
spec.Subject, spec.Value = "site:S-102", ScalarValue(24.6, "m2")
spec.Source = "Re-measure RM-2026-002, Acme Surveys"
var minted ID
graph := authored(t, root, func(tx *Tx) error {
id, _, err := tx.Supersede(spec)
minted = id
return err
})
// The new claim carries the id which was minted for it, which is the id the
// retraction names — and it was minted because of that reference and for no
// other reason.
assert.Equal(t, ID("site:S-102:area:1"), minted)
replacement, ok := graph.Claims().Claim(minted)
require.True(t, ok)
assert.Equal(t, RankNormal, replacement.Rank())
value, ok := replacement.Value().Scalar()
require.True(t, ok)
assert.Equal(t, 24.6, value)
// The claim it corrected is retracted in its favour, and says everything it
// said before: correction is supersession, never an edit.
retracted := slices.Collect(graph.Claims().Replaced(replacement))
require.Len(t, retracted, 1)
assert.Equal(t, RankDeprecated, retracted[0].Rank())
was, ok := retracted[0].Value().Scalar()
require.True(t, ok)
assert.Equal(t, 24.2, was)
assert.Equal(t, "Design drawing DR-2026-004, Acme Architects", retracted[0].Source())
// The pair is not left in dispute: a retracted claim is never competing, so
// the correction resolves it rather than doubling it.
assert.Len(t, graph.Claims().Live("site:S-102", "area"), 1)
for conflict := range graph.Claims().Conflicts() {
assert.NotEqual(t, ID("site:S-102"), conflict.Subject(),
"a correction is not a disagreement")
}
}
func TestTxSupersedeRefusesWhatItCannotCorrect(t *testing.T) {
testCases := []struct {
name string
subject ID
assert func(t *testing.T, err error)
}{
{
name: "refuses a pair the model says nothing about",
subject: "site:S-101",
assert: func(t *testing.T, err error) {
var got NothingToSupersedeError
require.ErrorAs(t, err, &got)
assert.Equal(t, ID("site:S-101"), got.Subject)
assert.Equal(t, "area", got.Predicate)
},
},
{
name: "refuses a pair the model states more than once, naming the competing claims",
subject: "site:S-103",
assert: func(t *testing.T, err error) {
var got AmbiguousSupersessionError
require.ErrorAs(t, err, &got)
assert.Equal(t, ID("site:S-103"), got.Subject)
assert.Equal(t, []string{"site:M-0001", "site:M-0002"}, got.Competing)
},
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
spec := aClaim()
spec.Subject = testCase.subject
err := rejected(t, recordFixture(t), func(tx *Tx) error {
_, _, err := tx.Supersede(spec)
return err
})
testCase.assert(t, err)
})
}
}
// TestClaimsWrittenByTheSameTransactionCount walks the three questions a
// transaction asks about a claim id, each of which has to count the claims the
// same change has already written rather than only the ones it loaded.
//
// It is its own function because the property is about the transaction rather
// than about any one mutation: the graph a Tx holds is the model as it found it,
// so every question answered from the graph alone is wrong by exactly the claims
// the change is in the middle of writing.
func TestClaimsWrittenByTheSameTransactionCount(t *testing.T) {
t.Run("retracts a claim in favour of one the same change wrote", func(t *testing.T) {
root := recordFixture(t)
graph := authored(t, root, func(tx *Tx) error {
spec := aClaim()
spec.ID, spec.Subject, spec.Value = "site:M-0100", "site:S-103", ScalarValue(31.2, "m2")
if _, _, err := tx.AddClaim(spec); err != nil {
return err
}
// The replacement is not in the model the transaction loaded, and
// naming it is the whole of what the second step is for.
_, err := tx.DeprecateClaim("site:M-0001", "site:M-0100")
return err
})
replacement, ok := graph.Claims().Claim("site:M-0100")
require.True(t, ok)
replaced := slices.Collect(graph.Claims().Replaced(replacement))
require.Len(t, replaced, 1)
id, ok := replaced[0].ID()
require.True(t, ok)
assert.Equal(t, ID("site:M-0001"), id)
})
t.Run("refuses a second claim written under an id the same change took", func(t *testing.T) {
err := rejected(t, recordFixture(t), func(tx *Tx) error {
spec := aClaim()
spec.ID = "site:M-0100"
if _, _, err := tx.AddClaim(spec); err != nil {
return err
}
spec.Subject = "site:S-102"
_, _, err := tx.AddClaim(spec)
return err
})
var taken TakenIDError
require.ErrorAs(t, err, &taken)
assert.Equal(t, ID("site:M-0100"), taken.ID)
})
t.Run("mints past an id the same change already wrote a claim under", func(t *testing.T) {
tx := begin(t, recordFixture(t))
defer func() { _ = tx.Close() }()
spec := aClaim()
spec.ID, spec.Subject = "site:S-102:area:1", "site:S-101"
_, _, err := tx.AddClaim(spec)
require.NoError(t, err)
minted, err := tx.MintClaimID("site:S-102", "area")
require.NoError(t, err)
assert.Equal(t, ID("site:S-102:area:2"), minted)
})
}
// TestMintClaimIDIsStableAndFree checks the half of the generated format which
// is a promise: the id is well formed, it says what it is derived from, and it
// never lands on something the model already holds.
func TestMintClaimIDIsStableAndFree(t *testing.T) {
tx := begin(t, recordFixture(t))
defer func() { _ = tx.Close() }()
minted, err := tx.MintClaimID("site:S-102", "area")
require.NoError(t, err)
assert.Equal(t, ID("site:S-102:area:1"), minted)
// Twice over the same model is the same answer, because nothing was
// written between the two.
again, err := tx.MintClaimID("site:S-102", "area")
require.NoError(t, err)
assert.Equal(t, minted, again)
// What comes back is an id: the namespace is the subject's, which the
// registry declares, and the whole of it reads back as one symbol.
parsed, err := ParseID(string(minted))
require.NoError(t, err)
assert.Equal(t, minted, parsed)
assert.Equal(t, "site", parsed.Namespace())
// And it steps over an ordinal something already holds rather than
// colliding with it.
require.NoError(t, tx.AddNode(NodeSpec{
ID: "site:S-101:area:1",
Kind: KindSpace,
Type: "MeetingRoom",
Geometry: GeometryArea,
}, "entities/site.dfc"))
next, err := tx.MintClaimID("site:S-101", "area")
require.NoError(t, err)
assert.Equal(t, ID("site:S-101:area:2"), next)
}
// TestNoAuthoringPathEditsAClaimValue is the assertion behind
// [0009](docs/decisions/0009-derived-values-are-never-written-back.md): there is
// no way through this package to change what a claim says.
//
// It walks the mutations rather than naming one, because the property is about
// the surface as a whole: a mutation added later which wrote over a value would
// be caught by this the day it was added, which is the only way a rule like this
// stays true.
func TestNoAuthoringPathEditsAClaimValue(t *testing.T) {
mutations := map[string]func(tx *Tx) error{
"AddClaim": func(tx *Tx) error {
spec := aClaim()
spec.Subject = "site:S-102"
spec.Value = ScalarValue(99.9, "m2")
_, _, err := tx.AddClaim(spec)
return err
},
"Supersede": func(tx *Tx) error {
spec := aClaim()
spec.Subject = "site:S-102"
spec.Value = ScalarValue(99.9, "m2")
_, _, err := tx.Supersede(spec)
return err
},
"DeprecateClaim": func(tx *Tx) error {
_, err := tx.DeprecateClaim("site:M-0001", "site:M-0002")
return err
},
"SetLabel": func(tx *Tx) error {
return tx.SetLabel("site:S-102", "Board Room")
},
"Retire": func(tx *Tx) error {
return tx.Retire("site:S-102", RetirementSpec{Reason: "Knocked through."})
},
}
for name, mutate := range mutations {
t.Run(name+" leaves every claim already written saying what it said", func(t *testing.T) {
root := recordFixture(t)
before, diags := LoadGraph(root)
require.Empty(t, diags)
was := stated(before)
for written := range stated(authored(t, root, mutate)) {
delete(was, written)
}
assert.Empty(t, was, "every claim which was there still says exactly what it said")
})
}
}
// stated is what every claim of a model says, keyed by everything about it a
// correction is forbidden to touch.
//
// The rank is deliberately not in the key: a retraction is the one thing a
// change may write onto a claim which was already there, and it says the claim
// was withdrawn rather than that it said something else.
func stated(graph *Graph) map[string]*Claim {
out := make(map[string]*Claim)
for claim := range graph.Claims().All() {
var value strings.Builder
if scalar, ok := claim.Value().Scalar(); ok {
value.WriteString(decimal(scalar))
}
if components, ok := claim.Value().Coordinate(); ok {
for _, component := range components {
value.WriteString(decimal(component) + " ")
}
}
if text, ok := claim.Value().Text(); ok {
value.WriteString(text)
}
out[strings.Join([]string{
string(claim.Subject()),
claim.Predicate(),
value.String(),
string(claim.Value().Unit()),
claim.Source(),
string(claim.Method()),
claim.Date().Format(dateLayout),
}, "|")] = claim
}
return out
}
func TestParseValue(t *testing.T) {
testCases := []struct {
name string
written string
unit Unit
declared Predicate
expected Value
expectErr ValueProblem
}{
{
name: "reads a scalar and its unit",
written: "24.2",