1+ /**
2+ * @file This file contains tests for the PresetManager class.
3+ * @module presetManagerTest
4+ */
5+
6+ import { PresetManager } from '../core/PresetManager.js' ;
7+ import { PRESET_REGISTRY } from '../presets/index.js' ;
8+ import { CAMERA_PRESETS } from '../presets/cameraPresets.js' ;
9+
10+ class PresetManagerTest {
11+ constructor ( ) {
12+ this . testResults = document . getElementById ( "testResults" ) ;
13+ this . runTests ( ) ;
14+ }
15+
16+ logResult ( message , passed ) {
17+ const result = `${ passed ? "✅" : "❌" } ${ message } ` ;
18+ if ( this . testResults ) {
19+ const div = document . createElement ( "div" ) ;
20+ div . innerHTML = message ; // Use innerHTML to render h2 tags
21+ div . style . color = passed ? "#4CAF50" : "#f44336" ;
22+ this . testResults . appendChild ( div ) ;
23+ }
24+ }
25+
26+ async runTests ( ) {
27+ this . logResult ( "<h2>Starting PresetManager Tests...</h2>" , true ) ;
28+
29+ await this . testInitialization ( ) ;
30+ await this . testGetPresetNames ( ) ;
31+ await this . testLoadPreset ( ) ;
32+ await this . testCaching ( ) ;
33+
34+ this . logResult ( "<h2>PresetManager Tests Complete</h2>" , true ) ;
35+ }
36+
37+ async testInitialization ( ) {
38+ const presetManager = new PresetManager ( ) ;
39+ this . logResult ( "PresetManager should initialize" , presetManager instanceof PresetManager ) ;
40+ }
41+
42+ async testGetPresetNames ( ) {
43+ const presetManager = new PresetManager ( ) ;
44+
45+ // Test for camera presets
46+ try {
47+ const cameraPresetNames = await presetManager . getPresetNames ( PRESET_REGISTRY . CAMERA ) ;
48+ const expectedCameraPresetNames = Object . keys ( CAMERA_PRESETS ) ;
49+ this . logResult ( "getPresetNames should return correct camera preset names" , JSON . stringify ( cameraPresetNames . sort ( ) ) === JSON . stringify ( expectedCameraPresetNames . sort ( ) ) ) ;
50+ } catch ( e ) {
51+ this . logResult ( `getPresetNames for camera presets failed: ${ e . message } ` , false ) ;
52+ }
53+
54+ // Test for an invalid preset type
55+ try {
56+ await presetManager . getPresetNames ( 'invalid_type' ) ;
57+ this . logResult ( "getPresetNames should throw an error for an invalid type" , false ) ;
58+ } catch ( e ) {
59+ this . logResult ( "getPresetNames should throw an error for an invalid type" , e . message . includes ( 'Unknown preset type' ) ) ;
60+ }
61+ }
62+
63+ async testLoadPreset ( ) {
64+ const presetManager = new PresetManager ( ) ;
65+
66+ // Test loading a valid camera preset
67+ try {
68+ const preset = await presetManager . loadPreset ( PRESET_REGISTRY . CAMERA , 'DEFAULT' ) ;
69+ this . logResult ( "loadPreset should load a valid camera preset" , preset === CAMERA_PRESETS . DEFAULT ) ;
70+ } catch ( e ) {
71+ this . logResult ( `loadPreset for a valid camera preset failed: ${ e . message } ` , false ) ;
72+ }
73+
74+ // Test loading a non-existent preset
75+ try {
76+ await presetManager . loadPreset ( PRESET_REGISTRY . CAMERA , 'NON_EXISTENT' ) ;
77+ this . logResult ( "loadPreset should throw an error for a non-existent preset" , false ) ;
78+ } catch ( e ) {
79+ this . logResult ( "loadPreset should throw an error for a non-existent preset" , e . message . includes ( 'not found for type' ) ) ;
80+ }
81+
82+ // Test loading from an invalid preset type
83+ try {
84+ await presetManager . loadPreset ( 'invalid_type' , 'DEFAULT' ) ;
85+ this . logResult ( "loadPreset should throw an error for an invalid type" , false ) ;
86+ } catch ( e ) {
87+ this . logResult ( "loadPreset should throw an error for an invalid type" , e . message . includes ( 'Unknown preset type' ) ) ;
88+ }
89+ }
90+
91+ async testCaching ( ) {
92+ let callCount = 0 ;
93+ const mockManifest = {
94+ [ PRESET_REGISTRY . CAMERA ] : {
95+ loader : ( ) => {
96+ callCount ++ ;
97+ return Promise . resolve ( {
98+ CAMERA_PRESETS : {
99+ 'DEFAULT' : { "test" : "data" }
100+ }
101+ } ) ;
102+ } ,
103+ exportName : "CAMERA_PRESETS" ,
104+ }
105+ } ;
106+
107+ const presetManager = new PresetManager ( mockManifest ) ;
108+
109+ // Load the same preset twice
110+ await presetManager . loadPreset ( PRESET_REGISTRY . CAMERA , 'DEFAULT' ) ;
111+ await presetManager . loadPreset ( PRESET_REGISTRY . CAMERA , 'DEFAULT' ) ;
112+
113+ this . logResult ( "loadPreset should cache presets and only call loader once" , callCount === 1 ) ;
114+ }
115+ }
116+
117+ window . addEventListener ( "DOMContentLoaded" , ( ) => {
118+ new PresetManagerTest ( ) ;
119+ } ) ;
0 commit comments