package services_test import ( "bytes" "context" "encoding/json" "testing" "github.com/andyleap/hnh-map/internal/app" "github.com/andyleap/hnh-map/internal/app/services" "github.com/andyleap/hnh-map/internal/app/store" "go.etcd.io/bbolt" ) func newTestExportService(t *testing.T) (*services.ExportService, *store.Store) { t.Helper() db := newTestDB(t) st := store.New(db) mapSvc := services.NewMapService(services.MapServiceDeps{ Store: st, GridStorage: t.TempDir(), GridUpdates: &app.Topic[app.TileData]{}, }) return services.NewExportService(st, mapSvc), st } func TestExport_EmptyDB(t *testing.T) { export, _ := newTestExportService(t) var buf bytes.Buffer err := export.Export(context.Background(), &buf) if err != nil { t.Fatal(err) } if buf.Len() == 0 { t.Fatal("expected non-empty zip output") } // ZIP magic number if buf.Len() < 4 || buf.Bytes()[0] != 0x50 || buf.Bytes()[1] != 0x4b { t.Fatal("expected valid zip header") } } func TestExport_WithGrid(t *testing.T) { export, st := newTestExportService(t) ctx := context.Background() gd := app.GridData{ID: "g1", Map: 1, Coord: app.Coord{X: 0, Y: 0}} gdRaw, _ := json.Marshal(gd) mi := app.MapInfo{ID: 1, Name: "test", Hidden: false} miRaw, _ := json.Marshal(mi) st.Update(ctx, func(tx *bbolt.Tx) error { if err := st.PutGrid(tx, "g1", gdRaw); err != nil { return err } return st.PutMap(tx, 1, miRaw) }) var buf bytes.Buffer err := export.Export(ctx, &buf) if err != nil { t.Fatal(err) } if buf.Len() < 4 { t.Fatal("expected zip data") } }