|
| 1 | +import assert from 'node:assert'; |
| 2 | +import { afterEach, beforeEach, describe, it } from 'node:test'; |
| 3 | +import { createPluginServices } from '../../src/plugin-api.js'; |
| 4 | +import { resetGitHubEventCache } from '../../src/utils/ci-env.js'; |
| 5 | + |
| 6 | +describe('Plugin API', () => { |
| 7 | + let originalEnv; |
| 8 | + |
| 9 | + // Minimal mock services required by createPluginServices |
| 10 | + let mockServices = { |
| 11 | + testRunner: { |
| 12 | + once: () => {}, |
| 13 | + on: () => {}, |
| 14 | + off: () => {}, |
| 15 | + createBuild: () => {}, |
| 16 | + finalizeBuild: () => {}, |
| 17 | + }, |
| 18 | + serverManager: { |
| 19 | + start: () => {}, |
| 20 | + stop: () => {}, |
| 21 | + }, |
| 22 | + }; |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + originalEnv = { ...process.env }; |
| 26 | + // Clear CI-related environment variables |
| 27 | + let ciVars = [ |
| 28 | + 'VIZZLY_BRANCH', |
| 29 | + 'VIZZLY_COMMIT_SHA', |
| 30 | + 'VIZZLY_COMMIT_MESSAGE', |
| 31 | + 'VIZZLY_PR_NUMBER', |
| 32 | + 'GITHUB_ACTIONS', |
| 33 | + 'GITHUB_HEAD_REF', |
| 34 | + 'GITHUB_REF_NAME', |
| 35 | + 'GITHUB_SHA', |
| 36 | + 'GITHUB_EVENT_NAME', |
| 37 | + 'GITHUB_EVENT_PATH', |
| 38 | + 'GITHUB_REF', |
| 39 | + ]; |
| 40 | + for (let key of ciVars) { |
| 41 | + delete process.env[key]; |
| 42 | + } |
| 43 | + resetGitHubEventCache(); |
| 44 | + }); |
| 45 | + |
| 46 | + afterEach(() => { |
| 47 | + process.env = originalEnv; |
| 48 | + resetGitHubEventCache(); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('createPluginServices', () => { |
| 52 | + it('returns frozen object with expected shape', () => { |
| 53 | + let services = createPluginServices(mockServices); |
| 54 | + |
| 55 | + assert.ok(Object.isFrozen(services), 'services should be frozen'); |
| 56 | + assert.ok(services.git, 'should have git property'); |
| 57 | + assert.ok(services.testRunner, 'should have testRunner property'); |
| 58 | + assert.ok(services.serverManager, 'should have serverManager property'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('exposes git.detect as a function', () => { |
| 62 | + let services = createPluginServices(mockServices); |
| 63 | + |
| 64 | + assert.strictEqual(typeof services.git.detect, 'function'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('exposes testRunner methods', () => { |
| 68 | + let services = createPluginServices(mockServices); |
| 69 | + |
| 70 | + assert.strictEqual(typeof services.testRunner.once, 'function'); |
| 71 | + assert.strictEqual(typeof services.testRunner.on, 'function'); |
| 72 | + assert.strictEqual(typeof services.testRunner.off, 'function'); |
| 73 | + assert.strictEqual(typeof services.testRunner.createBuild, 'function'); |
| 74 | + assert.strictEqual(typeof services.testRunner.finalizeBuild, 'function'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('exposes serverManager methods', () => { |
| 78 | + let services = createPluginServices(mockServices); |
| 79 | + |
| 80 | + assert.strictEqual(typeof services.serverManager.start, 'function'); |
| 81 | + assert.strictEqual(typeof services.serverManager.stop, 'function'); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('services.git.detect', () => { |
| 86 | + it('returns object with expected properties', async () => { |
| 87 | + let services = createPluginServices(mockServices); |
| 88 | + let result = await services.git.detect(); |
| 89 | + |
| 90 | + assert.ok('branch' in result, 'should have branch property'); |
| 91 | + assert.ok('commit' in result, 'should have commit property'); |
| 92 | + assert.ok('message' in result, 'should have message property'); |
| 93 | + assert.ok('prNumber' in result, 'should have prNumber property'); |
| 94 | + assert.ok('buildName' in result, 'should have buildName property'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('detects branch from local git repo', async () => { |
| 98 | + let services = createPluginServices(mockServices); |
| 99 | + let result = await services.git.detect(); |
| 100 | + |
| 101 | + // We're in a git repo, so branch should be detected |
| 102 | + assert.ok(result.branch, 'branch should be detected'); |
| 103 | + assert.strictEqual(typeof result.branch, 'string'); |
| 104 | + }); |
| 105 | + |
| 106 | + it('detects commit from local git repo', async () => { |
| 107 | + let services = createPluginServices(mockServices); |
| 108 | + let result = await services.git.detect(); |
| 109 | + |
| 110 | + // We're in a git repo, so commit should be detected |
| 111 | + assert.ok(result.commit, 'commit should be detected'); |
| 112 | + assert.strictEqual( |
| 113 | + result.commit.length, |
| 114 | + 40, |
| 115 | + 'commit should be 40 char SHA' |
| 116 | + ); |
| 117 | + }); |
| 118 | + |
| 119 | + it('uses buildPrefix in buildName when provided', async () => { |
| 120 | + let services = createPluginServices(mockServices); |
| 121 | + let result = await services.git.detect({ buildPrefix: 'Storybook' }); |
| 122 | + |
| 123 | + assert.ok(result.buildName, 'buildName should be present'); |
| 124 | + assert.ok( |
| 125 | + result.buildName.startsWith('Storybook'), |
| 126 | + `buildName should start with prefix, got: ${result.buildName}` |
| 127 | + ); |
| 128 | + }); |
| 129 | + |
| 130 | + it('respects VIZZLY_BRANCH environment variable', async () => { |
| 131 | + process.env.VIZZLY_BRANCH = 'custom-branch'; |
| 132 | + |
| 133 | + let services = createPluginServices(mockServices); |
| 134 | + let result = await services.git.detect(); |
| 135 | + |
| 136 | + assert.strictEqual(result.branch, 'custom-branch'); |
| 137 | + }); |
| 138 | + |
| 139 | + it('respects VIZZLY_COMMIT_SHA environment variable', async () => { |
| 140 | + process.env.VIZZLY_COMMIT_SHA = 'abc123def456'; |
| 141 | + |
| 142 | + let services = createPluginServices(mockServices); |
| 143 | + let result = await services.git.detect(); |
| 144 | + |
| 145 | + assert.strictEqual(result.commit, 'abc123def456'); |
| 146 | + }); |
| 147 | + |
| 148 | + it('detects GitHub Actions PR context', async () => { |
| 149 | + process.env.GITHUB_ACTIONS = 'true'; |
| 150 | + process.env.GITHUB_HEAD_REF = 'feature/my-branch'; |
| 151 | + process.env.GITHUB_EVENT_NAME = 'pull_request'; |
| 152 | + process.env.GITHUB_REF = 'refs/pull/42/merge'; |
| 153 | + |
| 154 | + let services = createPluginServices(mockServices); |
| 155 | + let result = await services.git.detect(); |
| 156 | + |
| 157 | + assert.strictEqual(result.branch, 'feature/my-branch'); |
| 158 | + assert.strictEqual(result.prNumber, 42); |
| 159 | + }); |
| 160 | + |
| 161 | + it('returns null for prNumber when not in PR context', async () => { |
| 162 | + let services = createPluginServices(mockServices); |
| 163 | + let result = await services.git.detect(); |
| 164 | + |
| 165 | + // Not in a PR context, so prNumber should be null |
| 166 | + assert.strictEqual(result.prNumber, null); |
| 167 | + }); |
| 168 | + }); |
| 169 | +}); |
0 commit comments