11import { assert , beforeEach , describe , it } from "@effect/vitest" ;
22import { vi } from "vite-plus/test" ;
33
4- const { createFromBitmap } = vi . hoisted ( ( ) => ( {
5- createFromBitmap : vi . fn ( ( ) => ( { kind : "unread-completion-overlay" } ) ) ,
4+ const { createFromBuffer, overlayImage } = vi . hoisted ( ( ) => ( {
5+ createFromBuffer : vi . fn ( ) ,
6+ overlayImage : { isEmpty : vi . fn ( ( ) => false ) } ,
67} ) ) ;
78
89vi . mock ( "electron" , ( ) => ( {
9- nativeImage : { createFromBitmap } ,
10+ nativeImage : { createFromBuffer } ,
1011} ) ) ;
1112
1213import { setWindowsTaskbarUnreadIndicator } from "./WindowsTaskbarBadge.ts" ;
@@ -20,35 +21,119 @@ function makeWindow(destroyed = false) {
2021
2122describe ( "setWindowsTaskbarUnreadIndicator" , ( ) => {
2223 beforeEach ( ( ) => {
23- createFromBitmap . mockClear ( ) ;
24+ createFromBuffer . mockReset ( ) ;
25+ createFromBuffer . mockReturnValue ( overlayImage ) ;
26+ overlayImage . isEmpty . mockReset ( ) ;
27+ overlayImage . isEmpty . mockReturnValue ( false ) ;
2428 } ) ;
2529
2630 it ( "sets and clears the Windows taskbar overlay" , ( ) => {
2731 const window = makeWindow ( ) ;
32+ const badgeDataUrl = "data:image/png;base64,one" ;
2833
29- assert . isTrue ( setWindowsTaskbarUnreadIndicator ( { platform : "win32" , window, visible : true } ) ) ;
30- assert . deepEqual ( window . setOverlayIcon . mock . calls , [
31- [ { kind : "unread-completion-overlay" } , "Completed thread awaiting review" ] ,
34+ assert . isTrue (
35+ setWindowsTaskbarUnreadIndicator ( { platform : "win32" , window, count : 1 , badgeDataUrl } ) ,
36+ ) ;
37+ assert . deepEqual ( createFromBuffer . mock . calls , [
38+ [ Buffer . from ( "one" , "base64" ) , { width : 64 , height : 64 , scaleFactor : 4 } ] ,
39+ ] ) ;
40+ assert . deepEqual ( window . setOverlayIcon . mock . calls [ 0 ] , [
41+ overlayImage ,
42+ "1 completed thread awaiting review" ,
3243 ] ) ;
3344
34- assert . isTrue ( setWindowsTaskbarUnreadIndicator ( { platform : "win32" , window, visible : false } ) ) ;
45+ assert . isTrue (
46+ setWindowsTaskbarUnreadIndicator ( {
47+ platform : "win32" ,
48+ window,
49+ count : 0 ,
50+ badgeDataUrl : null ,
51+ } ) ,
52+ ) ;
3553 assert . deepEqual ( window . setOverlayIcon . mock . calls [ 1 ] , [ null , "" ] ) ;
3654 } ) ;
3755
56+ it ( "caches decoded overlays" , ( ) => {
57+ const window = makeWindow ( ) ;
58+ const badgeDataUrl = "data:image/png;base64,two" ;
59+
60+ assert . isTrue (
61+ setWindowsTaskbarUnreadIndicator ( { platform : "win32" , window, count : 2 , badgeDataUrl } ) ,
62+ ) ;
63+ assert . isTrue (
64+ setWindowsTaskbarUnreadIndicator ( { platform : "win32" , window, count : 2 , badgeDataUrl } ) ,
65+ ) ;
66+
67+ assert . equal ( createFromBuffer . mock . calls . length , 1 ) ;
68+ } ) ;
69+
70+ it ( "rejects missing, malformed, and empty badge images" , ( ) => {
71+ const window = makeWindow ( ) ;
72+
73+ assert . isFalse (
74+ setWindowsTaskbarUnreadIndicator ( {
75+ platform : "win32" ,
76+ window,
77+ count : 1 ,
78+ badgeDataUrl : null ,
79+ } ) ,
80+ ) ;
81+ assert . isFalse (
82+ setWindowsTaskbarUnreadIndicator ( {
83+ platform : "win32" ,
84+ window,
85+ count : 1 ,
86+ badgeDataUrl : "data:image/svg+xml;base64,badge" ,
87+ } ) ,
88+ ) ;
89+
90+ overlayImage . isEmpty . mockReturnValueOnce ( true ) ;
91+ assert . isFalse (
92+ setWindowsTaskbarUnreadIndicator ( {
93+ platform : "win32" ,
94+ window,
95+ count : 1 ,
96+ badgeDataUrl : "data:image/png;base64,empty" ,
97+ } ) ,
98+ ) ;
99+ assert . lengthOf ( window . setOverlayIcon . mock . calls , 0 ) ;
100+ } ) ;
101+
38102 it ( "does nothing outside Windows or without a live window" , ( ) => {
39103 const window = makeWindow ( ) ;
40104 const destroyedWindow = makeWindow ( true ) ;
105+ const badgeDataUrl = "data:image/png;base64,platform" ;
41106
42- assert . isFalse ( setWindowsTaskbarUnreadIndicator ( { platform : "darwin" , window, visible : true } ) ) ;
43- assert . isFalse ( setWindowsTaskbarUnreadIndicator ( { platform : "linux" , window, visible : true } ) ) ;
44107 assert . isFalse (
45- setWindowsTaskbarUnreadIndicator ( { platform : "win32" , window : null , visible : true } ) ,
108+ setWindowsTaskbarUnreadIndicator ( {
109+ platform : "darwin" ,
110+ window,
111+ count : 1 ,
112+ badgeDataUrl,
113+ } ) ,
114+ ) ;
115+ assert . isFalse (
116+ setWindowsTaskbarUnreadIndicator ( {
117+ platform : "linux" ,
118+ window,
119+ count : 1 ,
120+ badgeDataUrl,
121+ } ) ,
122+ ) ;
123+ assert . isFalse (
124+ setWindowsTaskbarUnreadIndicator ( {
125+ platform : "win32" ,
126+ window : null ,
127+ count : 1 ,
128+ badgeDataUrl,
129+ } ) ,
46130 ) ;
47131 assert . isFalse (
48132 setWindowsTaskbarUnreadIndicator ( {
49133 platform : "win32" ,
50134 window : destroyedWindow ,
51- visible : true ,
135+ count : 1 ,
136+ badgeDataUrl,
52137 } ) ,
53138 ) ;
54139 assert . lengthOf ( window . setOverlayIcon . mock . calls , 0 ) ;
@@ -61,6 +146,13 @@ describe("setWindowsTaskbarUnreadIndicator", () => {
61146 throw new Error ( "overlay failed" ) ;
62147 } ) ;
63148
64- assert . isFalse ( setWindowsTaskbarUnreadIndicator ( { platform : "win32" , window, visible : true } ) ) ;
149+ assert . isFalse (
150+ setWindowsTaskbarUnreadIndicator ( {
151+ platform : "win32" ,
152+ window,
153+ count : 1 ,
154+ badgeDataUrl : "data:image/png;base64,failure" ,
155+ } ) ,
156+ ) ;
65157 } ) ;
66158} ) ;
0 commit comments