Open History to see the last commit
1import { describe, expect, it, vi } from 'vitest'
2import { createApp as createNodeApp } from '../src/create-app.js'
3import { createApp as createEdgeApp } from '../src/edge-create-app.js'
4import { createMemoryEnvironment } from '../src/edge-environment.js'
5import type { AddOn, Framework, Options } from '../src/types.js'
6
7const commandAddOn: AddOn = {
8 id: 'command-approval',
9 name: 'Command approval',
10 description: 'Version-aware command fixture',
11 type: 'add-on',
12 phase: 'add-on',
13 modes: ['file-router'],
14 command: {
15 command: 'npx',
16 args: ['storybook', 'init'],
17 allowBuilds: ['esbuild'],
18 },
19 getFiles: async () => [],
20 getFileContents: async () => '',
21 getDeletedFiles: async () => [],
22}
23
24const framework: Framework = {
25 id: 'version-fixture',
26 name: 'Version fixture',
27 description: 'Version fixture',
28 version: '1.0.0',
29 basePackageJSON: {},
30 optionalPackages: {},
31 supportedModes: {
32 'file-router': {
33 displayName: 'File router',
34 description: '',
35 forceTypescript: true,
36 },
37 },
38 getFiles: async () => [],
39 getFileContents: async () => '',
40 getDeletedFiles: async () => [],
41 getAddOns: () => [commandAddOn],
42}
43
44const options: Options = {
45 projectName: 'version-fixture',
46 targetDir: '/app',
47 framework,
48 mode: 'file-router',
49 typescript: true,
50 tailwind: false,
51 packageManager: 'pnpm',
52 git: false,
53 install: false,
54 intent: false,
55 chosenAddOns: [commandAddOn],
56 addOnOptions: {},
57}
58
59for (const [runtime, createApp] of Object.entries({
60 Node: createNodeApp,
61 edge: createEdgeApp,
62})) {
63 describe(`${runtime} pnpm command planning`, () => {
64 it.each(['9.15.5', '10.1.0', '10.2.0', '11.22.0'])(
65 'uses the selected pnpm %s version without recording a probe command',
66 async (version) => {
67 const { environment, output } = createMemoryEnvironment('/app')
68 const lookup = vi.fn(async () => version)
69 environment.getPackageManagerVersion = lookup
70 await createApp(environment, options)
71 expect(lookup).toHaveBeenCalledExactlyOnceWith('pnpm', '/app')
72 expect(output.commands).toEqual([
73 {
74 command: 'pnpm',
75 args: [
76 ...(['10.2.0', '11.22.0'].includes(version)
77 ? ['--allow-build=esbuild']
78 : []),
79 'dlx',
80 'storybook',
81 'init',
82 ],
83 },
84 ])
85 },
86 )
87
88 it.each(['10.1.0', '10.2.0'])(
89 'applies the same approval boundary to starter commands on pnpm %s',
90 async (version) => {
91 const { environment, output } = createMemoryEnvironment('/app')
92 const lookup = vi.fn(async () => version)
93 environment.getPackageManagerVersion = lookup
94 await createApp(environment, {
95 ...options,
96 chosenAddOns: [],
97 starter: {
98 ...commandAddOn,
99 type: 'starter',
100 framework: framework.id,
101 mode: 'file-router',
102 typescript: true,
103 files: {},
104 deletedFiles: [],
105 },
106 })
107 expect(lookup).toHaveBeenCalledExactlyOnceWith('pnpm', '/app')
108 expect(output.commands).toEqual([
109 {
110 command: 'pnpm',
111 args: [
112 ...(version === '10.2.0' ? ['--allow-build=esbuild'] : []),
113 'dlx',
114 'storybook',
115 'init',
116 ],
117 },
118 ])
119 },
120 )
121
122 it.each(['not-a-version', '10.2.0-', '10.2.0+build warning', '10.2.0-alpha.01', '01.2.0', '10.2.0+', 'rejected'])(
123 'keeps plain dlx when optional version detection is %s',
124 async (result) => {
125 const { environment, output } = createMemoryEnvironment('/app')
126 environment.getPackageManagerVersion = async () => {
127 if (result === 'rejected') throw new Error('Version probe unavailable')
128 return result
129 }
130 await createApp(environment, options)
131 expect(output.commands).toEqual([{
132 command: 'pnpm', args: ['dlx', 'storybook', 'init'],
133 }])
134 },
135 )
136
137 it.each(['10.2.0-rc.1', '10.2.0+build.01', '11.22.0-rc.1+git.sha'])(
138 'keeps approvals for the valid detected version %s',
139 async (version) => {
140 const { environment, output } = createMemoryEnvironment('/app')
141 environment.getPackageManagerVersion = async () => version
142 await createApp(environment, options)
143 expect(output.commands[0].args).toEqual([
144 '--allow-build=esbuild', 'dlx', 'storybook', 'init',
145 ])
146 },
147 )
148
149 it('uses explicit target versions in memory plans without probing the host', async () => {
150 const { environment, output } = createMemoryEnvironment('/app')
151 const lookup = vi.fn(async () => '11.22.0')
152 environment.getPackageManagerVersion = lookup
153 await createApp(environment, options, { packageManagerVersion: '10.1.0' })
154 expect(lookup).not.toHaveBeenCalled()
155 expect(output.commands[0].args).toEqual(['dlx', 'storybook', 'init'])
156 })
157
158 it('does not probe pnpm for projects without command build approvals', async () => {
159 const { environment } = createMemoryEnvironment('/app')
160 const lookup = vi.fn(async () => '11.22.0')
161 environment.getPackageManagerVersion = lookup
162 await createApp(environment, { ...options, chosenAddOns: [] })
163 expect(lookup).not.toHaveBeenCalled()
164 })
165 })
166}