1- import { describe , expect , it , vi } from "vitest" ;
1+ import { describe , expect , it , type Mock , vi } from "vitest" ;
22import ExecScript from "../exec_script" ;
33import type { ScriptLoadInfo } from "@App/app/service/service_worker/types" ;
44import type { GMInfoEnv , ScriptFunc } from "../types" ;
55import { compileScript , compileScriptCode } from "../utils" ;
66import type { Message } from "@Packages/message/types" ;
77import { encodeRValue } from "@App/pkg/utils/message_value" ;
88import { v4 as uuidv4 } from "uuid" ;
9+ import { getStorageName } from "@App/pkg/utils/utils" ;
910const nilFn : ScriptFunc = ( ) => { } ;
1011
1112const scriptRes = {
@@ -107,6 +108,40 @@ describe.concurrent("window.*", () => {
107108} ) ;
108109
109110describe . concurrent ( "GM Api" , ( ) => {
111+ const valueDaoUpdatetimeFix = (
112+ mockSendMessage : Mock < ( ...args : any [ ] ) => any > ,
113+ exec : ExecScript ,
114+ script : ScriptLoadInfo
115+ ) => {
116+ const forceUpdateTimeRefreshIdx = mockSendMessage . mock . calls . findIndex ( ( entry ) => {
117+ return entry ?. [ 0 ] ?. data ?. api === "internalApiWaitForFreshValueState" ;
118+ } ) ;
119+ if ( forceUpdateTimeRefreshIdx >= 0 ) {
120+ const actualCall = mockSendMessage . mock . calls [ forceUpdateTimeRefreshIdx ] [ 0 ] ;
121+ expect ( mockSendMessage ) . toHaveBeenNthCalledWith (
122+ forceUpdateTimeRefreshIdx + 1 ,
123+ expect . objectContaining ( {
124+ action : "content/runtime/gmApi" ,
125+ data : {
126+ api : "internalApiWaitForFreshValueState" ,
127+ params : [ expect . stringMatching ( / ^ .+ : : \d + $ / ) ] ,
128+ runFlag : expect . any ( String ) ,
129+ uuid : undefined ,
130+ } ,
131+ } )
132+ ) ;
133+ exec . valueUpdate ( getStorageName ( script ) , script . uuid , [
134+ {
135+ id : actualCall . data . params [ 0 ] ,
136+ entries : [ [ "TEST_NON_EXIST_REMOVAL" , encodeRValue ( undefined ) , encodeRValue ( undefined ) ] ] ,
137+ uuid : script . uuid ,
138+ storageName : getStorageName ( script ) ,
139+ sender : { runFlag : exec . sandboxContext ! . runFlag , tabId : - 2 } ,
140+ updatetime : Date . now ( ) ,
141+ } ,
142+ ] ) ;
143+ }
144+ } ;
110145 it . concurrent ( "GM_getValue" , async ( ) => {
111146 const script = Object . assign ( { } , scriptRes ) as ScriptLoadInfo ;
112147 script . value = { test : "ok" } ;
@@ -123,22 +158,32 @@ describe.concurrent("GM Api", () => {
123158 script . value = { test : "ok" } ;
124159 script . metadata . grant = [ "GM.getValue" ] ;
125160 script . code = `return GM.getValue("test").then(v=>v+"!");` ;
126- // @ts -ignore
127- const exec = new ExecScript ( script , undefined , undefined , nilFn , envInfo ) ;
161+ const mockSendMessage = vi . fn ( ) . mockResolvedValue ( { code : 0 } ) ;
162+ const mockMessage = {
163+ sendMessage : mockSendMessage ,
164+ } as unknown as Message ;
165+ const exec = new ExecScript ( script , "content" , mockMessage , nilFn , envInfo ) ;
128166 exec . scriptFunc = compileScript ( compileScriptCode ( script ) ) ;
129- const ret = await exec . exec ( ) ;
167+ const retPromise = exec . exec ( ) ;
168+ valueDaoUpdatetimeFix ( mockSendMessage , exec , script ) ;
169+ const ret = await retPromise ;
130170 expect ( ret ) . toEqual ( "ok!" ) ;
131171 } ) ;
132172
133173 it . concurrent ( "GM_listValues" , async ( ) => {
134174 const script = Object . assign ( { } , scriptRes ) as ScriptLoadInfo ;
135175 script . value = { test1 : "23" , test2 : "45" , test3 : "67" } ;
136176 script . metadata . grant = [ "GM_listValues" ] ;
137- script . code = `return GM_listValues().join("-");` ;
138- // @ts -ignore
139- const exec = new ExecScript ( script , undefined , undefined , nilFn , envInfo ) ;
177+ script . code = `return GM_listValues.join("-");` ;
178+ const mockSendMessage = vi . fn ( ) . mockResolvedValue ( { code : 0 } ) ;
179+ const mockMessage = {
180+ sendMessage : mockSendMessage ,
181+ } as unknown as Message ;
182+ const exec = new ExecScript ( script , "content" , mockMessage , nilFn , envInfo ) ;
140183 exec . scriptFunc = compileScript ( compileScriptCode ( script ) ) ;
141- const ret = await exec . exec ( ) ;
184+ const retPromise = exec . exec ( ) ;
185+ valueDaoUpdatetimeFix ( mockSendMessage , exec , script ) ;
186+ const ret = await retPromise ;
142187 expect ( ret ) . toEqual ( "test1-test2-test3" ) ;
143188 } ) ;
144189
@@ -150,23 +195,33 @@ describe.concurrent("GM Api", () => {
150195 script . value . test3 = "75" ;
151196 script . value . test1 = "40" ;
152197 script . metadata . grant = [ "GM_listValues" ] ;
153- script . code = `return GM_listValues().join("-");` ;
154- // @ts -ignore
155- const exec = new ExecScript ( script , undefined , undefined , nilFn , envInfo ) ;
198+ script . code = `return GM_listValues.join("-");` ;
199+ const mockSendMessage = vi . fn ( ) . mockResolvedValue ( { code : 0 } ) ;
200+ const mockMessage = {
201+ sendMessage : mockSendMessage ,
202+ } as unknown as Message ;
203+ const exec = new ExecScript ( script , "content" , mockMessage , nilFn , envInfo ) ;
156204 exec . scriptFunc = compileScript ( compileScriptCode ( script ) ) ;
157- const ret = await exec . exec ( ) ;
158- expect ( ret ) . toEqual ( "test5-test2-test3-test1" ) ; // TM也沒有sort
205+ const retPromise = exec . exec ( ) ;
206+ valueDaoUpdatetimeFix ( mockSendMessage , exec , script ) ;
207+ const ret = await retPromise ;
208+ expect ( ret ) . toEqual ( "test5-test2-test3-test1" ) ; // TM也没有sort
159209 } ) ;
160210
161211 it . concurrent ( "GM.listValues" , async ( ) => {
162212 const script = Object . assign ( { } , scriptRes ) as ScriptLoadInfo ;
163213 script . value = { test1 : "23" , test2 : "45" , test3 : "67" } ;
164214 script . metadata . grant = [ "GM.listValues" ] ;
165215 script . code = `return GM.listValues().then(v=>v.join("-"));` ;
166- // @ts -ignore
167- const exec = new ExecScript ( script , undefined , undefined , nilFn , envInfo ) ;
216+ const mockSendMessage = vi . fn ( ) . mockResolvedValue ( { code : 0 } ) ;
217+ const mockMessage = {
218+ sendMessage : mockSendMessage ,
219+ } as unknown as Message ;
220+ const exec = new ExecScript ( script , "content" , mockMessage , nilFn , envInfo ) ;
168221 exec . scriptFunc = compileScript ( compileScriptCode ( script ) ) ;
169- const ret = await exec . exec ( ) ;
222+ const retPromise = exec . exec ( ) ;
223+ valueDaoUpdatetimeFix ( mockSendMessage , exec , script ) ;
224+ const ret = await retPromise ;
170225 expect ( ret ) . toEqual ( "test1-test2-test3" ) ;
171226 } ) ;
172227
@@ -179,11 +234,16 @@ describe.concurrent("GM Api", () => {
179234 script . value . test1 = "40" ;
180235 script . metadata . grant = [ "GM.listValues" ] ;
181236 script . code = `return GM.listValues().then(v=>v.join("-"));` ;
182- // @ts -ignore
183- const exec = new ExecScript ( script , undefined , undefined , nilFn , envInfo ) ;
237+ const mockSendMessage = vi . fn ( ) . mockResolvedValue ( { code : 0 } ) ;
238+ const mockMessage = {
239+ sendMessage : mockSendMessage ,
240+ } as unknown as Message ;
241+ const exec = new ExecScript ( script , "content" , mockMessage , nilFn , envInfo ) ;
184242 exec . scriptFunc = compileScript ( compileScriptCode ( script ) ) ;
185- const ret = await exec . exec ( ) ;
186- expect ( ret ) . toEqual ( "test5-test2-test3-test1" ) ; // TM也沒有sort
243+ const retPromise = exec . exec ( ) ;
244+ valueDaoUpdatetimeFix ( mockSendMessage , exec , script ) ;
245+ const ret = await retPromise ;
246+ expect ( ret ) . toEqual ( "test5-test2-test3-test1" ) ; // TM也没有sort
187247 } ) ;
188248
189249 it . concurrent ( "GM_getValues" , async ( ) => {
@@ -212,10 +272,15 @@ describe.concurrent("GM Api", () => {
212272 script . value = { test1 : "23" , test2 : 45 , test3 : "67" } ;
213273 script . metadata . grant = [ "GM.getValues" ] ;
214274 script . code = `return GM.getValues(["test2", "test3", "test1"]).then(v=>v);` ;
215- // @ts -ignore
216- const exec = new ExecScript ( script , undefined , undefined , nilFn , envInfo ) ;
275+ const mockSendMessage = vi . fn ( ) . mockResolvedValue ( { code : 0 } ) ;
276+ const mockMessage = {
277+ sendMessage : mockSendMessage ,
278+ } as unknown as Message ;
279+ const exec = new ExecScript ( script , "content" , mockMessage , nilFn , envInfo ) ;
217280 exec . scriptFunc = compileScript ( compileScriptCode ( script ) ) ;
218- const ret = await exec . exec ( ) ;
281+ const retPromise = exec . exec ( ) ;
282+ valueDaoUpdatetimeFix ( mockSendMessage , exec , script ) ;
283+ const ret = await retPromise ;
219284 expect ( ret . test1 ) . toEqual ( "23" ) ;
220285 expect ( ret . test2 ) . toEqual ( 45 ) ;
221286 expect ( ret . test3 ) . toEqual ( "67" ) ;
@@ -499,7 +564,7 @@ describe.concurrent("GM_value", () => {
499564 api : "GM_setValues" ,
500565 params : [
501566 // event id
502- expect . stringMatching ( / ^ .+ : : \d $ / ) ,
567+ expect . stringMatching ( / ^ .+ : : \d + $ / ) ,
503568 // the object payload
504569 keyValuePairs1 ,
505570 ] ,
@@ -523,7 +588,7 @@ describe.concurrent("GM_value", () => {
523588 api : "GM_setValues" ,
524589 params : [
525590 // event id
526- expect . stringMatching ( / ^ .+ : : \d $ / ) ,
591+ expect . stringMatching ( / ^ .+ : : \d + $ / ) ,
527592 // the object payload
528593 keyValuePairs2 ,
529594 ] ,
@@ -573,7 +638,7 @@ describe.concurrent("GM_value", () => {
573638 api : "GM_setValues" ,
574639 params : [
575640 // event id
576- expect . stringMatching ( / ^ .+ : : \d $ / ) ,
641+ expect . stringMatching ( / ^ .+ : : \d + $ / ) ,
577642 // the object payload
578643 keyValuePairs1 ,
579644 ] ,
@@ -592,7 +657,7 @@ describe.concurrent("GM_value", () => {
592657 api : "GM_setValue" ,
593658 params : [
594659 // event id
595- expect . stringMatching ( / ^ .+ : : \d $ / ) ,
660+ expect . stringMatching ( / ^ .+ : : \d + $ / ) ,
596661 // the string payload
597662 "b" ,
598663 ] ,
@@ -643,7 +708,7 @@ describe.concurrent("GM_value", () => {
643708 api : "GM_setValues" ,
644709 params : [
645710 // event id
646- expect . stringMatching ( / ^ .+ : : \d $ / ) ,
711+ expect . stringMatching ( / ^ .+ : : \d + $ / ) ,
647712 // the object payload
648713 keyValuePairs1 ,
649714 ] ,
@@ -667,7 +732,7 @@ describe.concurrent("GM_value", () => {
667732 api : "GM_setValues" ,
668733 params : [
669734 // event id
670- expect . stringMatching ( / ^ .+ : : \d $ / ) ,
735+ expect . stringMatching ( / ^ .+ : : \d + $ / ) ,
671736 // the string payload
672737 keyValuePairs2 ,
673738 ] ,
@@ -702,14 +767,16 @@ describe.concurrent("GM_value", () => {
702767 const retPromise = exec . exec ( ) ;
703768 expect ( mockSendMessage ) . toHaveBeenCalledTimes ( 1 ) ;
704769 // 模拟值变化
705- exec . valueUpdate ( {
706- id : "id-1" ,
707- entries : [ [ "param1" , encodeRValue ( 123 ) , encodeRValue ( undefined ) ] ] ,
708- uuid : script . uuid ,
709- storageName : script . uuid ,
710- sender : { runFlag : exec . sandboxContext ! . runFlag , tabId : - 2 } ,
711- valueUpdated : true ,
712- } ) ;
770+ exec . valueUpdate ( getStorageName ( script ) , script . uuid , [
771+ {
772+ id : "id-1" ,
773+ entries : [ [ "param1" , encodeRValue ( 123 ) , encodeRValue ( undefined ) ] ] ,
774+ uuid : script . uuid ,
775+ storageName : script . uuid ,
776+ sender : { runFlag : exec . sandboxContext ! . runFlag , tabId : - 2 } ,
777+ updatetime : Date . now ( ) ,
778+ } ,
779+ ] ) ;
713780 const ret = await retPromise ;
714781 expect ( ret ) . toEqual ( { name : "param1" , oldValue : undefined , newValue : 123 , remote : false } ) ;
715782 } ) ;
@@ -737,14 +804,16 @@ describe.concurrent("GM_value", () => {
737804 const retPromise = exec . exec ( ) ;
738805 expect ( mockSendMessage ) . toHaveBeenCalledTimes ( 1 ) ;
739806 // 模拟值变化
740- exec . valueUpdate ( {
741- id : "id-2" ,
742- entries : [ [ "param2" , encodeRValue ( 456 ) , encodeRValue ( undefined ) ] ] ,
743- uuid : script . uuid ,
744- storageName : "testStorage" ,
745- sender : { runFlag : "user" , tabId : - 2 } ,
746- valueUpdated : true ,
747- } ) ;
807+ exec . valueUpdate ( getStorageName ( script ) , script . uuid , [
808+ {
809+ id : "id-2" ,
810+ entries : [ [ "param2" , encodeRValue ( 456 ) , encodeRValue ( undefined ) ] ] ,
811+ uuid : script . uuid ,
812+ storageName : "testStorage" ,
813+ sender : { runFlag : "user" , tabId : - 2 } ,
814+ updatetime : Date . now ( ) ,
815+ } ,
816+ ] ) ;
748817 const ret2 = await retPromise ;
749818 expect ( ret2 ) . toEqual ( { name : "param2" , oldValue : undefined , newValue : 456 , remote : true } ) ;
750819 } ) ;
@@ -772,14 +841,16 @@ describe.concurrent("GM_value", () => {
772841 expect ( id ) . toBeTypeOf ( "string" ) ;
773842 expect ( id . length ) . greaterThan ( 0 ) ;
774843 // 触发valueUpdate
775- exec . valueUpdate ( {
776- id : id ,
777- entries : [ [ "a" , encodeRValue ( 123 ) , encodeRValue ( undefined ) ] ] ,
778- uuid : script . uuid ,
779- storageName : script . uuid ,
780- sender : { runFlag : exec . sandboxContext ! . runFlag , tabId : - 2 } ,
781- valueUpdated : true ,
782- } ) ;
844+ exec . valueUpdate ( getStorageName ( script ) , script . uuid , [
845+ {
846+ id : id ,
847+ entries : [ [ "a" , encodeRValue ( 123 ) , encodeRValue ( undefined ) ] ] ,
848+ uuid : script . uuid ,
849+ storageName : script . uuid ,
850+ sender : { runFlag : exec . sandboxContext ! . runFlag , tabId : - 2 } ,
851+ updatetime : Date . now ( ) ,
852+ } ,
853+ ] ) ;
783854
784855 const ret = await retPromise ;
785856 expect ( ret ) . toEqual ( 123 ) ;
0 commit comments