1package authoring23import (4 "context"5 "os"6 "path/filepath"7 "testing"89 "github.com/FelineStateMachine/atlas/format/vnext"10 "github.com/FelineStateMachine/atlas/internal/minting"11)1213func TestNativeMinterPersistsOneRecipeAndReturnsTheInstalledVolume(t *testing.T) {14 root := t.TempDir()15 minter, err := NewNativeMinter(NativeMinterOptions{16 RecipesDir: filepath.Join(root, "recipes"),17 CacheDir: filepath.Join(root, "cache"),18 LibraryDir: filepath.Join(root, "library"),19 })20 if err != nil {21 t.Fatal(err)22 }23 var built BuildOptions24 minter.build = func(_ context.Context, options BuildOptions) (BuildResult, error) {25 built = options26 if options.Event != nil {27 options.Event(Event{Stage: "publish", Message: "native build installed", Artifact: "/library/my-region.atlas"})28 }29 return BuildResult{Descriptor: vnext.Descriptor{Slug: "my-region", Title: "My Region", Stamp: "abc123"}}, nil30 }31 request := minting.Request{32 Title: "My Region", Bounds: [4]float64{-105.1, 39.6, -104.9, 39.8}, DetailZoom: 12,33 Topo: true, Roads: true, Hydro: true, Counties: true,34 }35 var events []minting.Event36 result, err := minter.Mint(context.Background(), request, func(event minting.Event) { events = append(events, event) })37 if err != nil {38 t.Fatal(err)39 }40 if result.Slug != "my-region" || result.World != "my-region" || result.Title != "My Region" || result.Stamp != "abc123" {41 t.Fatalf("mint result = %+v", result)42 }43 if built.CacheDir != filepath.Join(root, "cache") || built.LibraryDir != filepath.Join(root, "library") || built.ProjectPath != filepath.Join(root, "recipes", "my-region"+ProjectExtension) {44 t.Fatalf("build options = %+v", built)45 }46 project, err := LoadProject(built.ProjectPath)47 if err != nil {48 t.Fatalf("load managed recipe: %v", err)49 }50 if project.ID != "my-region" || project.Title != "My Region" {51 t.Fatalf("managed recipe identity = %q / %q", project.ID, project.Title)52 }53 entries, err := os.ReadDir(filepath.Join(root, "recipes"))54 if err != nil {55 t.Fatal(err)56 }57 if len(entries) != 1 || entries[0].Name() != "my-region"+ProjectExtension {58 t.Fatalf("managed recipes = %v", entries)59 }60 if len(events) != 1 || events[0].Stage != "publish" || events[0].Artifact == "" {61 t.Fatalf("mint events = %+v", events)62 }63}6465func TestNativeMinterPreviewUsesTheSameRecipeAsMint(t *testing.T) {66 minter, err := NewNativeMinter(NativeMinterOptions{67 RecipesDir: filepath.Join(t.TempDir(), "recipes"), CacheDir: t.TempDir(), LibraryDir: t.TempDir(),68 })69 if err != nil {70 t.Fatal(err)71 }72 request := minter.Default()73 request.Title = "A Map"74 preview, err := minter.Preview(request)75 if err != nil {76 t.Fatal(err)77 }78 if preview.Pixels < 1 || preview.RasterTiles < 1 || preview.Requests != 7 || preview.EstimatedBytes < 1 {79 t.Fatalf("preview = %+v", preview)80 }81}8283func TestNativeMinterPublishesARevisionPastEveryExistingBuild(t *testing.T) {84 descriptors := []vnext.Descriptor{85 {Slug: "my-map", Revision: 2},86 {Slug: "another-map", Revision: 99},87 {Slug: "my-map", Revision: 7},88 }89 if got := nextMintRevision(descriptors, "my-map"); got != 8 {90 t.Fatalf("next revision = %d, want 8", got)91 }92 if got := nextMintRevision(descriptors, "new-map"); got != 1 {93 t.Fatalf("new revision = %d, want 1", got)94 }95}