package system import ( "context" "os" "path/filepath" "testing" ) func TestResolveLocalOssPathFromProjectRoot(t *testing.T) { tempDir := t.TempDir() relativePath := filepath.FromSlash("resource/upload/000000/user/file/test.xlsx") realPath := filepath.Join(tempDir, "server", relativePath) if err := os.MkdirAll(filepath.Dir(realPath), 0o755); err != nil { t.Fatalf("mkdir test oss path: %v", err) } if err := os.WriteFile(realPath, []byte("xlsx"), 0o644); err != nil { t.Fatalf("write test oss file: %v", err) } oldWd, err := os.Getwd() if err != nil { t.Fatalf("get wd: %v", err) } if err = os.Chdir(tempDir); err != nil { t.Fatalf("chdir temp root: %v", err) } defer func() { if err := os.Chdir(oldWd); err != nil { t.Fatalf("restore wd: %v", err) } }() got, err := NewSysOss().resolveLocalOssPath(context.Background(), relativePath) if err != nil { t.Fatalf("resolve local oss path: %v", err) } if got != realPath { t.Fatalf("resolved path = %q, want %q", got, realPath) } } func TestAppOssDownloadContentTypeXlsx(t *testing.T) { got := appOssDownloadContentType("resource/upload/test.xlsx", "xlsx") if got != "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" { t.Fatalf("content type = %q", got) } } func TestLocalFileURLByMappingWithProductionUploadPath(t *testing.T) { filePath := "/data/app/iotlcmadmin-web-svc/upload/000000/user/file/20260710/sim-card-renew/result/REN-result.xlsx" configValue := "/data/app/iotlcmadmin-web-svc/upload|http://iotlcm.example.com:8008/upload" got, ok := localFileURLByMapping(filePath, configValue) if !ok { t.Fatal("expected production upload path mapping to match") } want := "http://iotlcm.example.com:8008/upload/000000/user/file/20260710/sim-card-renew/result/REN-result.xlsx" if got != want { t.Fatalf("url = %q, want %q", got, want) } } func TestLocalFileURLByBaseWithProductionUploadPath(t *testing.T) { filePath := "/data/app/iotlcmadmin-web-svc/upload/000000/user/file/20260710/sim-card-renew/import/REN-import.xlsx" got, ok := localFileURLByBase(filePath, "/data/app/iotlcmadmin-web-svc/upload", "http://iotlcm.example.com:8008/upload") if !ok { t.Fatal("expected upload path base mapping to match") } want := "http://iotlcm.example.com:8008/upload/000000/user/file/20260710/sim-card-renew/import/REN-import.xlsx" if got != want { t.Fatalf("url = %q, want %q", got, want) } } func TestLocalFileURLByMappingWithDevelopmentUploadPath(t *testing.T) { got, ok := localFileURLByMapping( filepath.FromSlash("resource/upload/000000/user/file/test.xlsx"), "resource/upload|/upload", ) if !ok { t.Fatal("expected development upload path mapping to match") } if got != "/upload/000000/user/file/test.xlsx" { t.Fatalf("url = %q", got) } }