1+ import { describe , it , expect , vi , beforeEach } from 'vitest' ;
2+ import { ArtoolkitPlugin } from '../src/plugin.js' ;
3+ import { createEventBus } from './setupTests' ;
4+
5+ describe ( 'ArtoolkitPlugin (more coverage)' , ( ) => {
6+ let core : { eventBus : ReturnType < typeof createEventBus > } ;
7+
8+ beforeEach ( ( ) => {
9+ core = { eventBus : createEventBus ( ) } ;
10+ } ) ;
11+
12+ it ( 'disable() removes handlers and terminates worker' , async ( ) => {
13+ const plugin = new ArtoolkitPlugin ( { worker : true } ) ;
14+ await plugin . init ( core ) ;
15+
16+ // Fake a browser worker with spies
17+ const addEventListener = vi . fn ( ) ;
18+ const removeEventListener = vi . fn ( ) ;
19+ const terminate = vi . fn ( ) ;
20+ // @ts -ignore
21+ plugin . _worker = { addEventListener, removeEventListener, terminate, postMessage : vi . fn ( ) } ;
22+
23+ await plugin . enable ( ) ;
24+ // Simulate that we added a message listener during start
25+ expect ( typeof plugin . enabled ) . toBe ( 'boolean' ) ;
26+
27+ await plugin . disable ( ) ;
28+
29+ expect ( removeEventListener ) . toHaveBeenCalledWith ( 'message' , expect . any ( Function ) ) ;
30+ expect ( terminate ) . toHaveBeenCalledTimes ( 1 ) ;
31+ } ) ;
32+
33+ it ( 'engine:update falls back when postMessage throws' , async ( ) => {
34+ const plugin = new ArtoolkitPlugin ( { worker : true } ) ;
35+ await plugin . init ( core ) ;
36+
37+ const postMessage = vi . fn ( ( ) => { throw new Error ( 'boom' ) ; } ) ;
38+ // @ts -ignore
39+ plugin . _worker = { postMessage } ;
40+
41+ // No throw should propagate
42+ // @ts -ignore call private
43+ plugin . _onEngineUpdate ( { id : 99 , imageBitmap : { } as ImageBitmap , width : 2 , height : 2 } ) ;
44+
45+ // Fallback tries a second post without ImageBitmap, so we expect at least one call
46+ expect ( postMessage ) . toHaveBeenCalled ( ) ;
47+ } ) ;
48+
49+ it ( 'getMarkerState returns null when marker not tracked' , async ( ) => {
50+ const plugin = new ArtoolkitPlugin ( { worker : false } ) ;
51+ await plugin . init ( core ) ;
52+ expect ( plugin . getMarkerState ( 12345 ) ) . toBeNull ( ) ;
53+ } ) ;
54+
55+ it ( 'detectionResult with no detections is safely ignored' , async ( ) => {
56+ const plugin = new ArtoolkitPlugin ( { worker : false } ) ;
57+ await plugin . init ( core ) ;
58+ await plugin . enable ( ) ;
59+
60+ // @ts -ignore
61+ plugin . _onWorkerMessage ( { data : { type : 'detectionResult' , payload : { } } } ) ;
62+
63+ // No exception; no markers added
64+ expect ( plugin . getMarkerState ( 1 ) ) . toBeNull ( ) ;
65+ } ) ;
66+ } ) ;
0 commit comments