Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/helpers_test.go
T
jschoubben 1e826ff008 I8, H-A-82: the earlier take-in goes; no delivered version is kept
I8: the take-in fed by the forge bridge's messages goes with B1's forgemsg: the FORGE
consumer and its handlers, the group end, the pending state with its keys, claims, dry
take-in and hand tools (indexer_pending, indexer_take_in_pending, indexer_drop_pending,
indexer_take_in_merge, kept again for the held take-in in I5), the catch-up by comparison
(kickBehind, TakeInBehind, sayUntaken, takeInHeads, kickCatchUp, CatchUp, merge-untaken),
the source numbering in memory (nextSource), the source verb's follow and Src stamping on
a source answer, the bridge's merged message call, the pull request candidates written
from pull and open messages, the checker's previews and verdict hearing. A repository's
trees at a commit are the internal whole (wholeAt), which ApplyMessage takes in; the dry
indexer stays for a run on request's candidate. The manifest drops the consumes of
forge.**, the checker's verdict and version-delivered, the reads of the checker's
previews and the invokes of open-pulls, bridge_merged_message and mesh-delivery.source.
RoomAgain says room when a take-in is held on a full store (heldFull, I5 holds it).

H-A-82: the fill of delivered versions into the edges goes: HearVersionDelivered, the
delivered.<module> key, the first fill's kinds 2, 4 and 6 from the placement and the
stream running, and outdated's kinds 2, 4 and 6; their entries read not yet delivered.

The tests of the removed paths go (index_test's candidate groups, source_test,
branch_take_in_test, previews_test, candidate_manifests_test, the FORGE handle tests);
the versions tests now hold kinds 2, 4 and 6 as declarations. The integration tests
still drive FORGE and are rewritten in I10.

Builds: design 47 H §743, §744, §764 (the indexer); H-A-48, H-A-82

Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
2026-11-20 04:56:27 +01:00

151 lines
3.8 KiB
Go

package index
import (
"context"
"encoding/json"
"maps"
"slices"
"sync"
mcseat "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions"
"git.novox.be/novox/mesh-sdk/go/views"
)
// rawList is the list a kept predefined value holds, as it was written.
func rawList(value []byte) json.RawMessage {
var kept struct {
List json.RawMessage `json:"list"`
}
_ = json.Unmarshal(value, &kept)
return kept.List
}
// memObjects is an object bucket in memory with a cap: a put past the cap is refused as the bus's DiscardNew refuses it.
type memObjects struct {
mu sync.Mutex
objs map[string][]byte
puts int
capacity int64
}
func newMemObjects(capacity int64) *memObjects {
return &memObjects{objs: map[string][]byte{}, capacity: capacity}
}
func (m *memObjects) used() int64 {
var n int64
for _, d := range m.objs {
n += int64(len(d))
}
return n
}
func (m *memObjects) Put(_ context.Context, name string, data []byte) error {
m.mu.Lock()
defer m.mu.Unlock()
if m.capacity > 0 && m.used()-int64(len(m.objs[name]))+int64(len(data)) > m.capacity {
return errObjectsFullForTest
}
m.objs[name] = slices.Clone(data)
m.puts++
return nil
}
func (m *memObjects) Get(_ context.Context, name string) ([]byte, error) {
m.mu.Lock()
defer m.mu.Unlock()
d, ok := m.objs[name]
if !ok {
return nil, ErrNoObject
}
return slices.Clone(d), nil
}
func (m *memObjects) Delete(_ context.Context, name string) error {
m.mu.Lock()
defer m.mu.Unlock()
delete(m.objs, name)
return nil
}
func (m *memObjects) Names(context.Context) ([]string, error) {
m.mu.Lock()
defer m.mu.Unlock()
return slices.Sorted(maps.Keys(m.objs)), nil
}
func (m *memObjects) Fill(context.Context) (int64, int64, error) {
m.mu.Lock()
defer m.mu.Unlock()
return m.used(), m.capacity, nil
}
var errObjectsFullForTest = &objectsFullError{}
type objectsFullError struct{}
func (*objectsFullError) Error() string { return "maximum bytes exceeded" }
// memFindings is the finding writer in memory (FindingWriter): what is written is kept by its key as the finding's JSON, what
// is cleared is gone, and Held answers what is kept.
type memFindings struct {
vals map[string][]byte
err error // every write's error
}
func newFindings() *memFindings { return &memFindings{vals: map[string][]byte{}} }
func (m *memFindings) Write(_ context.Context, f mcseat.Finding) error {
if m.err != nil {
return m.err
}
raw, err := json.Marshal(f)
m.vals[views.FindingKey(f.Scope, f.ID, f.Kind)] = raw
return err
}
func (m *memFindings) Clear(_ context.Context, scope, id, kind, _ string) error {
delete(m.vals, views.FindingKey(scope, id, kind))
return nil
}
func (m *memFindings) Held() []mcseat.Finding {
var out []mcseat.Finding
for _, k := range slices.Sorted(maps.Keys(m.vals)) {
var f mcseat.Finding
if json.Unmarshal(m.vals[k], &f) == nil {
out = append(out, f)
}
}
return out
}
// testSelf is the module the tests' indexer runs as, its credential's: its findings, its own entry and its graph key are named
// from it, never from a constant (design 47 G A-296).
const testSelf = "acme-indexer"
// Apply takes one repository's trees in, as a take-in does (ApplyMessage), for the tests.
func (ix *Indexer) Apply(ctx context.Context, f whole) error {
_, err := ix.ApplyMessage(ctx, f)
return err
}
const otherMerge = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
// srcT is a source revision's heads as the tests write them; applyS makes it the latest source revision the indexer took in.
type srcT struct {
Group string
Heads map[string]string
}
func sourceOf(repo, head string) srcT { return srcT{Group: "g", Heads: map[string]string{repo: head}} }
func applyS(ix *Indexer, _ context.Context, rev uint64, s srcT) error {
ix.mu.Lock()
defer ix.mu.Unlock()
if rev >= ix.source.rev {
ix.source = sourceNow{rev: rev, heads: maps.Clone(s.Heads)}
}
return nil
}