Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/index_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

131 lines
3.9 KiB
Go

package index
import (
"context"
"errors"
"strings"
"testing"
)
const (
h1 = "0123456789abcdef0123456789abcdef01234567"
h2 = "89abcdef0123456789abcdef0123456789abcdef"
h3 = "fedcba9876543210fedcba9876543210fedcba98"
)
// memStore is the candidates bucket's compare-and-set behaviour in memory: a revision per write, a conflict when the
// revision read is no longer the key's. Writes counts the writes that went through; beforeWrite lets a test change the
// key under the indexer; unbound, when set, is every read's error (a state not yet on the bus).
type memStore struct {
unbound error
keysErr error // the listing's error
vals map[string][]byte
revs map[string]uint64
next uint64
writes int
beforeWrite func()
failWrite func(key string) error // a write's own error, asked before each Create and Update
maxValue int // the bucket's value cap (buscompose.StateMaxValueBytes); a larger value is refused as the bus refuses it
}
// tooBig is the bus's refusal of a value over a bucket's cap.
func (m *memStore) tooBig(value []byte) error {
if m.maxValue > 0 && len(value) > m.maxValue {
return errors.New("nats: API error: code=400 err_code=10054 description=message size exceeds maximum allowed")
}
return nil
}
func newMem() *memStore { return &memStore{vals: map[string][]byte{}, revs: map[string]uint64{}} }
func (m *memStore) Get(_ context.Context, key string) ([]byte, uint64, bool, error) {
if m.unbound != nil {
return nil, 0, false, m.unbound
}
v, ok := m.vals[key]
return v, m.revs[key], ok, nil
}
func (m *memStore) Keys(context.Context) ([]string, error) {
if m.keysErr != nil {
return nil, m.keysErr
}
var keys []string
for k := range m.vals {
keys = append(keys, k)
}
return keys, nil
}
func (m *memStore) hook() {
if h := m.beforeWrite; h != nil {
m.beforeWrite = nil
h()
}
}
func (m *memStore) put(key string, value []byte) {
m.next++
m.vals[key], m.revs[key] = value, m.next
}
func (m *memStore) Create(_ context.Context, key string, value []byte) error {
m.hook()
if f := m.failWrite; f != nil {
if err := f(key); err != nil {
return err
}
}
if _, ok := m.vals[key]; ok {
return ErrConflict
}
if err := m.tooBig(value); err != nil {
return err
}
m.writes++
m.put(key, value)
return nil
}
func (m *memStore) Update(_ context.Context, key string, value []byte, rev uint64) error {
m.hook()
if f := m.failWrite; f != nil {
if err := f(key); err != nil {
return err
}
}
if _, ok := m.vals[key]; !ok || m.revs[key] != rev {
return ErrConflict
}
if err := m.tooBig(value); err != nil {
return err
}
m.writes++
m.put(key, value)
return nil
}
func (m *memStore) Delete(_ context.Context, key string, rev uint64) error {
m.hook()
if _, ok := m.vals[key]; !ok || m.revs[key] != rev {
return ErrConflict
}
m.writes++
delete(m.vals, key)
return nil
}
// buildRecord is the controller's record as mesh-sdk go/delivery/testdata/build-record-build-agent.json (captured from
// the controller's encoder; copied here, the vendored SDK does not carry testdata) has it, with the repository, module and path changed.
func buildRecord(t *testing.T, repo, module, path, at string) []byte {
t.Helper()
return logged(t, "build-"+module, module, repo, path, "", "", at)
}
func newIndexer(t *testing.T) (*Indexer, *memStore, *strings.Builder) {
t.Helper()
st, logs := newMem(), &strings.Builder{}
b := NewBuilds()
_ = b.Put(buildRecord(t, "novox/mesh-delivery", "a", "modules/a", "2026-10-19T09:00:00Z"))
_ = b.Put(buildRecord(t, "novox/mesh-catalog", "c", "modules/c", "2026-10-19T09:00:00Z"))
_ = b.Put(buildRecord(t, "novox/mesh-delivery", "odd-name", "modules/odd", "2026-10-19T09:00:00Z"))
return &Indexer{Module: testSelf, Store: st, Builds: b, Logf: func(f string, a ...any) {
logs.WriteString(strings.TrimSpace(strings.ReplaceAll(f, "\n", " ")) + "\n")
_ = a
}}, st, logs
}