|
| 1 | +import {selectActiveConfig} from './active-config.js' |
| 2 | +import {Project} from './project.js' |
| 3 | +import {describe, expect, test, vi, beforeEach} from 'vitest' |
| 4 | +import {inTemporaryDirectory, writeFile, mkdir} from '@shopify/cli-kit/node/fs' |
| 5 | +import {joinPath, basename} from '@shopify/cli-kit/node/path' |
| 6 | + |
| 7 | +vi.mock('../../services/local-storage.js', () => ({ |
| 8 | + getCachedAppInfo: vi.fn().mockReturnValue(undefined), |
| 9 | + setCachedAppInfo: vi.fn(), |
| 10 | + clearCachedAppInfo: vi.fn(), |
| 11 | +})) |
| 12 | + |
| 13 | +vi.mock('../../services/app/config/use.js', () => ({ |
| 14 | + default: vi.fn(), |
| 15 | +})) |
| 16 | + |
| 17 | +const {getCachedAppInfo} = await import('../../services/local-storage.js') |
| 18 | + |
| 19 | +beforeEach(() => { |
| 20 | + vi.mocked(getCachedAppInfo).mockReturnValue(undefined) |
| 21 | +}) |
| 22 | + |
| 23 | +describe('selectActiveConfig', () => { |
| 24 | + test('selects config by user-provided name (flag)', async () => { |
| 25 | + await inTemporaryDirectory(async (dir) => { |
| 26 | + await writeFile(joinPath(dir, 'shopify.app.toml'), 'client_id = "default"') |
| 27 | + await writeFile(joinPath(dir, 'shopify.app.staging.toml'), 'client_id = "staging"') |
| 28 | + const project = await Project.load(dir) |
| 29 | + |
| 30 | + const config = await selectActiveConfig(project, 'staging') |
| 31 | + |
| 32 | + expect(basename(config.file.path)).toBe('shopify.app.staging.toml') |
| 33 | + expect(config.file.content.client_id).toBe('staging') |
| 34 | + expect(config.source).toBe('flag') |
| 35 | + expect(config.isLinked).toBe(true) |
| 36 | + }) |
| 37 | + }) |
| 38 | + |
| 39 | + test('selects config from cache', async () => { |
| 40 | + await inTemporaryDirectory(async (dir) => { |
| 41 | + await writeFile(joinPath(dir, 'shopify.app.toml'), 'client_id = "default"') |
| 42 | + await writeFile(joinPath(dir, 'shopify.app.production.toml'), 'client_id = "production"') |
| 43 | + const project = await Project.load(dir) |
| 44 | + |
| 45 | + vi.mocked(getCachedAppInfo).mockReturnValue({ |
| 46 | + directory: dir, |
| 47 | + configFile: 'shopify.app.production.toml', |
| 48 | + }) |
| 49 | + |
| 50 | + const config = await selectActiveConfig(project) |
| 51 | + |
| 52 | + expect(basename(config.file.path)).toBe('shopify.app.production.toml') |
| 53 | + expect(config.file.content.client_id).toBe('production') |
| 54 | + expect(config.source).toBe('cached') |
| 55 | + }) |
| 56 | + }) |
| 57 | + |
| 58 | + test('falls back to default shopify.app.toml when no flag or cache', async () => { |
| 59 | + await inTemporaryDirectory(async (dir) => { |
| 60 | + await writeFile(joinPath(dir, 'shopify.app.toml'), 'client_id = "default-id"') |
| 61 | + const project = await Project.load(dir) |
| 62 | + |
| 63 | + const config = await selectActiveConfig(project) |
| 64 | + |
| 65 | + expect(basename(config.file.path)).toBe('shopify.app.toml') |
| 66 | + expect(config.file.content.client_id).toBe('default-id') |
| 67 | + }) |
| 68 | + }) |
| 69 | + |
| 70 | + test('detects isLinked from client_id', async () => { |
| 71 | + await inTemporaryDirectory(async (dir) => { |
| 72 | + await writeFile(joinPath(dir, 'shopify.app.toml'), 'client_id = ""') |
| 73 | + const project = await Project.load(dir) |
| 74 | + |
| 75 | + const config = await selectActiveConfig(project) |
| 76 | + |
| 77 | + expect(config.isLinked).toBe(false) |
| 78 | + }) |
| 79 | + }) |
| 80 | + |
| 81 | + test('detects isLinked when client_id is present', async () => { |
| 82 | + await inTemporaryDirectory(async (dir) => { |
| 83 | + await writeFile(joinPath(dir, 'shopify.app.toml'), 'client_id = "abc123"') |
| 84 | + const project = await Project.load(dir) |
| 85 | + |
| 86 | + const config = await selectActiveConfig(project) |
| 87 | + |
| 88 | + expect(config.isLinked).toBe(true) |
| 89 | + }) |
| 90 | + }) |
| 91 | + |
| 92 | + test('resolves config-specific dotenv', async () => { |
| 93 | + await inTemporaryDirectory(async (dir) => { |
| 94 | + await writeFile(joinPath(dir, 'shopify.app.staging.toml'), 'client_id = "staging"') |
| 95 | + await writeFile(joinPath(dir, 'shopify.app.toml'), 'client_id = "default"') |
| 96 | + await writeFile(joinPath(dir, '.env'), 'KEY=default') |
| 97 | + await writeFile(joinPath(dir, '.env.staging'), 'KEY=staging') |
| 98 | + const project = await Project.load(dir) |
| 99 | + |
| 100 | + const config = await selectActiveConfig(project, 'staging') |
| 101 | + |
| 102 | + expect(config.dotenv).toBeDefined() |
| 103 | + expect(config.dotenv!.variables.KEY).toBe('staging') |
| 104 | + }) |
| 105 | + }) |
| 106 | + |
| 107 | + test('resolves hidden config for client_id', async () => { |
| 108 | + await inTemporaryDirectory(async (dir) => { |
| 109 | + await writeFile(joinPath(dir, 'shopify.app.toml'), 'client_id = "abc123"') |
| 110 | + await mkdir(joinPath(dir, '.shopify')) |
| 111 | + await writeFile( |
| 112 | + joinPath(dir, '.shopify', 'project.json'), |
| 113 | + JSON.stringify({abc123: {dev_store_url: 'test.myshopify.com'}}), |
| 114 | + ) |
| 115 | + const project = await Project.load(dir) |
| 116 | + |
| 117 | + const config = await selectActiveConfig(project) |
| 118 | + |
| 119 | + expect(config.hiddenConfig).toStrictEqual({dev_store_url: 'test.myshopify.com'}) |
| 120 | + }) |
| 121 | + }) |
| 122 | + |
| 123 | + test('returns empty hidden config when no entry for client_id', async () => { |
| 124 | + await inTemporaryDirectory(async (dir) => { |
| 125 | + await writeFile(joinPath(dir, 'shopify.app.toml'), 'client_id = "abc123"') |
| 126 | + const project = await Project.load(dir) |
| 127 | + |
| 128 | + const config = await selectActiveConfig(project) |
| 129 | + |
| 130 | + expect(config.hiddenConfig).toStrictEqual({}) |
| 131 | + }) |
| 132 | + }) |
| 133 | + |
| 134 | + test('file.path is absolute', async () => { |
| 135 | + await inTemporaryDirectory(async (dir) => { |
| 136 | + await writeFile(joinPath(dir, 'shopify.app.toml'), 'client_id = "abc"') |
| 137 | + const project = await Project.load(dir) |
| 138 | + |
| 139 | + const config = await selectActiveConfig(project) |
| 140 | + |
| 141 | + expect(config.file.path).toBe(joinPath(dir, 'shopify.app.toml')) |
| 142 | + }) |
| 143 | + }) |
| 144 | + |
| 145 | + test('accepts full filename as config name', async () => { |
| 146 | + await inTemporaryDirectory(async (dir) => { |
| 147 | + await writeFile(joinPath(dir, 'shopify.app.toml'), 'client_id = "default"') |
| 148 | + await writeFile(joinPath(dir, 'shopify.app.staging.toml'), 'client_id = "staging"') |
| 149 | + const project = await Project.load(dir) |
| 150 | + |
| 151 | + const config = await selectActiveConfig(project, 'shopify.app.staging.toml') |
| 152 | + |
| 153 | + expect(basename(config.file.path)).toBe('shopify.app.staging.toml') |
| 154 | + expect(config.file.content.client_id).toBe('staging') |
| 155 | + }) |
| 156 | + }) |
| 157 | + |
| 158 | + test('throws when requested config does not exist', async () => { |
| 159 | + await inTemporaryDirectory(async (dir) => { |
| 160 | + await writeFile(joinPath(dir, 'shopify.app.toml'), 'client_id = "default"') |
| 161 | + const project = await Project.load(dir) |
| 162 | + |
| 163 | + await expect(selectActiveConfig(project, 'nonexistent')).rejects.toThrow() |
| 164 | + }) |
| 165 | + }) |
| 166 | + |
| 167 | + test('throws when the only app config is malformed (no valid configs to fall back to)', async () => { |
| 168 | + await inTemporaryDirectory(async (dir) => { |
| 169 | + // The only config is broken TOML — Project.load skips it and finds 0 valid configs |
| 170 | + await writeFile(joinPath(dir, 'shopify.app.toml'), '{{invalid toml') |
| 171 | + |
| 172 | + await expect(Project.load(dir)).rejects.toThrow(/Could not find/) |
| 173 | + }) |
| 174 | + }) |
| 175 | + |
| 176 | + test('surfaces parse error when selecting a broken config while a valid one exists', async () => { |
| 177 | + await inTemporaryDirectory(async (dir) => { |
| 178 | + // Two configs: one good, one broken. Selecting the broken one by name should |
| 179 | + // surface the real parse error via the fallback re-read, not a generic "not found". |
| 180 | + await writeFile(joinPath(dir, 'shopify.app.toml'), 'client_id = "good"') |
| 181 | + await writeFile(joinPath(dir, 'shopify.app.broken.toml'), '{{invalid toml') |
| 182 | + |
| 183 | + const project = await Project.load(dir) |
| 184 | + |
| 185 | + await expect(selectActiveConfig(project, 'shopify.app.broken.toml')).rejects.toThrow() |
| 186 | + }) |
| 187 | + }) |
| 188 | + |
| 189 | + test('loads active config even when an unrelated config is malformed', async () => { |
| 190 | + await inTemporaryDirectory(async (dir) => { |
| 191 | + await writeFile(joinPath(dir, 'shopify.app.toml'), 'client_id = "good"') |
| 192 | + await writeFile(joinPath(dir, 'shopify.app.broken.toml'), '{{invalid toml') |
| 193 | + |
| 194 | + const project = await Project.load(dir) |
| 195 | + |
| 196 | + // The broken config is skipped, but selecting the good one works fine |
| 197 | + const config = await selectActiveConfig(project) |
| 198 | + |
| 199 | + expect(config.file.content.client_id).toBe('good') |
| 200 | + expect(basename(config.file.path)).toBe('shopify.app.toml') |
| 201 | + }) |
| 202 | + }) |
| 203 | +}) |
0 commit comments