~bigbes/sr-ht-spec

ref: 65eac3cf812b912b1cdc5b160de08b569ac54510 sr-ht-spec/search/fixture_test.go -rw-r--r-- 2.2 KiB
65eac3cf — Eugene Blikh feat(service): write plane — Propose, Merge, ListProposals (Phase 3) 26 days ago
                                                                                
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
package search

import (
	"context"
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/require"

	"sourcecraft.dev/bigbes/sr-ht-spec/core"
	"sourcecraft.dev/bigbes/sr-ht-spec/doc"
	"sourcecraft.dev/bigbes/sr-ht-spec/gitx"
)

// space is a shorthand for a valid SpaceRef in tests.
func space(t *testing.T, s string) core.SpaceRef {
	t.Helper()
	sp, err := core.ParseSpaceRef(s)
	require.NoError(t, err)
	return sp
}

// corpus is a space's worth of documents, built in memory. No repository, no
// checkout: doc.FromDocuments is the seam that takes a document set directly,
// and gitx.Document is exactly what a tree walk yields.
type corpus struct {
	Space core.SpaceRef
	Rev   string
	docs  []gitx.Document
}

func newCorpus(t *testing.T, ref, rev string) *corpus {
	t.Helper()
	return &corpus{Space: space(t, ref), Rev: rev}
}

func (c *corpus) add(path, body string) *corpus {
	c.docs = append(c.docs, gitx.Document{Path: path, Data: []byte(body)})
	return c
}

// extract builds the archive and projects it into indexable documents, which is
// the whole of what service/ will do between a tree walk and a rebuild.
func (c *corpus) extract(t *testing.T) []Document {
	t.Helper()
	arc := doc.FromDocuments(c.Space, c.Rev, c.docs)
	docs, err := Extract(arc, Bodies(c.docs))
	require.NoError(t, err)
	return docs
}

// openIndex opens an empty index in a temp dir, closed at the end of the test.
func openIndex(t *testing.T) *Index {
	t.Helper()
	idx, err := Open(filepath.Join(t.TempDir(), "spec.bleve"))
	require.NoError(t, err)
	t.Cleanup(func() { require.NoError(t, idx.Close()) })
	return idx
}

// indexCorpus opens an index and rebuilds every corpus into it.
func indexCorpus(t *testing.T, corpora ...*corpus) *Index {
	t.Helper()
	idx := openIndex(t)
	for _, c := range corpora {
		_, err := idx.RebuildSpace(context.Background(), c.Space, c.extract(t))
		require.NoError(t, err)
	}
	return idx
}

// hitIDs is the ordered list of document ids a search returned.
func hitIDs(t *testing.T, idx *Index, q Query) []string {
	t.Helper()
	res, err := idx.Search(context.Background(), q)
	require.NoError(t, err)
	ids := make([]string, 0, len(res.Hits))
	for _, h := range res.Hits {
		ids = append(ids, h.ID)
	}
	return ids
}