Finish the D&D module cleanup

This commit is contained in:
2026-07-25 13:30:23 +00:00
parent aeaaf44ae0
commit 29ee68824d
14 changed files with 76 additions and 366 deletions

View File

@@ -15,16 +15,17 @@ type SourceRefOrder struct {
// NewSourceRefOrder captures the source identity and unit positions from doc.
func NewSourceRefOrder(doc *source.SourceDocument) SourceRefOrder {
return NewSourceRefOrderWithIndex(doc, source.NewDocumentIndex(doc))
return NewSourceRefOrderFromIndex(source.NewDocumentIndex(doc))
}
// NewSourceRefOrderWithIndex captures source identity with a supplied
// document index for source-reference comparison and canonicalization.
func NewSourceRefOrderWithIndex(doc *source.SourceDocument, index source.DocumentIndex) SourceRefOrder {
if doc == nil {
// NewSourceRefOrderFromIndex captures source identity and unit positions from
// one document index for source-reference comparison and canonicalization.
func NewSourceRefOrderFromIndex(index source.DocumentIndex) SourceRefOrder {
sourceID, ok := index.DocumentID()
if !ok {
return SourceRefOrder{}
}
return SourceRefOrder{sourceID: doc.ID, index: index}
return SourceRefOrder{sourceID: sourceID, index: index}
}
// Less orders references by source identity, then document positions when

View File

@@ -60,19 +60,22 @@ func TestSourceRefOrderCanonicalizePreservesRepresentationAndOwnership(t *testin
func TestSourceRefOrderSnapshotAndEarliestValid(t *testing.T) {
doc := unitRefSourceDocument(30, 10, 20)
order := NewSourceRefOrder(doc)
sourceID := doc.ID
index := source.NewDocumentIndex(doc)
order := NewSourceRefOrderFromIndex(index)
doc.ID = "changed"
doc.Units[0].ID, doc.Units[1].ID = 10, 30
refs := []source.SourceRef{
{SourceID: doc.ID, StartUnitID: 20, EndUnitID: 20},
{SourceID: doc.ID, StartUnitID: 10, EndUnitID: 10},
{SourceID: sourceID, StartUnitID: 20, EndUnitID: 20},
{SourceID: sourceID, StartUnitID: 10, EndUnitID: 10},
{SourceID: "other", StartUnitID: 1, EndUnitID: 1},
{SourceID: doc.ID, StartUnitID: 999, EndUnitID: 999},
{SourceID: doc.ID, StartUnitID: 20, EndUnitID: 30},
{SourceID: sourceID, StartUnitID: 999, EndUnitID: 999},
{SourceID: sourceID, StartUnitID: 20, EndUnitID: 30},
}
if position, ok := order.EarliestValid(refs); !ok || position != 1 {
t.Fatalf("EarliestValid() = %d, %t, want 1, true", position, ok)
}
if position, ok := order.EarliestValid([]source.SourceRef{{SourceID: doc.ID, StartUnitID: 20, EndUnitID: 30}}); ok || position != 0 {
if position, ok := order.EarliestValid([]source.SourceRef{{SourceID: sourceID, StartUnitID: 20, EndUnitID: 30}}); ok || position != 0 {
t.Fatalf("EarliestValid(invalid) = %d, %t, want 0, false", position, ok)
}
if position, ok := (SourceRefOrder{}).EarliestValid(refs); ok || position != 0 {