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
package store

import (
	"context"
	"fmt"
	"os"
)

// storeLog prints a debug line to stderr when GONIX_STORE_DEBUG is set.
func storeLog(format string, args ...any) {
	if os.Getenv("GONIX_STORE_DEBUG") != "" {
		fmt.Fprintf(os.Stderr, "[store] "+format+"\n", args...)
	}
}

// StubStore is a minimal Store implementation for testing.
// It returns reasonable defaults without talking to the nix daemon.
type StubStore struct{}

// NewStub returns a StubStore that can be used for parse-only tests.
func NewStub() *StubStore { return &StubStore{} }

var _ Store = (*StubStore)(nil)

func (s *StubStore) IsValidPath(_ context.Context, path string) (bool, error) {
	storeLog("isValidPath %q → true", path)
	return true, nil
}

func (s *StubStore) QueryPathInfo(_ context.Context, path string) (*PathInfo, error) {
	storeLog("queryPathInfo %q", path)
	return &PathInfo{
		NarHash:  make([]byte, 32),
		HashType: 3, // sha256
		NarSize:  0,
	}, nil
}

func (s *StubStore) ComputeFSClosure(_ context.Context, path string) ([]string, error) {
	storeLog("computeFSClosure %q → [%q]", path, path)
	return []string{path}, nil
}

func (s *StubStore) EnsurePath(_ context.Context, path string) error {
	storeLog("ensurePath %q", path)
	return nil
}

func (s *StubStore) AddTextToStore(_ context.Context, name, content string, refs []string) (string, error) {
	preview := content
	if len(preview) > 60 {
		preview = preview[:60] + "..."
	}
	result := ComputeTextPath(name, content, refs, StoreDir)
	storeLog("addTextToStore name=%q refs=%v content=%q → %q", name, refs, preview, result)
	return result, nil
}

func (s *StubStore) WriteDerivation(_ context.Context, d *Derivation) (string, error) {
	name := d.Name + ".drv"
	result := fmt.Sprintf("/nix/store/%s-%s", fakeHash(name), name)
	storeLog("writeDerivation name=%q → %q", d.Name, result)
	return result, nil
}

// fakeHash returns a deterministic valid Nix base-32 hash string for a given name.
// Nix base-32 uses the alphabet: 0-9 a-d f-n p-s v-z (32 chars, no e o t u).
// We produce a reproducible 32-char hash by hashing the name mod the alphabet.
func fakeHash(name string) string {
	const alphabet = "0123456789abcdfghijklmnpqrsvwxyz" // 32 valid nix base32 chars
	h := uint32(0x811c9dc5) // FNV-1a seed
	for _, c := range name {
		h ^= uint32(c)
		h *= 0x01000193
	}
	buf := make([]byte, 32)
	for i := range buf {
		buf[i] = alphabet[h%32]
		h = (h>>5) | (h<<27) // rotate
	}
	return string(buf)
}

func (s *StubStore) ReadDerivation(_ context.Context, path string) (*Derivation, error) {
	storeLog("readDerivation %q → error (not implemented)", path)
	return nil, fmt.Errorf("ReadDerivation not implemented in StubStore: %s", path)
}