Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
]
}
Expand Down
1 change: 1 addition & 0 deletions packages/test-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export {
liveAuth,
liveCreate,
liveDelete,
liveEventually,
liveFirst,
liveName,
liveRequest,
Expand Down
16 changes: 16 additions & 0 deletions packages/test-utils/src/live.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ export const liveFirst = async (type: string, query: Record<string, string> = {}
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 <T>(lookup: () => Promise<T>, done: (value: T) => boolean, timeout = 20_000, interval = 1000): Promise<T> => {
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.
Expand Down
13 changes: 8 additions & 5 deletions plugins/tags/test/integration/tags-write.it.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -9,6 +9,9 @@ const run = async (args: string[]): Promise<string> => {
}

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', () => {
Expand All @@ -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 () => {
Expand All @@ -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)
})
})
Loading