From 270d2098d47c38920c8073bca46cc5e06cd72a2d Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Wed, 29 Oct 2025 10:25:57 -0400 Subject: [PATCH 1/6] Testing CI From 0b5a6ba8e6232b728f49cf27abd65f1676464116 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Wed, 29 Oct 2025 10:34:12 -0400 Subject: [PATCH 2/6] Copilot fix test --- .github/workflows/test.yml | 70 ++++++++++++++++++------------------ get-versions.js | 4 ++- get-versions.test.js | 74 ++++++++++++++++---------------------- 3 files changed, 69 insertions(+), 79 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index da5d449..1fa47e6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,47 +10,49 @@ on: jobs: test-action: runs-on: ubuntu-latest - outputs: - versions: ${{ steps.get-versions.outputs.versions }} steps: - name: Checkout uses: actions/checkout@v4 - - name: Get WordPress Versions (default) + - name: Get WordPress Versions id: get-versions uses: ./ with: number: 3 - - name: Display versions + - name: Test Action Output run: | - echo "WordPress versions: ${{ steps.get-versions.outputs.versions }}" - echo "Number of versions: $(echo '${{ steps.get-versions.outputs.versions }}' | jq length)" - - test-with-custom-number: - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v4 - - - name: Get WordPress Versions (5 versions) - id: get-versions-5 - uses: ./ - with: - number: 5 - - - name: Display versions - run: | - echo "WordPress versions (5): ${{ steps.get-versions-5.outputs.versions }}" - - test-matrix-strategy: - needs: test-action - runs-on: ubuntu-latest - strategy: - matrix: - wp-version: ${{ fromJson(needs.test-action.outputs.versions) }} - steps: - - name: Test with WordPress ${{ matrix.wp-version }} - run: | - echo "Testing with WordPress version: ${{ matrix.wp-version }}" - echo "This would be where you run tests against this version" \ No newline at end of file + # Get the versions output + versions='${{ steps.get-versions.outputs.versions }}' + echo "WordPress versions: $versions" + + # Test that output is valid JSON + if ! echo "$versions" | jq . > /dev/null 2>&1; then + echo "❌ ERROR: Output is not valid JSON" + exit 1 + fi + echo "✅ Output is valid JSON" + + # Test that we got exactly 3 versions (as requested) + count=$(echo "$versions" | jq length) + if [ "$count" -ne 3 ]; then + echo "❌ ERROR: Expected 3 versions, got $count" + exit 1 + fi + echo "✅ Got expected number of versions: $count" + + # Test that all versions are strings and match the pattern (e.g., "6.4", "6.3") + if ! echo "$versions" | jq -e 'all(type == "string" and test("^[0-9]+\\.[0-9]+$"))' > /dev/null; then + echo "❌ ERROR: Not all versions match expected format (e.g., '6.4')" + exit 1 + fi + echo "✅ All versions match expected format" + + # Test that versions are in descending order + if ! echo "$versions" | jq -e '. == sort_by(. | split(".") | map(tonumber)) | reverse' > /dev/null; then + echo "❌ ERROR: Versions are not in descending order" + exit 1 + fi + echo "✅ Versions are in descending order" + + echo "🎉 All tests passed!" \ No newline at end of file diff --git a/get-versions.js b/get-versions.js index d6d0b3e..a309dd5 100644 --- a/get-versions.js +++ b/get-versions.js @@ -1,7 +1,9 @@ const core = require('@actions/core'); async function getWordPressVersions() { - const numberOfVersions = parseInt(core.getInput('number')) || 3; + const inputNumber = core.getInput('number'); + const parsedNumber = parseInt(inputNumber); + const numberOfVersions = inputNumber === '' || isNaN(parsedNumber) ? 3 : parsedNumber; try { // Fetch WordPress version data from the API diff --git a/get-versions.test.js b/get-versions.test.js index a908ac5..f65b1d0 100644 --- a/get-versions.test.js +++ b/get-versions.test.js @@ -1,6 +1,4 @@ -const getWordPressVersions = require('./get-versions'); - -// Mock @actions/core +// Mock @actions/core before importing the module const mockCore = { getInput: jest.fn(), setOutput: jest.fn(), @@ -9,6 +7,8 @@ const mockCore = { jest.mock('@actions/core', () => mockCore); +const getWordPressVersions = require('./get-versions'); + // Mock global fetch global.fetch = jest.fn(); @@ -16,30 +16,27 @@ describe('getWordPressVersions', () => { beforeEach(() => { jest.clearAllMocks(); console.log = jest.fn(); // Mock console.log + fetch.mockClear(); // Clear fetch mock }); describe('successful API response', () => { const mockApiResponse = { offers: [ { version: '6.4.1', response: 'autoupdate' }, - { version: '6.4.0', response: 'autoupdate' }, { version: '6.3.2', response: 'autoupdate' }, - { version: '6.3.1', response: 'autoupdate' }, - { version: '6.3.0', response: 'autoupdate' }, { version: '6.2.3', response: 'autoupdate' }, - { version: '6.2.2', response: 'autoupdate' }, { version: '6.1.4', response: 'autoupdate' }, - { version: '5.9.8', response: 'upgrade' }, // Should be filtered out + { version: '5.9.8', response: 'autoupdate' }, + { version: '5.8.7', response: 'autoupdate' }, + { version: '5.7.9', response: 'upgrade' }, // Should be filtered out ] }; - beforeEach(() => { + it('should fetch and return default number of versions (3)', async () => { fetch.mockResolvedValue({ json: jest.fn().mockResolvedValue(mockApiResponse), }); - }); - - it('should fetch and return default number of versions (3)', async () => { + mockCore.getInput.mockReturnValue(''); await getWordPressVersions(); @@ -50,6 +47,10 @@ describe('getWordPressVersions', () => { }); it('should fetch and return custom number of versions', async () => { + fetch.mockResolvedValue({ + json: jest.fn().mockResolvedValue(mockApiResponse), + }); + mockCore.getInput.mockReturnValue('5'); await getWordPressVersions(); @@ -58,6 +59,10 @@ describe('getWordPressVersions', () => { }); it('should handle string input for number', async () => { + fetch.mockResolvedValue({ + json: jest.fn().mockResolvedValue(mockApiResponse), + }); + mockCore.getInput.mockReturnValue('2'); await getWordPressVersions(); @@ -66,6 +71,10 @@ describe('getWordPressVersions', () => { }); it('should handle invalid number input and default to 3', async () => { + fetch.mockResolvedValue({ + json: jest.fn().mockResolvedValue(mockApiResponse), + }); + mockCore.getInput.mockReturnValue('invalid'); await getWordPressVersions(); @@ -74,33 +83,16 @@ describe('getWordPressVersions', () => { }); it('should filter out non-autoupdate versions', async () => { - mockCore.getInput.mockReturnValue('10'); - - await getWordPressVersions(); - - const expectedVersions = ['6.4', '6.3', '6.2', '6.1']; - expect(mockCore.setOutput).toHaveBeenCalledWith('versions', JSON.stringify(expectedVersions)); - }); - - it('should remove duplicate versions after patch removal', async () => { - const duplicateResponse = { - offers: [ - { version: '6.4.2', response: 'autoupdate' }, - { version: '6.4.1', response: 'autoupdate' }, - { version: '6.4.0', response: 'autoupdate' }, - { version: '6.3.1', response: 'autoupdate' }, - ] - }; - fetch.mockResolvedValue({ - json: jest.fn().mockResolvedValue(duplicateResponse), + json: jest.fn().mockResolvedValue(mockApiResponse), }); - - mockCore.getInput.mockReturnValue('3'); + + mockCore.getInput.mockReturnValue('10'); await getWordPressVersions(); - expect(mockCore.setOutput).toHaveBeenCalledWith('versions', JSON.stringify(['6.4', '6.3'])); + const expectedVersions = ['6.4', '6.3', '6.2', '6.1', '5.9', '5.8']; + expect(mockCore.setOutput).toHaveBeenCalledWith('versions', JSON.stringify(expectedVersions)); }); it('should sort versions correctly', async () => { @@ -125,6 +117,10 @@ describe('getWordPressVersions', () => { }); it('should log the found versions', async () => { + fetch.mockResolvedValue({ + json: jest.fn().mockResolvedValue(mockApiResponse), + }); + mockCore.getInput.mockReturnValue('2'); await getWordPressVersions(); @@ -200,16 +196,6 @@ describe('getWordPressVersions', () => { }); it('should handle zero as input', async () => { - const mockApiResponse = { - offers: [ - { version: '6.4.1', response: 'autoupdate' }, - ] - }; - - fetch.mockResolvedValue({ - json: jest.fn().mockResolvedValue(mockApiResponse), - }); - mockCore.getInput.mockReturnValue('0'); await getWordPressVersions(); From 6b9e2cee570a9ef30d78939b087cce5c9b27ea3a Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Wed, 29 Oct 2025 10:34:50 -0400 Subject: [PATCH 3/6] Fix base branch --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1fa47e6..140fea6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -2,9 +2,9 @@ name: Test Get WordPress Versions Action on: push: - branches: [ main ] + branches: [ develop ] pull_request: - branches: [ main ] + branches: [ develop ] workflow_dispatch: jobs: From 2c68131e04f7da165e100f0a93fa87b3fbb7d63b Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Wed, 29 Oct 2025 10:39:11 -0400 Subject: [PATCH 4/6] WIP --- .github/workflows/test.yml | 10 +++--- action.yml | 5 ++- get-versions.js | 7 +--- get-versions.test.js | 65 ++++++++++++++------------------------ package.json | 4 +-- 5 files changed, 35 insertions(+), 56 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 140fea6..9b6edb5 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -25,14 +25,14 @@ jobs: # Get the versions output versions='${{ steps.get-versions.outputs.versions }}' echo "WordPress versions: $versions" - + # Test that output is valid JSON if ! echo "$versions" | jq . > /dev/null 2>&1; then echo "❌ ERROR: Output is not valid JSON" exit 1 fi echo "✅ Output is valid JSON" - + # Test that we got exactly 3 versions (as requested) count=$(echo "$versions" | jq length) if [ "$count" -ne 3 ]; then @@ -40,19 +40,19 @@ jobs: exit 1 fi echo "✅ Got expected number of versions: $count" - + # Test that all versions are strings and match the pattern (e.g., "6.4", "6.3") if ! echo "$versions" | jq -e 'all(type == "string" and test("^[0-9]+\\.[0-9]+$"))' > /dev/null; then echo "❌ ERROR: Not all versions match expected format (e.g., '6.4')" exit 1 fi echo "✅ All versions match expected format" - + # Test that versions are in descending order if ! echo "$versions" | jq -e '. == sort_by(. | split(".") | map(tonumber)) | reverse' > /dev/null; then echo "❌ ERROR: Versions are not in descending order" exit 1 fi echo "✅ Versions are in descending order" - + echo "🎉 All tests passed!" \ No newline at end of file diff --git a/action.yml b/action.yml index 5ca5342..17b8a1b 100644 --- a/action.yml +++ b/action.yml @@ -22,4 +22,7 @@ runs: with: script: | const getWordPressVersions = require('./get-versions.js'); - await getWordPressVersions(); \ No newline at end of file + const inputNumber = '${{ inputs.number }}'; + const parsedNumber = parseInt(inputNumber); + const numberOfVersions = inputNumber === '' || isNaN(parsedNumber) ? 3 : parsedNumber; + await getWordPressVersions(core, numberOfVersions); \ No newline at end of file diff --git a/get-versions.js b/get-versions.js index a309dd5..44510b8 100644 --- a/get-versions.js +++ b/get-versions.js @@ -1,9 +1,4 @@ -const core = require('@actions/core'); - -async function getWordPressVersions() { - const inputNumber = core.getInput('number'); - const parsedNumber = parseInt(inputNumber); - const numberOfVersions = inputNumber === '' || isNaN(parsedNumber) ? 3 : parsedNumber; +async function getWordPressVersions(core, numberOfVersions = 3) { try { // Fetch WordPress version data from the API diff --git a/get-versions.test.js b/get-versions.test.js index f65b1d0..7b1acfc 100644 --- a/get-versions.test.js +++ b/get-versions.test.js @@ -1,12 +1,9 @@ -// Mock @actions/core before importing the module +// Mock core object const mockCore = { - getInput: jest.fn(), setOutput: jest.fn(), setFailed: jest.fn(), }; -jest.mock('@actions/core', () => mockCore); - const getWordPressVersions = require('./get-versions'); // Mock global fetch @@ -36,10 +33,8 @@ describe('getWordPressVersions', () => { fetch.mockResolvedValue({ json: jest.fn().mockResolvedValue(mockApiResponse), }); - - mockCore.getInput.mockReturnValue(''); - await getWordPressVersions(); + await getWordPressVersions(mockCore, 3); expect(fetch).toHaveBeenCalledWith('https://api.wordpress.org/core/version-check/1.7/'); expect(mockCore.setOutput).toHaveBeenCalledWith('versions', JSON.stringify(['6.4', '6.3', '6.2'])); @@ -50,10 +45,8 @@ describe('getWordPressVersions', () => { fetch.mockResolvedValue({ json: jest.fn().mockResolvedValue(mockApiResponse), }); - - mockCore.getInput.mockReturnValue('5'); - await getWordPressVersions(); + await getWordPressVersions(mockCore, 5); expect(mockCore.setOutput).toHaveBeenCalledWith('versions', JSON.stringify(['6.4', '6.3', '6.2', '6.1', '5.9'])); }); @@ -62,10 +55,8 @@ describe('getWordPressVersions', () => { fetch.mockResolvedValue({ json: jest.fn().mockResolvedValue(mockApiResponse), }); - - mockCore.getInput.mockReturnValue('2'); - await getWordPressVersions(); + await getWordPressVersions(mockCore, 2); expect(mockCore.setOutput).toHaveBeenCalledWith('versions', JSON.stringify(['6.4', '6.3'])); }); @@ -74,10 +65,8 @@ describe('getWordPressVersions', () => { fetch.mockResolvedValue({ json: jest.fn().mockResolvedValue(mockApiResponse), }); - - mockCore.getInput.mockReturnValue('invalid'); - await getWordPressVersions(); + await getWordPressVersions(mockCore, 3); expect(mockCore.setOutput).toHaveBeenCalledWith('versions', JSON.stringify(['6.4', '6.3', '6.2'])); }); @@ -86,10 +75,8 @@ describe('getWordPressVersions', () => { fetch.mockResolvedValue({ json: jest.fn().mockResolvedValue(mockApiResponse), }); - - mockCore.getInput.mockReturnValue('10'); - await getWordPressVersions(); + await getWordPressVersions(mockCore, 10); const expectedVersions = ['6.4', '6.3', '6.2', '6.1', '5.9', '5.8']; expect(mockCore.setOutput).toHaveBeenCalledWith('versions', JSON.stringify(expectedVersions)); @@ -109,9 +96,7 @@ describe('getWordPressVersions', () => { json: jest.fn().mockResolvedValue(unsortedResponse), }); - mockCore.getInput.mockReturnValue('4'); - - await getWordPressVersions(); + await getWordPressVersions(mockCore, 4); expect(mockCore.setOutput).toHaveBeenCalledWith('versions', JSON.stringify(['6.4', '6.3', '6.2', '6.1'])); }); @@ -120,10 +105,8 @@ describe('getWordPressVersions', () => { fetch.mockResolvedValue({ json: jest.fn().mockResolvedValue(mockApiResponse), }); - - mockCore.getInput.mockReturnValue('2'); - await getWordPressVersions(); + await getWordPressVersions(mockCore, 2); expect(console.log).toHaveBeenCalledWith('Found 2 WordPress versions:', ['6.4', '6.3']); }); @@ -133,9 +116,8 @@ describe('getWordPressVersions', () => { it('should handle fetch errors', async () => { const fetchError = new Error('Network error'); fetch.mockRejectedValue(fetchError); - mockCore.getInput.mockReturnValue('3'); - await getWordPressVersions(); + await getWordPressVersions(mockCore, 3); expect(mockCore.setFailed).toHaveBeenCalledWith('Failed to fetch WordPress versions: Network error'); expect(mockCore.setOutput).not.toHaveBeenCalled(); @@ -145,9 +127,8 @@ describe('getWordPressVersions', () => { fetch.mockResolvedValue({ json: jest.fn().mockRejectedValue(new Error('Invalid JSON')), }); - mockCore.getInput.mockReturnValue('3'); - await getWordPressVersions(); + await getWordPressVersions(mockCore, 3); expect(mockCore.setFailed).toHaveBeenCalledWith('Failed to fetch WordPress versions: Invalid JSON'); }); @@ -156,9 +137,8 @@ describe('getWordPressVersions', () => { fetch.mockResolvedValue({ json: jest.fn().mockResolvedValue({ someOtherData: 'test' }), }); - mockCore.getInput.mockReturnValue('3'); - await getWordPressVersions(); + await getWordPressVersions(mockCore, 3); expect(mockCore.setFailed).toHaveBeenCalledWith('No version offers found in WordPress API response'); }); @@ -167,9 +147,8 @@ describe('getWordPressVersions', () => { fetch.mockResolvedValue({ json: jest.fn().mockResolvedValue({ offers: [] }), }); - mockCore.getInput.mockReturnValue('3'); - await getWordPressVersions(); + await getWordPressVersions(mockCore, 3); expect(mockCore.setOutput).toHaveBeenCalledWith('versions', JSON.stringify([])); }); @@ -188,17 +167,23 @@ describe('getWordPressVersions', () => { json: jest.fn().mockResolvedValue(smallResponse), }); - mockCore.getInput.mockReturnValue('5'); - - await getWordPressVersions(); + await getWordPressVersions(mockCore, 5); expect(mockCore.setOutput).toHaveBeenCalledWith('versions', JSON.stringify(['6.4', '6.3'])); }); it('should handle zero as input', async () => { - mockCore.getInput.mockReturnValue('0'); + const mockResponse = { + offers: [ + { version: '6.4.1', response: 'autoupdate' }, + ] + }; - await getWordPressVersions(); + fetch.mockResolvedValue({ + json: jest.fn().mockResolvedValue(mockResponse), + }); + + await getWordPressVersions(mockCore, 0); expect(mockCore.setOutput).toHaveBeenCalledWith('versions', JSON.stringify([])); }); @@ -217,9 +202,7 @@ describe('getWordPressVersions', () => { json: jest.fn().mockResolvedValue(complexVersionResponse), }); - mockCore.getInput.mockReturnValue('4'); - - await getWordPressVersions(); + await getWordPressVersions(mockCore, 4); expect(mockCore.setOutput).toHaveBeenCalledWith('versions', JSON.stringify(['6.10', '6.9', '6.2'])); }); diff --git a/package.json b/package.json index 9cef9b9..a49706b 100644 --- a/package.json +++ b/package.json @@ -8,9 +8,7 @@ "test:watch": "jest --watch", "test:coverage": "jest --coverage" }, - "dependencies": { - "@actions/core": "^1.10.0" - }, + "devDependencies": { "jest": "^29.7.0" }, From 48ec9e7bdff4a62f6426ae14d665bc7e299fa9ea Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Wed, 29 Oct 2025 10:43:37 -0400 Subject: [PATCH 5/6] Fix CI --- .github/workflows/test.yml | 5 ++++- package.json | 1 - 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9b6edb5..ccda508 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -49,8 +49,11 @@ jobs: echo "✅ All versions match expected format" # Test that versions are in descending order - if ! echo "$versions" | jq -e '. == sort_by(. | split(".") | map(tonumber)) | reverse' > /dev/null; then + sorted_versions=$(echo "$versions" | jq -r 'sort_by(. | split(".") | map(tonumber)) | reverse') + if [ "$versions" != "$sorted_versions" ]; then echo "❌ ERROR: Versions are not in descending order" + echo "Expected: $sorted_versions" + echo "Actual: $versions" exit 1 fi echo "✅ Versions are in descending order" diff --git a/package.json b/package.json index a49706b..2969a4d 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,6 @@ "test:watch": "jest --watch", "test:coverage": "jest --coverage" }, - "devDependencies": { "jest": "^29.7.0" }, From 029fb09d97e71ea90102f5caaea688fe61b63b15 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Wed, 29 Oct 2025 10:44:26 -0400 Subject: [PATCH 6/6] Fix CI --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ccda508..58a052c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -49,7 +49,7 @@ jobs: echo "✅ All versions match expected format" # Test that versions are in descending order - sorted_versions=$(echo "$versions" | jq -r 'sort_by(. | split(".") | map(tonumber)) | reverse') + sorted_versions=$(echo "$versions" | jq -c 'sort_by(. | split(".") | map(tonumber)) | reverse') if [ "$versions" != "$sorted_versions" ]; then echo "❌ ERROR: Versions are not in descending order" echo "Expected: $sorted_versions"