|
| 1 | +package layout |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +// realManifest mirrors the structure produced by Vite: one entry point with |
| 8 | +// CSS, several non-entry chunks (dynamic imports, vendor splits, etc.). |
| 9 | +var realManifest = []byte(`{ |
| 10 | + "__commonjsHelpers-CLPN-Npl.js": { |
| 11 | + "file": "assets/_commonjsHelpers-CLPN-Npl.js", |
| 12 | + "name": "_commonjsHelpers" |
| 13 | + }, |
| 14 | + "_index-BmL2jti6.js": { |
| 15 | + "file": "assets/index-BmL2jti6.js", |
| 16 | + "name": "index" |
| 17 | + }, |
| 18 | + "_index-bUj5cbfw.js": { |
| 19 | + "file": "assets/index-bUj5cbfw.js", |
| 20 | + "name": "index", |
| 21 | + "isDynamicEntry": true, |
| 22 | + "imports": ["_index-BmL2jti6.js"] |
| 23 | + }, |
| 24 | + "node_modules/.pnpm/chart.js@4.5.1/node_modules/chart.js/dist/chart.js": { |
| 25 | + "file": "assets/chart-BjMzFfek.js", |
| 26 | + "name": "chart", |
| 27 | + "isDynamicEntry": true |
| 28 | + }, |
| 29 | + "src/main.ts": { |
| 30 | + "file": "assets/main-CxdDuL5j.js", |
| 31 | + "name": "main", |
| 32 | + "src": "src/main.ts", |
| 33 | + "isEntry": true, |
| 34 | + "dynamicImports": ["node_modules/.pnpm/chart.js@4.5.1/node_modules/chart.js/dist/chart.js"], |
| 35 | + "css": ["assets/main-QTw0RKdw.css"] |
| 36 | + } |
| 37 | +}`) |
| 38 | + |
| 39 | +func TestNewManifest_ParsesEntryPoint(t *testing.T) { |
| 40 | + manifest, err := NewManifest(realManifest) |
| 41 | + if err != nil { |
| 42 | + t.Fatalf("unexpected error: %v", err) |
| 43 | + } |
| 44 | + |
| 45 | + if len(manifest.JS) != 1 || manifest.JS[0] != "/assets/main-CxdDuL5j.js" { |
| 46 | + t.Errorf("JS = %v, want [/assets/main-CxdDuL5j.js]", manifest.JS) |
| 47 | + } |
| 48 | + |
| 49 | + if len(manifest.CSS) != 1 || manifest.CSS[0] != "/assets/main-QTw0RKdw.css" { |
| 50 | + t.Errorf("CSS = %v, want [/assets/main-QTw0RKdw.css]", manifest.CSS) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestNewManifest_SkipsNonEntryChunks(t *testing.T) { |
| 55 | + manifest, err := NewManifest(realManifest) |
| 56 | + if err != nil { |
| 57 | + t.Fatalf("unexpected error: %v", err) |
| 58 | + } |
| 59 | + |
| 60 | + if len(manifest.JS) != 1 { |
| 61 | + t.Errorf("expected 1 JS entry, got %d: %v", len(manifest.JS), manifest.JS) |
| 62 | + } |
| 63 | + |
| 64 | + if len(manifest.CSS) != 1 { |
| 65 | + t.Errorf("expected 1 CSS entry, got %d: %v", len(manifest.CSS), manifest.CSS) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func TestNewManifest_EmptyManifest(t *testing.T) { |
| 70 | + manifest, err := NewManifest([]byte(`{}`)) |
| 71 | + if err != nil { |
| 72 | + t.Fatalf("unexpected error: %v", err) |
| 73 | + } |
| 74 | + |
| 75 | + if len(manifest.JS) != 0 || len(manifest.CSS) != 0 { |
| 76 | + t.Errorf("expected empty manifest, got JS=%v CSS=%v", manifest.JS, manifest.CSS) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func TestNewManifest_InvalidJSON(t *testing.T) { |
| 81 | + _, err := NewManifest([]byte(`not json`)) |
| 82 | + if err == nil { |
| 83 | + t.Error("expected error for invalid JSON, got nil") |
| 84 | + } |
| 85 | +} |
0 commit comments