From 611ae9c8e5cc30d3cf2e66d114cdc1242172240a Mon Sep 17 00:00:00 2001 From: Stephen Collings Date: Tue, 22 Aug 2023 17:23:31 +0100 Subject: [PATCH] fix: Updating autolinks with is_alphanumeric and unit test --- lib/plugins/autolinks.js | 7 +- test/unit/lib/plugins/autolinks.test.js | 147 ++++++++++++++++++++++++ 2 files changed, 149 insertions(+), 5 deletions(-) create mode 100644 test/unit/lib/plugins/autolinks.test.js diff --git a/lib/plugins/autolinks.js b/lib/plugins/autolinks.js index 4d64dbae..128e4021 100644 --- a/lib/plugins/autolinks.js +++ b/lib/plugins/autolinks.js @@ -13,8 +13,7 @@ module.exports = class Autolinks extends Diffable { } comparator (existing, attr) { - return existing.key_prefix === attr.key_prefix && - existing.url_template === attr.url_template + return existing.key_prefix === attr.key_prefix } changed (existing, attr) { @@ -24,9 +23,7 @@ module.exports = class Autolinks extends Diffable { const isAlphaNumericMatch = attr.is_alphanumeric === undefined ? existing.is_alphanumeric // === true, the default : attr.is_alphanumeric === existing.is_alphanumeric - return existing.key_prefix === attr.key_prefix && - existing.url_template !== attr.url_template && - !isAlphaNumericMatch + return existing.url_template !== attr.url_template || !isAlphaNumericMatch } async update (existing, attr) { diff --git a/test/unit/lib/plugins/autolinks.test.js b/test/unit/lib/plugins/autolinks.test.js new file mode 100644 index 00000000..63d82927 --- /dev/null +++ b/test/unit/lib/plugins/autolinks.test.js @@ -0,0 +1,147 @@ +const Autolinks = require('../../../../lib/plugins/autolinks') + +describe('Autolinks', () => { + const repo = { owner: 'owner', repo: 'repo' } + let github + + function configure (config) { + const log = { debug: jest.fn(), error: console.error } + const nop = false + const errors = [] + return new Autolinks(nop, github, repo, config, log, errors) + } + + beforeEach(() => { + github = { + repos: { + listAutolinks: jest.fn().mockResolvedValue([]), + createAutolink: jest.fn().mockResolvedValue(), + deleteAutolink: jest.fn().mockResolvedValue(), + } + } + }) + + describe('sync', () => { + it('syncs autolinks', () => { + const plugin = configure([ + { key_prefix: 'ADD-', url_template: 'https://test/' }, + { key_prefix: 'SAME-', url_template: 'https://test/' }, + { key_prefix: 'NEW_URL-', url_template: 'https://new-url/' }, + { key_prefix: 'SAME_ALPHA-UNDEFINED-', url_template: 'https://test/' }, + { key_prefix: 'SAME_ALPHA-FALSE-', url_template: 'https://test/', is_alphanumeric: false }, + { key_prefix: 'SAME_ALPHA-TRUE-', url_template: 'https://test/', is_alphanumeric: true }, + { key_prefix: 'NEW_ALPHA-UNDEFINED-', url_template: 'https://test/' }, + { key_prefix: 'NEW_ALPHA-FALSE-', url_template: 'https://test/', is_alphanumeric: false }, + { key_prefix: 'NEW_ALPHA-TRUE-', url_template: 'https://test/', is_alphanumeric: true }, + ]) + + github.repos.listAutolinks.mockResolvedValueOnce({ + data: [ + { id: '1', key_prefix: 'SAME-', url_template: 'https://test/', is_alphanumeric: true }, + { id: '2', key_prefix: 'REMOVE-', url_template: 'https://test/', is_alphanumeric: true }, + { id: '3', key_prefix: 'NEW_URL-', url_template: 'https://current-url/', is_alphanumeric: true }, + { id: '4', key_prefix: 'SAME_ALPHA-UNDEFINED-', url_template: 'https://test/', is_alphanumeric: true }, + { id: '5', key_prefix: 'SAME_ALPHA-FALSE-', url_template: 'https://test/', is_alphanumeric: false }, + { id: '6', key_prefix: 'SAME_ALPHA-TRUE-', url_template: 'https://test/', is_alphanumeric: true }, + { id: '7', key_prefix: 'NEW_ALPHA-UNDEFINED-', url_template: 'https://test/', is_alphanumeric: false }, + { id: '8', key_prefix: 'NEW_ALPHA-FALSE-', url_template: 'https://test/', is_alphanumeric: true }, + { id: '9', key_prefix: 'NEW_ALPHA-TRUE-', url_template: 'https://test/', is_alphanumeric: false }, + ] + }) + + return plugin.sync().then(() => { + expect(github.repos.createAutolink).toHaveBeenCalledWith({ + key_prefix: 'ADD-', + url_template: 'https://test/', + is_alphanumeric: true, + ...repo + }) + + expect(github.repos.deleteAutolink).toHaveBeenCalledWith({ + autolink_id: '2', + ...repo + }) + + expect(github.repos.deleteAutolink).toHaveBeenCalledWith({ + autolink_id: '3', + ...repo + }) + expect(github.repos.createAutolink).toHaveBeenCalledWith({ + key_prefix: 'NEW_URL-', + url_template: 'https://new-url/', + is_alphanumeric: true, + ...repo + }) + + expect(github.repos.deleteAutolink).not.toHaveBeenCalledWith({ + autolink_id: '4', + ...repo + }) + expect(github.repos.createAutolink).not.toHaveBeenCalledWith({ + key_prefix: 'SAME_ALPHA-UNDEFINED-', + url_template: 'https://test/', + is_alphanumeric: true, + ...repo + }) + + expect(github.repos.deleteAutolink).not.toHaveBeenCalledWith({ + autolink_id: '5', + ...repo + }) + expect(github.repos.createAutolink).not.toHaveBeenCalledWith({ + key_prefix: 'SAME_ALPHA-FALSE-', + url_template: 'https://test/', + is_alphanumeric: false, + ...repo + }) + + expect(github.repos.deleteAutolink).not.toHaveBeenCalledWith({ + autolink_id: '6', + ...repo + }) + expect(github.repos.createAutolink).not.toHaveBeenCalledWith({ + key_prefix: 'SAME_ALPHA-TRUE-', + url_template: 'https://test/', + is_alphanumeric: true, + ...repo + }) + + expect(github.repos.deleteAutolink).toHaveBeenCalledWith({ + autolink_id: '7', + ...repo + }) + expect(github.repos.createAutolink).toHaveBeenCalledWith({ + key_prefix: 'NEW_ALPHA-UNDEFINED-', + url_template: 'https://test/', + is_alphanumeric: true, + ...repo + }) + + expect(github.repos.deleteAutolink).toHaveBeenCalledWith({ + autolink_id: '8', + ...repo + }) + expect(github.repos.createAutolink).toHaveBeenCalledWith({ + key_prefix: 'NEW_ALPHA-FALSE-', + url_template: 'https://test/', + is_alphanumeric: false, + ...repo + }) + + expect(github.repos.deleteAutolink).toHaveBeenCalledWith({ + autolink_id: '9', + ...repo + }) + expect(github.repos.createAutolink).toHaveBeenCalledWith({ + key_prefix: 'NEW_ALPHA-TRUE-', + url_template: 'https://test/', + is_alphanumeric: true, + ...repo + }) + + expect(github.repos.deleteAutolink).toHaveBeenCalledTimes(5) + expect(github.repos.createAutolink).toHaveBeenCalledTimes(5) + }) + }) + }) +})