|
| 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} 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 | +const useModule = await import('../../services/app/config/use.js') |
| 19 | + |
| 20 | +beforeEach(() => { |
| 21 | + vi.mocked(getCachedAppInfo).mockReturnValue(undefined) |
| 22 | +}) |
| 23 | + |
| 24 | +describe('selectActiveConfig', () => { |
| 25 | + test('selects config by user-provided name (flag)', async () => { |
| 26 | + await inTemporaryDirectory(async (dir) => { |
| 27 | + await writeFile(joinPath(dir, 'shopify.app.toml'), 'client_id = "default"') |
| 28 | + await writeFile(joinPath(dir, 'shopify.app.staging.toml'), 'client_id = "staging"') |
| 29 | + const project = await Project.load(dir) |
| 30 | + |
| 31 | + const config = await selectActiveConfig(project, 'staging') |
| 32 | + |
| 33 | + expect(config.fileName).toBe('shopify.app.staging.toml') |
| 34 | + expect(config.file.content.client_id).toBe('staging') |
| 35 | + expect(config.source).toBe('flag') |
| 36 | + expect(config.isLinked).toBe(true) |
| 37 | + }) |
| 38 | + }) |
| 39 | + |
| 40 | + test('selects config from cache', async () => { |
| 41 | + await inTemporaryDirectory(async (dir) => { |
| 42 | + await writeFile(joinPath(dir, 'shopify.app.toml'), 'client_id = "default"') |
| 43 | + await writeFile(joinPath(dir, 'shopify.app.production.toml'), 'client_id = "production"') |
| 44 | + const project = await Project.load(dir) |
| 45 | + |
| 46 | + vi.mocked(getCachedAppInfo).mockReturnValue({ |
| 47 | + directory: dir, |
| 48 | + configFile: 'shopify.app.production.toml', |
| 49 | + }) |
| 50 | + |
| 51 | + const config = await selectActiveConfig(project) |
| 52 | + |
| 53 | + expect(config.fileName).toBe('shopify.app.production.toml') |
| 54 | + expect(config.file.content.client_id).toBe('production') |
| 55 | + expect(config.source).toBe('cached') |
| 56 | + }) |
| 57 | + }) |
| 58 | + |
| 59 | + test('falls back to default shopify.app.toml when no flag or cache', async () => { |
| 60 | + await inTemporaryDirectory(async (dir) => { |
| 61 | + await writeFile(joinPath(dir, 'shopify.app.toml'), 'client_id = "default-id"') |
| 62 | + const project = await Project.load(dir) |
| 63 | + |
| 64 | + const config = await selectActiveConfig(project) |
| 65 | + |
| 66 | + expect(config.fileName).toBe('shopify.app.toml') |
| 67 | + expect(config.file.content.client_id).toBe('default-id') |
| 68 | + }) |
| 69 | + }) |
| 70 | + |
| 71 | + test('detects isLinked from client_id', async () => { |
| 72 | + await inTemporaryDirectory(async (dir) => { |
| 73 | + await writeFile(joinPath(dir, 'shopify.app.toml'), 'client_id = ""') |
| 74 | + const project = await Project.load(dir) |
| 75 | + |
| 76 | + const config = await selectActiveConfig(project) |
| 77 | + |
| 78 | + expect(config.isLinked).toBe(false) |
| 79 | + }) |
| 80 | + }) |
| 81 | + |
| 82 | + test('detects isLinked when client_id is present', async () => { |
| 83 | + await inTemporaryDirectory(async (dir) => { |
| 84 | + await writeFile(joinPath(dir, 'shopify.app.toml'), 'client_id = "abc123"') |
| 85 | + const project = await Project.load(dir) |
| 86 | + |
| 87 | + const config = await selectActiveConfig(project) |
| 88 | + |
| 89 | + expect(config.isLinked).toBe(true) |
| 90 | + }) |
| 91 | + }) |
| 92 | + |
| 93 | + test('resolves config-specific dotenv', async () => { |
| 94 | + await inTemporaryDirectory(async (dir) => { |
| 95 | + await writeFile(joinPath(dir, 'shopify.app.staging.toml'), 'client_id = "staging"') |
| 96 | + await writeFile(joinPath(dir, 'shopify.app.toml'), 'client_id = "default"') |
| 97 | + await writeFile(joinPath(dir, '.env'), 'KEY=default') |
| 98 | + await writeFile(joinPath(dir, '.env.staging'), 'KEY=staging') |
| 99 | + const project = await Project.load(dir) |
| 100 | + |
| 101 | + const config = await selectActiveConfig(project, 'staging') |
| 102 | + |
| 103 | + expect(config.dotenv).toBeDefined() |
| 104 | + expect(config.dotenv!.variables.KEY).toBe('staging') |
| 105 | + }) |
| 106 | + }) |
| 107 | + |
| 108 | + test('resolves hidden config for client_id', async () => { |
| 109 | + await inTemporaryDirectory(async (dir) => { |
| 110 | + await writeFile(joinPath(dir, 'shopify.app.toml'), 'client_id = "abc123"') |
| 111 | + await mkdir(joinPath(dir, '.shopify')) |
| 112 | + await writeFile( |
| 113 | + joinPath(dir, '.shopify', 'project.json'), |
| 114 | + JSON.stringify({abc123: {dev_store_url: 'test.myshopify.com'}}), |
| 115 | + ) |
| 116 | + const project = await Project.load(dir) |
| 117 | + |
| 118 | + const config = await selectActiveConfig(project) |
| 119 | + |
| 120 | + expect(config.hiddenConfig).toStrictEqual({dev_store_url: 'test.myshopify.com'}) |
| 121 | + }) |
| 122 | + }) |
| 123 | + |
| 124 | + test('returns empty hidden config when no entry for client_id', async () => { |
| 125 | + await inTemporaryDirectory(async (dir) => { |
| 126 | + await writeFile(joinPath(dir, 'shopify.app.toml'), 'client_id = "abc123"') |
| 127 | + const project = await Project.load(dir) |
| 128 | + |
| 129 | + const config = await selectActiveConfig(project) |
| 130 | + |
| 131 | + expect(config.hiddenConfig).toStrictEqual({}) |
| 132 | + }) |
| 133 | + }) |
| 134 | + |
| 135 | + test('path is absolute', async () => { |
| 136 | + await inTemporaryDirectory(async (dir) => { |
| 137 | + await writeFile(joinPath(dir, 'shopify.app.toml'), 'client_id = "abc"') |
| 138 | + const project = await Project.load(dir) |
| 139 | + |
| 140 | + const config = await selectActiveConfig(project) |
| 141 | + |
| 142 | + expect(config.path).toBe(joinPath(dir, 'shopify.app.toml')) |
| 143 | + }) |
| 144 | + }) |
| 145 | + |
| 146 | + test('accepts full filename as config name', async () => { |
| 147 | + await inTemporaryDirectory(async (dir) => { |
| 148 | + await writeFile(joinPath(dir, 'shopify.app.toml'), 'client_id = "default"') |
| 149 | + await writeFile(joinPath(dir, 'shopify.app.staging.toml'), 'client_id = "staging"') |
| 150 | + const project = await Project.load(dir) |
| 151 | + |
| 152 | + const config = await selectActiveConfig(project, 'shopify.app.staging.toml') |
| 153 | + |
| 154 | + expect(config.fileName).toBe('shopify.app.staging.toml') |
| 155 | + expect(config.file.content.client_id).toBe('staging') |
| 156 | + }) |
| 157 | + }) |
| 158 | + |
| 159 | + test('throws when requested config does not exist', async () => { |
| 160 | + await inTemporaryDirectory(async (dir) => { |
| 161 | + await writeFile(joinPath(dir, 'shopify.app.toml'), 'client_id = "default"') |
| 162 | + const project = await Project.load(dir) |
| 163 | + |
| 164 | + await expect(selectActiveConfig(project, 'nonexistent')).rejects.toThrow() |
| 165 | + }) |
| 166 | + }) |
| 167 | +}) |
0 commit comments