apify-test-tools 0.10.2-beta.7 → 0.10.2-beta.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,76 @@
1
+ import { describe, expect, it } from 'vitest';
2
+
3
+ import { checkRun } from '../../lib/run-checks.js';
4
+ import type { RunTestResult } from '../../lib/run-test-result.js';
5
+
6
+ const fakeRun = (overrides: { status?: string; log?: string; stats?: object; chargedEventCounts?: object } = {}) =>
7
+ ({
8
+ status: overrides.status ?? 'SUCCEEDED',
9
+ getRunInfo: async () => ({
10
+ stats: { durationMillis: 5_000 },
11
+ chargedEventCounts: overrides.chargedEventCounts,
12
+ }),
13
+ getDataset: async () => ({ items: new Array(10) }),
14
+ getStatistics: async () => ({
15
+ requestsFailed: 0,
16
+ requestsRetries: 1,
17
+ requestRetryHistogram: [5, 1],
18
+ ...overrides.stats,
19
+ }),
20
+ getLog: async () => overrides.log ?? 'all good',
21
+ }) as unknown as RunTestResult;
22
+
23
+ const failures = async (run: RunTestResult, options: Parameters<typeof checkRun>[1]) =>
24
+ (await checkRun(run, options)).filter(({ pass }) => !pass).map(({ name }) => name);
25
+
26
+ describe('checkRun()', () => {
27
+ it('passes a run matching the defaults', async () => {
28
+ expect(await failures(fakeRun(), { datasetItemCount: 10 })).toEqual([]);
29
+ });
30
+
31
+ it('reports every failing check at once', async () => {
32
+ const run = fakeRun({ status: 'FAILED', log: 'TypeError: x', stats: { requestsFailed: 2 } });
33
+ expect(await failures(run, { datasetItemCount: { min: 11 } })).toEqual([
34
+ 'status',
35
+ 'datasetItemCount',
36
+ 'failedRequests',
37
+ 'forbiddenLogs',
38
+ ]);
39
+ });
40
+
41
+ it('disables a check with null', async () => {
42
+ const run = fakeRun({ stats: { requestsFailed: 2 } });
43
+ expect(await failures(run, { datasetItemCount: 10, failedRequests: null })).toEqual([]);
44
+ });
45
+
46
+ it('treats explicitly undefined options as disabled checks', async () => {
47
+ const options = { datasetItemCount: 10, duration: undefined, maxRetriesPerRequest: undefined };
48
+ expect(await failures(fakeRun(), options)).toEqual([]);
49
+ });
50
+
51
+ it('checks the longest retry chain only when asked to', async () => {
52
+ expect(await failures(fakeRun(), { datasetItemCount: 10, maxRetriesPerRequest: 0 })).toEqual([
53
+ 'maxRetriesPerRequest',
54
+ ]);
55
+ expect(await failures(fakeRun(), { datasetItemCount: 10, maxRetriesPerRequest: 1 })).toEqual([]);
56
+ });
57
+
58
+ describe('chargedEventCounts', () => {
59
+ const run = fakeRun({ chargedEventCounts: { start: 1, item: 7 } });
60
+
61
+ it('passes matching counts and intervals', async () => {
62
+ const options = { datasetItemCount: 10, chargedEventCounts: { start: 1, item: { min: 5 } } };
63
+ expect(await failures(run, options)).toEqual([]);
64
+ });
65
+
66
+ it('expects unlisted charged events to not happen', async () => {
67
+ const options = { datasetItemCount: 10, chargedEventCounts: { start: 1 } as Record<string, number> };
68
+ expect(await failures(run, options)).toEqual(['chargedEventCounts.item']);
69
+ });
70
+
71
+ it('fails an expected event that was never charged', async () => {
72
+ const options = { datasetItemCount: 10, chargedEventCounts: { start: 1, item: 7, extra: { min: 0 } } };
73
+ expect(await failures(run, options)).toEqual(['chargedEventCounts.extra']);
74
+ });
75
+ });
76
+ });
@@ -0,0 +1,44 @@
1
+ import type { ActorRun, ApifyClient } from 'apify-client';
2
+ import { describe, expect, it, vi } from 'vitest';
3
+
4
+ import { RunTestResult } from '../../lib/run-test-result.js';
5
+
6
+ const run = { id: 'run', status: 'SUCCEEDED', defaultDatasetId: 'ds', defaultKeyValueStoreId: 'kvs' } as ActorRun;
7
+
8
+ describe('RunTestResult', () => {
9
+ it('fetches the log once, even when empty or requested concurrently', async () => {
10
+ const get = vi.fn(async () => '');
11
+ const client = { run: () => ({ log: () => ({ get }) }) } as unknown as ApifyClient;
12
+ const result = new RunTestResult(client, run);
13
+
14
+ await Promise.all([result.getLog(), result.getLog()]);
15
+ await result.getLog();
16
+
17
+ expect(get).toHaveBeenCalledTimes(1);
18
+ });
19
+
20
+ it('retries after a failed fetch', async () => {
21
+ const get = vi.fn().mockRejectedValueOnce(new Error('boom')).mockResolvedValueOnce('log');
22
+ const client = { run: () => ({ log: () => ({ get }) }) } as unknown as ApifyClient;
23
+ const result = new RunTestResult(client, run);
24
+
25
+ await expect(result.getLog()).rejects.toThrow('boom');
26
+ expect(await result.getLog()).toBe('log');
27
+ });
28
+
29
+ it('pages through the dataset instead of listing it in one response', async () => {
30
+ const total = 25_000;
31
+ const listItems = vi.fn(async ({ offset, limit }: { offset: number; limit: number }) => ({
32
+ total,
33
+ items: Array.from({ length: Math.min(limit, total - offset) }, (_, i) => offset + i),
34
+ }));
35
+ const client = { dataset: () => ({ listItems }) } as unknown as ApifyClient;
36
+ const result = new RunTestResult(client, run);
37
+
38
+ const { items } = await result.getDataset<number>();
39
+
40
+ expect(items).toHaveLength(total);
41
+ expect(items[24_999]).toBe(24_999);
42
+ expect(listItems).toHaveBeenCalledTimes(25);
43
+ });
44
+ });