diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 5b45465d..390d186a 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -204,7 +204,7 @@ jobs: }, { "type": "mrkdwn", - "text": "*${{ github.event.sender.login }}* has merged this release." + "text": "*${{ github.event.sender.login }}* has triggered this release." } ] } diff --git a/packages/test-utils/src/index.ts b/packages/test-utils/src/index.ts index 0ac44f46..0def1ec0 100644 --- a/packages/test-utils/src/index.ts +++ b/packages/test-utils/src/index.ts @@ -92,6 +92,7 @@ export { liveAuth, liveCreate, liveDelete, + liveEventually, liveFirst, liveName, liveRequest, diff --git a/packages/test-utils/src/live.ts b/packages/test-utils/src/live.ts index a41b6408..bddd8ebd 100644 --- a/packages/test-utils/src/live.ts +++ b/packages/test-utils/src/live.ts @@ -56,6 +56,22 @@ export const liveFirst = async (type: string, query: Record = {} return body.data?.[0]?.id } +/** + * The value of a lookup once it satisfies a condition: the API lists a change + * (a renamed or deleted resource) a moment after the write that made it, so a + * lookup right after a command may still see the previous state. Returns the + * last value when the time is up, for the test's own assertion to report. + */ +export const liveEventually = async (lookup: () => Promise, done: (value: T) => boolean, timeout = 20_000, interval = 1000): Promise => { + const until = Date.now() + timeout + let value = await lookup() + while (!done(value) && Date.now() < until) { + await new Promise((resolve) => setTimeout(resolve, interval)) + value = await lookup() + } + return value +} + /** * A describe block that runs only with the test organization's credentials, * with a timeout fit for real API calls. diff --git a/plugins/tags/test/integration/tags-write.it.ts b/plugins/tags/test/integration/tags-write.it.ts index 9ed936fb..59cd5456 100644 --- a/plugins/tags/test/integration/tags-write.it.ts +++ b/plugins/tags/test/integration/tags-write.it.ts @@ -1,4 +1,4 @@ -import { describeLive, LIVE_RUN, liveAuth, liveCreate, liveDelete, liveFirst, liveName } from '@commercelayer/cli-test-utils' +import { describeLive, LIVE_RUN, liveAuth, liveCreate, liveDelete, liveEventually, liveFirst, liveName } from '@commercelayer/cli-test-utils' import { runCommand } from '@oclif/test' import { expect } from 'chai' @@ -9,6 +9,9 @@ const run = async (args: string[]): Promise => { } const tagId = (name: string) => liveFirst('tags', { 'filter[q][name_eq]': name }) +// The API lists a created, renamed or deleted tag a moment after the command +const tagListed = (name: string) => liveEventually(() => tagId(name), (id) => id !== undefined) +const tagGone = (name: string) => liveEventually(() => tagId(name), (id) => id === undefined) // A tag of this run on a customer of this run describeLive('tags: create, add, remove, update and delete', () => { @@ -26,7 +29,7 @@ describeLive('tags: create, add, remove, update and delete', () => { it('creates a tag', async () => { await run(['tags:create', '-n', name]) - expect(await tagId(name)).to.be.a('string') + expect(await tagListed(name)).to.be.a('string') }) it('tags a resource and finds it', async () => { @@ -41,12 +44,12 @@ describeLive('tags: create, add, remove, update and delete', () => { it('renames the tag', async () => { await run(['tags:update', name, '-n', renamed]) - expect(await tagId(renamed)).to.be.a('string') - expect(await tagId(name)).to.equal(undefined) + expect(await tagListed(renamed)).to.be.a('string') + expect(await tagGone(name)).to.equal(undefined) }) it('deletes the tag', async () => { await run(['tags:delete', '-n', renamed]) - expect(await tagId(renamed)).to.equal(undefined) + expect(await tagGone(renamed)).to.equal(undefined) }) })