|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | + |
| 3 | +import { runRule } from '../../lib/init-test' |
| 4 | +import { ctasSchema } from '../../lib/linting-rules/ctas-schema' |
| 5 | + |
| 6 | +describe(ctasSchema.names.join(' - '), () => { |
| 7 | + test('valid CTA URL passes validation', async () => { |
| 8 | + const markdown = ` |
| 9 | +[Try Copilot](https://github.com/github-copilot/signup?ref_product=copilot&ref_type=trial&ref_style=text&ref_plan=pro) |
| 10 | +` |
| 11 | + const result = await runRule(ctasSchema, { strings: { markdown } }) |
| 12 | + const errors = result.markdown |
| 13 | + expect(errors.length).toBe(0) |
| 14 | + }) |
| 15 | + |
| 16 | + test('invalid ref_product value fails validation', async () => { |
| 17 | + const markdown = ` |
| 18 | +[Try Copilot](https://github.com/github-copilot/signup?ref_product=invalid&ref_type=trial&ref_style=text) |
| 19 | +` |
| 20 | + const result = await runRule(ctasSchema, { strings: { markdown } }) |
| 21 | + const errors = result.markdown |
| 22 | + expect(errors.length).toBe(1) |
| 23 | + expect(errors[0].errorDetail).toContain('Invalid value for ref_product') |
| 24 | + }) |
| 25 | + |
| 26 | + test('missing required parameter fails validation', async () => { |
| 27 | + const markdown = ` |
| 28 | +[Try Copilot](https://github.com/github-copilot/signup?ref_product=copilot&ref_style=text) |
| 29 | +` |
| 30 | + const result = await runRule(ctasSchema, { strings: { markdown } }) |
| 31 | + const errors = result.markdown |
| 32 | + expect(errors.length).toBe(1) |
| 33 | + expect(errors[0].errorDetail).toContain('Missing required parameter: ref_type') |
| 34 | + }) |
| 35 | + |
| 36 | + test('unexpected parameter fails validation', async () => { |
| 37 | + const markdown = ` |
| 38 | +[Try Copilot](https://github.com/github-copilot/signup?ref_product=copilot&ref_type=trial&ref_style=text&ref_unknown=test) |
| 39 | +` |
| 40 | + const result = await runRule(ctasSchema, { strings: { markdown } }) |
| 41 | + const errors = result.markdown |
| 42 | + expect(errors.length).toBe(1) |
| 43 | + expect(errors[0].errorDetail).toContain('Unexpected parameter: ref_unknown') |
| 44 | + }) |
| 45 | + |
| 46 | + test('non-CTA URLs are ignored', async () => { |
| 47 | + const markdown = ` |
| 48 | +[Regular link](https://github.com/features) |
| 49 | +[External link](https://example.com?param=value) |
| 50 | +` |
| 51 | + const result = await runRule(ctasSchema, { strings: { markdown } }) |
| 52 | + const errors = result.markdown |
| 53 | + expect(errors.length).toBe(0) |
| 54 | + }) |
| 55 | + |
| 56 | + test('case sensitive validation enforces lowercase values', async () => { |
| 57 | + const markdown = ` |
| 58 | +[Try Copilot](https://github.com/github-copilot/signup?ref_product=copilot&ref_type=Trial&ref_style=Button) |
| 59 | +` |
| 60 | + const result = await runRule(ctasSchema, { strings: { markdown } }) |
| 61 | + const errors = result.markdown |
| 62 | + expect(errors.length).toBe(2) // Should have errors for 'Trial' and 'Button' |
| 63 | + |
| 64 | + // Check that both expected errors are present (order may vary) |
| 65 | + const errorMessages = errors.map((error) => error.errorDetail) |
| 66 | + expect(errorMessages.some((msg) => msg.includes('Invalid value for ref_type: "Trial"'))).toBe( |
| 67 | + true, |
| 68 | + ) |
| 69 | + expect(errorMessages.some((msg) => msg.includes('Invalid value for ref_style: "Button"'))).toBe( |
| 70 | + true, |
| 71 | + ) |
| 72 | + }) |
| 73 | + |
| 74 | + test('URL regex correctly stops at curly braces (not overgreedy)', async () => { |
| 75 | + const markdown = ` |
| 76 | +--- |
| 77 | +try_ghec_for_free: '{% ifversion ghec %}https://github.com/account/enterprises/new?ref_cta=GHEC+trial&ref_loc=enterprise+administrators+landing+page&ref_page=docs{% endif %}' |
| 78 | +--- |
| 79 | +` |
| 80 | + const result = await runRule(ctasSchema, { strings: { markdown } }) |
| 81 | + const errors = result.markdown |
| 82 | + expect(errors.length).toBe(1) // Should detect and try to convert the old CTA format |
| 83 | + expect(errors[0].fixInfo).toBeDefined() |
| 84 | + |
| 85 | + // The extracted URL should not include the curly brace - verify by checking the fix |
| 86 | + const fixedUrl = errors[0].fixInfo?.insertText |
| 87 | + expect(fixedUrl).toBeDefined() |
| 88 | + expect(fixedUrl).not.toContain('{') // Should not include curly brace from Liquid syntax |
| 89 | + expect(fixedUrl).not.toContain('}') // Should not include curly brace from Liquid syntax |
| 90 | + expect(fixedUrl).toContain('ref_product=ghec') // Should have converted old format correctly |
| 91 | + }) |
| 92 | + |
| 93 | + test('old CTA format autofix preserves original URL structure', async () => { |
| 94 | + const markdown = ` |
| 95 | +[Try Copilot](https://github.com?ref_cta=Copilot+trial&ref_loc=getting+started&ref_page=docs) |
| 96 | +` |
| 97 | + const result = await runRule(ctasSchema, { strings: { markdown } }) |
| 98 | + const errors = result.markdown |
| 99 | + expect(errors.length).toBe(1) |
| 100 | + expect(errors[0].fixInfo).toBeDefined() |
| 101 | + |
| 102 | + // The fixed URL should not introduce extra slashes |
| 103 | + const fixedUrl = errors[0].fixInfo?.insertText |
| 104 | + expect(fixedUrl).toBeDefined() |
| 105 | + expect(fixedUrl).toMatch(/^https:\/\/github\.com\?ref_product=/) // Should not have github.com/? |
| 106 | + expect(fixedUrl).not.toMatch(/github\.com\/\?/) // Should not contain extra slash before query |
| 107 | + }) |
| 108 | + |
| 109 | + test('mixed parameter scenarios - new format takes precedence over old', async () => { |
| 110 | + const markdown = ` |
| 111 | +[Mixed Format](https://github.com/copilot?ref_product=copilot&ref_type=trial&ref_cta=Copilot+Enterprise+trial&ref_loc=enterprise+page) |
| 112 | +` |
| 113 | + const result = await runRule(ctasSchema, { strings: { markdown } }) |
| 114 | + const errors = result.markdown |
| 115 | + expect(errors.length).toBe(1) |
| 116 | + expect(errors[0].fixInfo).toBeDefined() |
| 117 | + |
| 118 | + // Should preserve existing new format parameters, only convert old ones not already covered |
| 119 | + const fixedUrl = errors[0].fixInfo?.insertText |
| 120 | + expect(fixedUrl).toBeDefined() |
| 121 | + expect(fixedUrl).toContain('ref_product=copilot') // Preserved from new format |
| 122 | + expect(fixedUrl).toContain('ref_type=trial') // Preserved from new format |
| 123 | + expect(fixedUrl).not.toContain('ref_cta=') // Old parameter removed |
| 124 | + expect(fixedUrl).not.toContain('ref_loc=') // Old parameter removed |
| 125 | + }) |
| 126 | + |
| 127 | + test('hash fragment preservation during conversion', async () => { |
| 128 | + const markdown = ` |
| 129 | +[Copilot Pricing](https://github.com/copilot?ref_cta=Copilot+trial&ref_loc=getting+started&ref_page=docs#pricing) |
| 130 | +` |
| 131 | + const result = await runRule(ctasSchema, { strings: { markdown } }) |
| 132 | + const errors = result.markdown |
| 133 | + expect(errors.length).toBe(1) |
| 134 | + expect(errors[0].fixInfo).toBeDefined() |
| 135 | + |
| 136 | + const fixedUrl = errors[0].fixInfo?.insertText |
| 137 | + expect(fixedUrl).toBeDefined() |
| 138 | + expect(fixedUrl).toContain('#pricing') // Hash fragment preserved |
| 139 | + expect(fixedUrl).toContain('ref_product=copilot') |
| 140 | + }) |
| 141 | + |
| 142 | + test('UTM parameter preservation during conversion', async () => { |
| 143 | + const markdown = ` |
| 144 | +[Track This](https://github.com/copilot?utm_source=docs&utm_campaign=trial&ref_cta=Copilot+trial&ref_loc=getting+started&other_param=value) |
| 145 | +` |
| 146 | + const result = await runRule(ctasSchema, { strings: { markdown } }) |
| 147 | + const errors = result.markdown |
| 148 | + expect(errors.length).toBe(1) |
| 149 | + expect(errors[0].fixInfo).toBeDefined() |
| 150 | + |
| 151 | + const fixedUrl = errors[0].fixInfo?.insertText |
| 152 | + expect(fixedUrl).toBeDefined() |
| 153 | + expect(fixedUrl).toContain('utm_source=docs') // UTM preserved |
| 154 | + expect(fixedUrl).toContain('utm_campaign=trial') // UTM preserved |
| 155 | + expect(fixedUrl).toContain('other_param=value') // Other params preserved |
| 156 | + expect(fixedUrl).toContain('ref_product=copilot') // New CTA params added |
| 157 | + expect(fixedUrl).not.toContain('ref_cta=') // Old CTA params removed |
| 158 | + }) |
| 159 | + |
| 160 | + test('multiple query parameter types handled correctly', async () => { |
| 161 | + const markdown = ` |
| 162 | +[Complex URL](https://github.com/features/copilot?utm_source=docs&ref_product=copilot&ref_type=invalid_type&campaign_id=123&ref_cta=old_cta&locale=en#section) |
| 163 | +` |
| 164 | + const result = await runRule(ctasSchema, { strings: { markdown } }) |
| 165 | + const errors = result.markdown |
| 166 | + expect(errors.length).toBe(1) // Only old format conversion error |
| 167 | + expect(errors[0].errorDetail).toContain('old parameter format') |
| 168 | + expect(errors[0].fixInfo).toBeDefined() // Should have autofix |
| 169 | + }) |
| 170 | +}) |
0 commit comments