Coverage for src/tests/frontend/test_production_vite.py: 100%

41 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2024-01-01 00:00 +0000

1"""Module for testing the production vite class.""" 

2 

3from pathlib import Path 

4 

5import pytest 

6 

7from kwai_api.frontend.vite import ProductionVite, Vite 

8 

9 

10@pytest.fixture 

11def vite(manifest_path: Path, tmp_path: Path) -> Vite: 

12 """A fixture for vite production.""" 

13 vite = ProductionVite(manifest_path, tmp_path) 

14 vite.init("src/index.ts") 

15 return vite 

16 

17 

18def test_production_vite_scripts(vite: Vite): 

19 """Test the production version of the Vite class for script tags.""" 

20 scripts = vite.get_scripts("http://localhost:8000/apps/portal/") 

21 assert len(scripts) == 1, "There should be one script tag" 

22 assert scripts[0] == "http://localhost:8000/apps/portal/assets/index-533fa9ea.js" 

23 

24 

25def test_production_vite_css(vite: Vite): 

26 """Test the production of the Vite class for css tags.""" 

27 css = vite.get_css("") 

28 assert len(css) == 1, "There should be a css link" 

29 assert css[0] == "assets/index-ace45c09.css" 

30 

31 

32def test_production_vite_preload(vite: Vite): 

33 """Test the production version of the Vite class for preload tags.""" 

34 preload = vite.get_preloads("") 

35 assert len(preload) == 1, "There should be a preload" 

36 assert preload[0] == "assets/vendor-97e41b2b.js" 

37 

38 

39def test_production_vite_get_asset_path(vite: Vite, tmp_path: Path): 

40 """Test getting the path of an asset.""" 

41 dist_path = tmp_path / "dist" 

42 dist_path.mkdir(exist_ok=True) 

43 with open(dist_path / "test_file.txt", "w") as f: 

44 f.write("This is a test file.") 

45 

46 asset_file = vite.get_asset_path(Path("test_file.txt")) 

47 assert asset_file is not None 

48 

49 

50def test_production_vite_get_public_path(vite: Vite, tmp_path: Path): 

51 """Test getting the path of a public file.""" 

52 dist_path = tmp_path / "dist" 

53 dist_path.mkdir(exist_ok=True) 

54 with open(dist_path / "test_file.txt", "w") as f: 

55 f.write("This is a test file.") 

56 

57 asset_file = vite.get_asset_path(Path("test_file.txt")) 

58 assert asset_file is not None 

59 

60 

61def test_production_vite_get_asset_path_with_relative(vite: Vite, tmp_path: Path): 

62 """Test if a relative path fails.""" 

63 dist_path = tmp_path / "dist" 

64 dist_path.mkdir(exist_ok=True) 

65 with open(dist_path / "test_file.txt", "w") as f: 

66 f.write("This is a test file.") 

67 

68 asset_file = vite.get_asset_path(Path("../../test_file.txt")) 

69 assert asset_file is None