package api import ( "net/http" "net/http/httptest" "strings" "testing" "github.com/go-chi/chi/v5" "github.com/kindredsystems/silo/internal/db" ) func newODSRouter(s *Server) http.Handler { r := chi.NewRouter() r.Get("/api/items/export.ods", s.HandleExportODS) r.Get("/api/items/template.ods", s.HandleODSTemplate) r.Post("/api/items/import.ods", s.HandleImportODS) r.Get("/api/projects/{code}/sheet.ods", s.HandleProjectSheetODS) return r } func TestHandleExportODS(t *testing.T) { s := newTestServerWithSchemas(t) router := newODSRouter(s) createItemDirect(t, s, "ODS-001", "ods export item", nil) req := httptest.NewRequest("GET", "/api/items/export.ods", nil) w := httptest.NewRecorder() router.ServeHTTP(w, req) if w.Code != http.StatusOK { t.Fatalf("status: got %d, want %d; body: %s", w.Code, http.StatusOK, w.Body.String()) } ct := w.Header().Get("Content-Type") if !strings.Contains(ct, "application/vnd.oasis.opendocument.spreadsheet") { t.Errorf("content-type: got %q, want ODS type", ct) } // ODS is a ZIP file — first 2 bytes should be PK body := w.Body.Bytes() if len(body) < 2 || body[0] != 'P' || body[1] != 'K' { t.Error("response body does not start with PK (ZIP magic)") } } func TestHandleODSTemplate(t *testing.T) { s := newTestServerWithSchemas(t) router := newODSRouter(s) req := httptest.NewRequest("GET", "/api/items/template.ods?schema=kindred-rd", nil) w := httptest.NewRecorder() router.ServeHTTP(w, req) if w.Code != http.StatusOK { t.Fatalf("status: got %d, want %d; body: %s", w.Code, http.StatusOK, w.Body.String()) } ct := w.Header().Get("Content-Type") if !strings.Contains(ct, "application/vnd.oasis.opendocument.spreadsheet") { t.Errorf("content-type: got %q, want ODS type", ct) } } func TestHandleProjectSheetODS(t *testing.T) { s := newTestServerWithSchemas(t) router := newODSRouter(s) // Create project and item ctx := httptest.NewRequest("GET", "/", nil).Context() proj := &db.Project{Code: "ODSPR", Name: "ODS Project"} s.projects.Create(ctx, proj) createItemDirect(t, s, "ODSPR-001", "project sheet item", nil) item, _ := s.items.GetByPartNumber(ctx, "ODSPR-001") s.projects.AddItemToProject(ctx, item.ID, proj.ID) req := httptest.NewRequest("GET", "/api/projects/ODSPR/sheet.ods", nil) w := httptest.NewRecorder() router.ServeHTTP(w, req) if w.Code != http.StatusOK { t.Fatalf("status: got %d, want %d; body: %s", w.Code, http.StatusOK, w.Body.String()) } ct := w.Header().Get("Content-Type") if !strings.Contains(ct, "application/vnd.oasis.opendocument.spreadsheet") { t.Errorf("content-type: got %q, want ODS type", ct) } }