|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/agenthub/edge-server/internal/store" |
| 10 | +) |
| 11 | + |
| 12 | +func TestBuildConfigDefaultsToMemoryStore(t *testing.T) { |
| 13 | + cfg, err := buildConfig(nil) |
| 14 | + if err != nil { |
| 15 | + t.Fatalf("buildConfig returned error: %v", err) |
| 16 | + } |
| 17 | + |
| 18 | + if cfg.Addr != "127.0.0.1:3210" { |
| 19 | + t.Fatalf("Addr = %q, want default listen address", cfg.Addr) |
| 20 | + } |
| 21 | + if cfg.StoreFile != "" { |
| 22 | + t.Fatalf("StoreFile = %q, want empty", cfg.StoreFile) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func TestBuildConfigParsesStoreFile(t *testing.T) { |
| 27 | + cfg, err := buildConfig([]string{"--addr", "127.0.0.1:4321", "--store-file", "edge-store.json"}) |
| 28 | + if err != nil { |
| 29 | + t.Fatalf("buildConfig returned error: %v", err) |
| 30 | + } |
| 31 | + |
| 32 | + if cfg.Addr != "127.0.0.1:4321" { |
| 33 | + t.Fatalf("Addr = %q, want parsed address", cfg.Addr) |
| 34 | + } |
| 35 | + if cfg.StoreFile != "edge-store.json" { |
| 36 | + t.Fatalf("StoreFile = %q, want parsed path", cfg.StoreFile) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func TestBuildConfigRejectsUnexpectedArguments(t *testing.T) { |
| 41 | + _, err := buildConfig([]string{"unexpected"}) |
| 42 | + if err == nil || !strings.Contains(err.Error(), "unexpected positional arguments") { |
| 43 | + t.Fatalf("buildConfig error = %v, want unexpected positional arguments error", err) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func TestNewStoreFromConfigUsesMemoryStoreByDefault(t *testing.T) { |
| 48 | + repository, err := newStoreFromConfig(config{}) |
| 49 | + if err != nil { |
| 50 | + t.Fatalf("newStoreFromConfig returned error: %v", err) |
| 51 | + } |
| 52 | + if _, ok := repository.(*store.Store); !ok { |
| 53 | + t.Fatalf("repository type = %T, want *store.Store", repository) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestNewStoreFromConfigUsesFileStore(t *testing.T) { |
| 58 | + path := filepath.Join(t.TempDir(), "edge-store.json") |
| 59 | + |
| 60 | + repository, err := newStoreFromConfig(config{StoreFile: path}) |
| 61 | + if err != nil { |
| 62 | + t.Fatalf("newStoreFromConfig returned error: %v", err) |
| 63 | + } |
| 64 | + fileStore, ok := repository.(*store.FileStore) |
| 65 | + if !ok { |
| 66 | + t.Fatalf("repository type = %T, want *store.FileStore", repository) |
| 67 | + } |
| 68 | + |
| 69 | + fileStore.CreateProject("proj_test", "Test Project") |
| 70 | + if _, err := os.Stat(path); err != nil { |
| 71 | + t.Fatalf("store file was not written: %v", err) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestNewStoreFromConfigReturnsFileStoreErrors(t *testing.T) { |
| 76 | + path := filepath.Join(t.TempDir(), "edge-store.json") |
| 77 | + if err := os.WriteFile(path, []byte("{not json"), 0o644); err != nil { |
| 78 | + t.Fatalf("WriteFile returned error: %v", err) |
| 79 | + } |
| 80 | + |
| 81 | + _, err := newStoreFromConfig(config{StoreFile: path}) |
| 82 | + if err == nil { |
| 83 | + t.Fatal("newStoreFromConfig returned nil error for invalid file store") |
| 84 | + } |
| 85 | + if !strings.Contains(err.Error(), "open store file") || !strings.Contains(err.Error(), "decode store snapshot") { |
| 86 | + t.Fatalf("newStoreFromConfig error = %v, want clear store file decode error", err) |
| 87 | + } |
| 88 | +} |
0 commit comments