11import { afterEach , describe , expect , it , vi } from "vite-plus/test" ;
2- import { buildReleaseFromTag , detectArch , fetchRelease , parseRelease } from "../routes/index" ;
2+ import { buildReleaseFromTag , detectArch , resolveRelease } from "../routes/index" ;
33
44describe ( "detectArch" , ( ) => {
55 it ( "defaults to x64 when no query param or user-agent" , ( ) => {
@@ -64,62 +64,6 @@ describe("detectArch", () => {
6464 } ) ;
6565} ) ;
6666
67- describe ( "parseRelease" , ( ) => {
68- it ( "parses both x64 and arm64 assets" , ( ) => {
69- const result = parseRelease ( {
70- tag_name : "v0.1.17-alpha.0" ,
71- assets : [
72- {
73- name : "vp-setup-x86_64-pc-windows-msvc.exe" ,
74- browser_download_url :
75- "https://github.com/voidzero-dev/vite-plus/releases/download/v0.1.17-alpha.0/vp-setup-x86_64-pc-windows-msvc.exe" ,
76- } ,
77- {
78- name : "vp-setup-aarch64-pc-windows-msvc.exe" ,
79- browser_download_url :
80- "https://github.com/voidzero-dev/vite-plus/releases/download/v0.1.17-alpha.0/vp-setup-aarch64-pc-windows-msvc.exe" ,
81- } ,
82- ] ,
83- } ) ;
84- expect ( result ) . toEqual ( {
85- tag : "v0.1.17-alpha.0" ,
86- assets : {
87- x64 : "https://github.com/voidzero-dev/vite-plus/releases/download/v0.1.17-alpha.0/vp-setup-x86_64-pc-windows-msvc.exe" ,
88- arm64 :
89- "https://github.com/voidzero-dev/vite-plus/releases/download/v0.1.17-alpha.0/vp-setup-aarch64-pc-windows-msvc.exe" ,
90- } ,
91- } ) ;
92- } ) ;
93-
94- it ( "returns null when no matching assets exist" , ( ) => {
95- const result = parseRelease ( {
96- tag_name : "v1.0.0" ,
97- assets : [
98- {
99- name : "some-other-file.tar.gz" ,
100- browser_download_url : "https://example.com/other.tar.gz" ,
101- } ,
102- ] ,
103- } ) ;
104- expect ( result ) . toBeNull ( ) ;
105- } ) ;
106-
107- it ( "handles release with only x64 asset" , ( ) => {
108- const result = parseRelease ( {
109- tag_name : "v0.1.0" ,
110- assets : [
111- {
112- name : "vp-setup-x86_64-pc-windows-msvc.exe" ,
113- browser_download_url : "https://example.com/x64.exe" ,
114- } ,
115- ] ,
116- } ) ;
117- expect ( result ) . not . toBeNull ( ) ;
118- expect ( result ! . assets . x64 ) . toBe ( "https://example.com/x64.exe" ) ;
119- expect ( result ! . assets . arm64 ) . toBeUndefined ( ) ;
120- } ) ;
121- } ) ;
122-
12367describe ( "buildReleaseFromTag" , ( ) => {
12468 it ( "constructs download URLs from a tag" , ( ) => {
12569 const result = buildReleaseFromTag ( "v0.1.17-alpha.0" ) ;
@@ -134,130 +78,56 @@ describe("buildReleaseFromTag", () => {
13478 } ) ;
13579} ) ;
13680
137- describe ( "fetchRelease " , ( ) => {
81+ describe ( "resolveRelease " , ( ) => {
13882 afterEach ( ( ) => vi . unstubAllGlobals ( ) ) ;
13983
140- it ( 'returns "not-found" when GitHub returns 404 for a tag' , async ( ) => {
141- vi . stubGlobal (
142- "fetch" ,
143- vi
144- . fn ( )
145- . mockResolvedValue ( new Response ( "Not Found" , { status : 404 , statusText : "Not Found" } ) ) ,
146- ) ;
147- const result = await fetchRelease ( "not-exists" , undefined ) ;
148- expect ( result ) . toBe ( "not-found" ) ;
149- } ) ;
150-
151- it ( "returns null when GitHub returns 403 (rate limited) for a tag" , async ( ) => {
152- vi . stubGlobal (
153- "fetch" ,
154- vi
155- . fn ( )
156- . mockResolvedValue ( new Response ( "Forbidden" , { status : 403 , statusText : "Forbidden" } ) ) ,
157- ) ;
158- const result = await fetchRelease ( "v1.0.0" , undefined ) ;
159- expect ( result ) . toBeNull ( ) ;
160- } ) ;
84+ it ( "returns a constructed release for a specific tag without any network call" , async ( ) => {
85+ const fetchMock = vi . fn ( ) ;
86+ vi . stubGlobal ( "fetch" , fetchMock ) ;
16187
162- it ( "returns null when GitHub returns 500 for a tag" , async ( ) => {
163- vi . stubGlobal (
164- "fetch" ,
165- vi . fn ( ) . mockResolvedValue (
166- new Response ( "Internal Server Error" , {
167- status : 500 ,
168- statusText : "Internal Server Error" ,
169- } ) ,
170- ) ,
171- ) ;
172- const result = await fetchRelease ( "v1.0.0" , undefined ) ;
173- expect ( result ) . toBeNull ( ) ;
174- } ) ;
88+ const result = await resolveRelease ( "v1.0.0" ) ;
17589
176- it ( "returns the release when GitHub returns 200 for a tag" , async ( ) => {
177- const release = {
178- tag_name : "v1.0.0" ,
179- assets : [
180- {
181- name : "vp-setup-x86_64-pc-windows-msvc.exe" ,
182- browser_download_url : "https://example.com/x64.exe" ,
183- } ,
184- ] ,
185- } ;
186- vi . stubGlobal (
187- "fetch" ,
188- vi . fn ( ) . mockResolvedValue ( new Response ( JSON . stringify ( release ) , { status : 200 } ) ) ,
189- ) ;
190- const result = await fetchRelease ( "v1.0.0" , undefined ) ;
191- expect ( result ) . toEqual ( release ) ;
90+ expect ( result ) . toEqual ( buildReleaseFromTag ( "v1.0.0" ) ) ;
91+ expect ( fetchMock ) . not . toHaveBeenCalled ( ) ;
19292 } ) ;
19393
194- it ( "default path: resolves via npm latest dist-tag then fetches that GitHub tag" , async ( ) => {
195- const release = {
196- tag_name : "v0.1.17" ,
197- assets : [
198- {
199- name : "vp-setup-x86_64-pc-windows-msvc.exe" ,
200- browser_download_url :
201- "https://github.com/voidzero-dev/vite-plus/releases/download/v0.1.17/vp-setup-x86_64-pc-windows-msvc.exe" ,
202- } ,
203- ] ,
204- } ;
94+ it ( "resolves latest version via npm and returns a constructed release" , async ( ) => {
20595 const fetchMock = vi
20696 . fn ( )
20797 . mockResolvedValueOnce (
20898 new Response ( JSON . stringify ( { "dist-tags" : { latest : "0.1.17" } } ) , { status : 200 } ) ,
209- )
210- . mockResolvedValueOnce ( new Response ( JSON . stringify ( release ) , { status : 200 } ) ) ;
99+ ) ;
211100 vi . stubGlobal ( "fetch" , fetchMock ) ;
212101
213- const result = await fetchRelease ( undefined , undefined ) ;
102+ const result = await resolveRelease ( undefined ) ;
214103
215- expect ( result ) . toEqual ( release ) ;
216- expect ( fetchMock ) . toHaveBeenCalledTimes ( 2 ) ;
217- const secondCallUrl = fetchMock . mock . calls [ 1 ] [ 0 ] ;
218- expect ( secondCallUrl ) . toContain ( "/releases/tags/v0.1.17" ) ;
104+ expect ( result ) . toEqual ( buildReleaseFromTag ( "v0.1.17" ) ) ;
105+ expect ( fetchMock ) . toHaveBeenCalledTimes ( 1 ) ;
219106 } ) ;
220107
221- it ( "default path: returns null and skips GitHub when npm registry fails " , async ( ) => {
108+ it ( "returns null when npm registry is unreachable " , async ( ) => {
222109 const fetchMock = vi
223110 . fn ( )
224111 . mockResolvedValueOnce (
225112 new Response ( "Service Unavailable" , { status : 503 , statusText : "Service Unavailable" } ) ,
226113 ) ;
227114 vi . stubGlobal ( "fetch" , fetchMock ) ;
228115
229- const result = await fetchRelease ( undefined , undefined ) ;
116+ const result = await resolveRelease ( undefined ) ;
230117
231118 expect ( result ) . toBeNull ( ) ;
232- expect ( fetchMock ) . toHaveBeenCalledTimes ( 1 ) ;
233119 } ) ;
234120
235- it ( "default path: returns null and skips GitHub when npm has no latest dist-tag" , async ( ) => {
121+ it ( "returns null when npm has no latest dist-tag" , async ( ) => {
236122 const fetchMock = vi
237123 . fn ( )
238124 . mockResolvedValueOnce (
239125 new Response ( JSON . stringify ( { "dist-tags" : { alpha : "0.1.17-alpha.5" } } ) , { status : 200 } ) ,
240126 ) ;
241127 vi . stubGlobal ( "fetch" , fetchMock ) ;
242128
243- const result = await fetchRelease ( undefined , undefined ) ;
244-
245- expect ( result ) . toBeNull ( ) ;
246- expect ( fetchMock ) . toHaveBeenCalledTimes ( 1 ) ;
247- } ) ;
248-
249- it ( 'default path: returns null (not "not-found") when GitHub 404s the resolved tag' , async ( ) => {
250- const fetchMock = vi
251- . fn ( )
252- . mockResolvedValueOnce (
253- new Response ( JSON . stringify ( { "dist-tags" : { latest : "0.1.17" } } ) , { status : 200 } ) ,
254- )
255- . mockResolvedValueOnce ( new Response ( "Not Found" , { status : 404 , statusText : "Not Found" } ) ) ;
256- vi . stubGlobal ( "fetch" , fetchMock ) ;
257-
258- const result = await fetchRelease ( undefined , undefined ) ;
129+ const result = await resolveRelease ( undefined ) ;
259130
260131 expect ( result ) . toBeNull ( ) ;
261- expect ( fetchMock ) . toHaveBeenCalledTimes ( 2 ) ;
262132 } ) ;
263133} ) ;
0 commit comments