|
| 1 | +import { |
| 2 | + prependSchemaVersionHeader, |
| 3 | + readSchemaApiVersion, |
| 4 | + validateSchemaApiVersion, |
| 5 | + SCHEMA_VERSION_MARKER_PREFIX, |
| 6 | +} from './schema-version.js' |
| 7 | +import {describe, expect, test} from 'vitest' |
| 8 | +import {AbortError} from '@shopify/cli-kit/node/error' |
| 9 | +import {inTemporaryDirectory, writeFile} from '@shopify/cli-kit/node/fs' |
| 10 | +import {joinPath} from '@shopify/cli-kit/node/path' |
| 11 | + |
| 12 | +function options(directory: string, apiVersion: string) { |
| 13 | + return {directory, localIdentifier: 'my-function', apiVersion} |
| 14 | +} |
| 15 | + |
| 16 | +describe('prependSchemaVersionHeader', () => { |
| 17 | + test('prepends a comment block with the version marker', () => { |
| 18 | + const result = prependSchemaVersionHeader('type Query { id: ID }', '2025-10') |
| 19 | + |
| 20 | + expect(result.startsWith(`${SCHEMA_VERSION_MARKER_PREFIX}2025-10\n`)).toBe(true) |
| 21 | + expect(result.endsWith('type Query { id: ID }')).toBe(true) |
| 22 | + }) |
| 23 | +}) |
| 24 | + |
| 25 | +describe('readSchemaApiVersion', () => { |
| 26 | + test('returns the version when the marker is present', async () => { |
| 27 | + await inTemporaryDirectory(async (tmpDir) => { |
| 28 | + const path = joinPath(tmpDir, 'schema.graphql') |
| 29 | + await writeFile(path, prependSchemaVersionHeader('type Query { id: ID }', '2025-10')) |
| 30 | + |
| 31 | + await expect(readSchemaApiVersion(path)).resolves.toEqual('2025-10') |
| 32 | + }) |
| 33 | + }) |
| 34 | + |
| 35 | + test('returns undefined when the file has no marker', async () => { |
| 36 | + await inTemporaryDirectory(async (tmpDir) => { |
| 37 | + const path = joinPath(tmpDir, 'schema.graphql') |
| 38 | + await writeFile(path, '# some other comment\ntype Query { id: ID }') |
| 39 | + |
| 40 | + await expect(readSchemaApiVersion(path)).resolves.toBeUndefined() |
| 41 | + }) |
| 42 | + }) |
| 43 | + |
| 44 | + test('returns undefined when the file does not exist', async () => { |
| 45 | + await inTemporaryDirectory(async (tmpDir) => { |
| 46 | + await expect(readSchemaApiVersion(joinPath(tmpDir, 'missing.graphql'))).resolves.toBeUndefined() |
| 47 | + }) |
| 48 | + }) |
| 49 | + |
| 50 | + test('does not match the marker once SDL content has started', async () => { |
| 51 | + await inTemporaryDirectory(async (tmpDir) => { |
| 52 | + const path = joinPath(tmpDir, 'schema.graphql') |
| 53 | + // Marker buried after SDL content should be ignored. |
| 54 | + await writeFile(path, `type Query { id: ID }\n${SCHEMA_VERSION_MARKER_PREFIX}2025-10\n`) |
| 55 | + |
| 56 | + await expect(readSchemaApiVersion(path)).resolves.toBeUndefined() |
| 57 | + }) |
| 58 | + }) |
| 59 | + |
| 60 | + test('trims surrounding whitespace from the marker value', async () => { |
| 61 | + await inTemporaryDirectory(async (tmpDir) => { |
| 62 | + const path = joinPath(tmpDir, 'schema.graphql') |
| 63 | + await writeFile(path, `${SCHEMA_VERSION_MARKER_PREFIX} 2025-10 \ntype Query { id: ID }`) |
| 64 | + |
| 65 | + await expect(readSchemaApiVersion(path)).resolves.toEqual('2025-10') |
| 66 | + }) |
| 67 | + }) |
| 68 | +}) |
| 69 | + |
| 70 | +describe('validateSchemaApiVersion', () => { |
| 71 | + test('no-ops when the schema file does not exist', async () => { |
| 72 | + await inTemporaryDirectory(async (tmpDir) => { |
| 73 | + await expect(validateSchemaApiVersion(options(tmpDir, '2025-10'))).resolves.toBeUndefined() |
| 74 | + }) |
| 75 | + }) |
| 76 | + |
| 77 | + test('no-ops when the schema file has no version marker', async () => { |
| 78 | + await inTemporaryDirectory(async (tmpDir) => { |
| 79 | + await writeFile(joinPath(tmpDir, 'schema.graphql'), 'type Query { id: ID }') |
| 80 | + |
| 81 | + await expect(validateSchemaApiVersion(options(tmpDir, '2025-10'))).resolves.toBeUndefined() |
| 82 | + }) |
| 83 | + }) |
| 84 | + |
| 85 | + test('no-ops when the marker matches the configured api_version', async () => { |
| 86 | + await inTemporaryDirectory(async (tmpDir) => { |
| 87 | + await writeFile( |
| 88 | + joinPath(tmpDir, 'schema.graphql'), |
| 89 | + prependSchemaVersionHeader('type Query { id: ID }', '2025-10'), |
| 90 | + ) |
| 91 | + |
| 92 | + await expect(validateSchemaApiVersion(options(tmpDir, '2025-10'))).resolves.toBeUndefined() |
| 93 | + }) |
| 94 | + }) |
| 95 | + |
| 96 | + test('throws an AbortError with remediation when the marker is stale', async () => { |
| 97 | + await inTemporaryDirectory(async (tmpDir) => { |
| 98 | + await writeFile( |
| 99 | + joinPath(tmpDir, 'schema.graphql'), |
| 100 | + prependSchemaVersionHeader('type Query { id: ID }', '2025-07'), |
| 101 | + ) |
| 102 | + |
| 103 | + const result = validateSchemaApiVersion(options(tmpDir, '2025-10')) |
| 104 | + |
| 105 | + await expect(result).rejects.toThrow(AbortError) |
| 106 | + await expect(result).rejects.toThrow(/2025-07[\s\S]*2025-10/) |
| 107 | + }) |
| 108 | + }) |
| 109 | +}) |
0 commit comments