Skip to content

Commit

Permalink
Add API client + e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicole White committed Aug 16, 2023
1 parent 6fe4d5d commit b565185
Show file tree
Hide file tree
Showing 8 changed files with 871 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,11 @@ jobs:
- name: Run tests
run: npm run test

- name: Run e2e tests
run: npm run test:e2e
env:
AUTOBLOCKS_API_KEY: ${{ secrets.DEMO_AUTOBLOCKS_API_KEY }}
AUTOBLOCKS_INGESTION_KEY: ${{ secrets.DEMO_AUTOBLOCKS_INGESTION_KEY }}

- name: Run build
run: npm run build
69 changes: 69 additions & 0 deletions e2e/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import crypto from 'crypto';
import { AutoblocksAPIClient, AutoblocksTracer } from '../src';

const { AUTOBLOCKS_API_KEY, AUTOBLOCKS_INGESTION_KEY } = process.env;

// We've created a view in our demo org to be used in CI tests.
// It has one filter, message == 'sdk.e2e', and its timespan is "last 1 hour"
const E2E_TESTS_VIEW_ID = 'clldzryfx0001i908okbbe5pf';
const E2E_TESTS_EXPECTED_MESSAGE = 'sdk.e2e';

const sleep = (seconds: number) =>
new Promise((resolve) => setTimeout(resolve, seconds * 1000));

const main = async () => {
if (!AUTOBLOCKS_API_KEY) {
throw new Error('AUTOBLOCKS_API_KEY env var is required.');
}
if (!AUTOBLOCKS_INGESTION_KEY) {
throw new Error('AUTOBLOCKS_INGESTION_KEY env var is required.');
}

const traceId = crypto.randomUUID();
const tracer = new AutoblocksTracer(AUTOBLOCKS_INGESTION_KEY, { traceId });
const client = new AutoblocksAPIClient(AUTOBLOCKS_API_KEY);

await tracer.sendEvent(E2E_TESTS_EXPECTED_MESSAGE);

// Make sure our view exists
const views = await client.getViews();
if (!views.some((view) => view.id === E2E_TESTS_VIEW_ID)) {
throw new Error(`View ${E2E_TESTS_VIEW_ID} not found!`);
}

// Find the test event we just sent
let cursor: string | undefined = undefined;
let retries = 10;

while (retries > 0) {
const { nextCursor, traces } = await client.getTracesFromView({
viewId: E2E_TESTS_VIEW_ID,
pageSize: 10,
cursor,
});

console.log('Found traces:');
console.log(traces.map((t) => t.id));

if (traces.some((t) => t.id === traceId)) {
console.log(`Found trace ${traceId}!`);
break;
}

retries--;

if (retries === 0) {
throw new Error(`Couldn't find trace ${traceId}.`);
}

cursor = nextCursor;

const sleepSeconds = 5;
console.log(
`Couldn't find trace ${traceId} yet, waiting ${sleepSeconds} seconds. ${retries} tries left.`,
);
await sleep(sleepSeconds);
}
};

main();
Loading

0 comments on commit b565185

Please sign in to comment.