apify-test-tools 0.9.1-beta.1 → 0.9.1-beta.11
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.
- package/.github/CODEOWNERS +1 -1
- package/.github/actionlint.yaml +12 -0
- package/.github/actions/checkout-restore-dependencies/action.yaml +94 -0
- package/.github/review-prompt.md +154 -0
- package/.github/scripts/check-major-tag-refs.mjs +72 -0
- package/.github/scripts/check-package-version-bump.mjs +74 -0
- package/.github/scripts/run-with-apify-tokens.mjs +104 -0
- package/.github/workflows/_check_code.yaml +16 -0
- package/.github/workflows/_move_major_tag.yaml +108 -0
- package/.github/workflows/_update_release_metadata.yaml +12 -0
- package/.github/workflows/manual_move_major_tag.yaml +26 -0
- package/.github/workflows/manual_release_stable.yaml +12 -0
- package/.github/workflows/on_master.yaml +11 -0
- package/.github/workflows/on_pull_request.yaml +49 -1
- package/.github/workflows/public_claude.yaml +101 -0
- package/.github/workflows/public_platform-tests-claude-investigate-and-fix.yaml +208 -0
- package/.github/workflows/public_platform-tests.yaml +63 -0
- package/.github/workflows/public_pr-build-test.yaml +175 -0
- package/.github/workflows/public_push-build-latest.yaml +44 -0
- package/.github/workflows/public_review.yaml +107 -0
- package/.github/workflows-package-version +1 -0
- package/.prettierignore +9 -0
- package/.vscode/settings.json +2 -1
- package/CHANGELOG.md +15 -0
- package/CONTRIBUTING.md +53 -2
- package/README.md +229 -36
- package/bin/build-from-local.ts +23 -28
- package/bin/build.ts +82 -70
- package/bin/main.ts +2 -1
- package/bin/types.ts +0 -11
- package/bin/utils/config/load-config.ts +133 -0
- package/bin/utils/config/parser.ts +89 -0
- package/bin/utils/config/structures/base.ts +65 -0
- package/bin/utils/config/structures/grouped.ts +40 -0
- package/bin/utils/config/structures/legacy.ts +22 -0
- package/bin/utils/json-file.ts +55 -0
- package/bin/utils.ts +5 -150
- package/dist/bin/build-from-local.d.ts +2 -2
- package/dist/bin/build-from-local.d.ts.map +1 -1
- package/dist/bin/build-from-local.js +20 -19
- package/dist/bin/build-from-local.js.map +1 -1
- package/dist/bin/build.d.ts +7 -11
- package/dist/bin/build.d.ts.map +1 -1
- package/dist/bin/build.js +51 -50
- package/dist/bin/build.js.map +1 -1
- package/dist/bin/main.d.ts.map +1 -1
- package/dist/bin/main.js +2 -1
- package/dist/bin/main.js.map +1 -1
- package/dist/bin/types.d.ts +0 -9
- package/dist/bin/types.d.ts.map +1 -1
- package/dist/bin/utils/config/load-config.d.ts +13 -0
- package/dist/bin/utils/config/load-config.d.ts.map +1 -0
- package/dist/bin/utils/config/load-config.js +99 -0
- package/dist/bin/utils/config/load-config.js.map +1 -0
- package/dist/bin/utils/config/parser.d.ts +19 -0
- package/dist/bin/utils/config/parser.d.ts.map +1 -0
- package/dist/bin/utils/config/parser.js +73 -0
- package/dist/bin/utils/config/parser.js.map +1 -0
- package/dist/bin/utils/config/structures/base.d.ts +36 -0
- package/dist/bin/utils/config/structures/base.d.ts.map +1 -0
- package/dist/bin/utils/config/structures/base.js +41 -0
- package/dist/bin/utils/config/structures/base.js.map +1 -0
- package/dist/bin/utils/config/structures/grouped.d.ts +6 -0
- package/dist/bin/utils/config/structures/grouped.d.ts.map +1 -0
- package/dist/bin/utils/config/structures/grouped.js +29 -0
- package/dist/bin/utils/config/structures/grouped.js.map +1 -0
- package/dist/bin/utils/config/structures/legacy.d.ts +6 -0
- package/dist/bin/utils/config/structures/legacy.d.ts.map +1 -0
- package/dist/bin/utils/config/structures/legacy.js +14 -0
- package/dist/bin/utils/config/structures/legacy.js.map +1 -0
- package/dist/bin/utils/json-file.d.ts +20 -0
- package/dist/bin/utils/json-file.d.ts.map +1 -0
- package/dist/bin/utils/json-file.js +42 -0
- package/dist/bin/utils/json-file.js.map +1 -0
- package/dist/bin/utils.d.ts +5 -8
- package/dist/bin/utils.d.ts.map +1 -1
- package/dist/bin/utils.js +4 -107
- package/dist/bin/utils.js.map +1 -1
- package/dist/lib/consts.d.ts +1 -0
- package/dist/lib/consts.d.ts.map +1 -1
- package/dist/lib/consts.js +3 -0
- package/dist/lib/consts.js.map +1 -1
- package/dist/lib/lib.d.ts +10 -0
- package/dist/lib/lib.d.ts.map +1 -1
- package/dist/lib/lib.js +24 -18
- package/dist/lib/lib.js.map +1 -1
- package/dist/lib/types.d.ts +11 -3
- package/dist/lib/types.d.ts.map +1 -1
- package/dist/test/unit/bin/utils/config/load-config.test.d.ts +2 -0
- package/dist/test/unit/bin/utils/config/load-config.test.d.ts.map +1 -0
- package/dist/test/unit/bin/utils/config/load-config.test.js +139 -0
- package/dist/test/unit/bin/utils/config/load-config.test.js.map +1 -0
- package/dist/test/unit/bin/utils/config/parser.test.d.ts +2 -0
- package/dist/test/unit/bin/utils/config/parser.test.d.ts.map +1 -0
- package/dist/test/unit/bin/utils/config/parser.test.js +129 -0
- package/dist/test/unit/bin/utils/config/parser.test.js.map +1 -0
- package/dist/test/unit/bin/utils/config/structures/base.test.d.ts +2 -0
- package/dist/test/unit/bin/utils/config/structures/base.test.d.ts.map +1 -0
- package/dist/test/unit/bin/utils/config/structures/base.test.js +74 -0
- package/dist/test/unit/bin/utils/config/structures/base.test.js.map +1 -0
- package/dist/test/unit/bin/utils/config/structures/grouped.test.d.ts +2 -0
- package/dist/test/unit/bin/utils/config/structures/grouped.test.d.ts.map +1 -0
- package/dist/test/unit/bin/utils/config/structures/grouped.test.js +75 -0
- package/dist/test/unit/bin/utils/config/structures/grouped.test.js.map +1 -0
- package/dist/test/unit/bin/utils/config/structures/legacy.test.d.ts +2 -0
- package/dist/test/unit/bin/utils/config/structures/legacy.test.d.ts.map +1 -0
- package/dist/test/unit/bin/utils/config/structures/legacy.test.js +63 -0
- package/dist/test/unit/bin/utils/config/structures/legacy.test.js.map +1 -0
- package/dist/test/unit/bin/utils/files.test.d.ts +2 -0
- package/dist/test/unit/bin/utils/files.test.d.ts.map +1 -0
- package/dist/test/unit/bin/utils/files.test.js +117 -0
- package/dist/test/unit/bin/utils/files.test.js.map +1 -0
- package/dist/test/unit/lib.test.d.ts +2 -0
- package/dist/test/unit/lib.test.d.ts.map +1 -0
- package/dist/test/unit/lib.test.js +37 -0
- package/dist/test/unit/lib.test.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/lib/consts.ts +4 -0
- package/lib/lib.ts +32 -20
- package/lib/types.ts +11 -3
- package/package.json +5 -2
- package/test/fixtures/bin/utils/files/array.json +1 -0
- package/test/fixtures/bin/utils/files/comments.jsonc +4 -0
- package/test/fixtures/bin/utils/files/directory.json/.gitkeep +0 -0
- package/test/fixtures/bin/utils/files/empty-object.json +1 -0
- package/test/fixtures/bin/utils/files/empty.json +0 -0
- package/test/fixtures/bin/utils/files/malformed.json +1 -0
- package/test/fixtures/bin/utils/files/nested.json +6 -0
- package/test/fixtures/bin/utils/files/no-json-extension.actorrc +1 -0
- package/test/fixtures/bin/utils/files/null.json +1 -0
- package/test/fixtures/bin/utils/files/number.json +1 -0
- package/test/fixtures/bin/utils/files/unreadable.json +1 -0
- package/test/tsconfig.json +4 -2
- package/test/unit/bin/utils/config/load-config.test.ts +180 -0
- package/test/unit/bin/utils/config/parser.test.ts +158 -0
- package/test/unit/bin/utils/config/structures/base.test.ts +83 -0
- package/test/unit/bin/utils/config/structures/grouped.test.ts +90 -0
- package/test/unit/bin/utils/config/structures/legacy.test.ts +78 -0
- package/test/unit/bin/utils/files.test.ts +158 -0
- package/test/unit/lib.test.ts +53 -0
- package/dist/test/unit/bin/utils.test.d.ts +0 -2
- package/dist/test/unit/bin/utils.test.d.ts.map +0 -1
- package/dist/test/unit/bin/utils.test.js +0 -321
- package/dist/test/unit/bin/utils.test.js.map +0 -1
- package/test/unit/bin/utils.test.ts +0 -381
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { GROUPED_PARSER } from '../../../../../../bin/utils/config/structures/grouped.js';
|
|
4
|
+
|
|
5
|
+
const actor = (fields: Record<string, unknown> = {}) => ({
|
|
6
|
+
folder: 'actors/shopify',
|
|
7
|
+
actorFullName: 'myteam/shopify',
|
|
8
|
+
...fields,
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
const group = (fields: Record<string, unknown> = {}) => ({
|
|
12
|
+
actors: [actor()],
|
|
13
|
+
tokenEnvVar: 'APIFY_TOKEN',
|
|
14
|
+
...fields,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
describe('GROUPED_PARSER', () => {
|
|
18
|
+
it('flattens every group into one list, handing each actor its group’s settings', () => {
|
|
19
|
+
expect(
|
|
20
|
+
GROUPED_PARSER.parse({
|
|
21
|
+
groups: {
|
|
22
|
+
myteam: {
|
|
23
|
+
actors: [actor(), actor({ folder: 'actors/email', actorFullName: 'myteam/email' })],
|
|
24
|
+
tokenEnvVar: 'APIFY_TOKEN_MYTEAM',
|
|
25
|
+
overrideActorContext: ['packages'],
|
|
26
|
+
},
|
|
27
|
+
other: { actors: [actor({ folder: 'actors/x', actorFullName: 'other/x' })], tokenEnvVar: 'TOKEN' },
|
|
28
|
+
},
|
|
29
|
+
}),
|
|
30
|
+
).toEqual([
|
|
31
|
+
{
|
|
32
|
+
folder: 'actors/shopify',
|
|
33
|
+
actorFullName: 'myteam/shopify',
|
|
34
|
+
tokenEnvVar: 'APIFY_TOKEN_MYTEAM',
|
|
35
|
+
overrideActorContext: ['packages'],
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
folder: 'actors/email',
|
|
39
|
+
actorFullName: 'myteam/email',
|
|
40
|
+
tokenEnvVar: 'APIFY_TOKEN_MYTEAM',
|
|
41
|
+
overrideActorContext: ['packages'],
|
|
42
|
+
},
|
|
43
|
+
{ folder: 'actors/x', actorFullName: 'other/x', tokenEnvVar: 'TOKEN', overrideActorContext: undefined },
|
|
44
|
+
]);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('ignores the group key entirely', () => {
|
|
48
|
+
expect(GROUPED_PARSER.parse({ groups: { 'not-a-folder-at-all': group() } })).toEqual([
|
|
49
|
+
{ folder: 'actors/shopify', actorFullName: 'myteam/shopify', tokenEnvVar: 'APIFY_TOKEN' },
|
|
50
|
+
]);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('allows actors to override some fields', () => {
|
|
54
|
+
expect(
|
|
55
|
+
GROUPED_PARSER.parse({ groups: { myteam: group({ actors: [actor({ tokenEnvVar: 'FROM_ACTOR' })] }) } }),
|
|
56
|
+
).toEqual([{ folder: 'actors/shopify', actorFullName: 'myteam/shopify', tokenEnvVar: 'FROM_ACTOR' }]);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("returns the entries as written — normalizing is not this layer's job", () => {
|
|
60
|
+
const raw = actor({ folder: 'actors/shopify/' });
|
|
61
|
+
|
|
62
|
+
expect(
|
|
63
|
+
GROUPED_PARSER.parse({ groups: { myteam: group({ actors: [raw], overrideActorContext: ['packages/'] }) } }),
|
|
64
|
+
).toEqual([{ ...raw, tokenEnvVar: 'APIFY_TOKEN', overrideActorContext: ['packages/'] }]);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('rejects a config with no groups (and thus no actors)', () => {
|
|
68
|
+
expect(() => GROUPED_PARSER.parse({})).toThrow();
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('reports problems from every group at once instead of only the first', () => {
|
|
72
|
+
const message = (() => {
|
|
73
|
+
try {
|
|
74
|
+
GROUPED_PARSER.parse({
|
|
75
|
+
groups: {
|
|
76
|
+
myteam: { actors: [actor({ folder: 123 })], tokenEnvVar: 'APIFY_TOKEN' },
|
|
77
|
+
other: { actors: [actor({ actorFullName: 'nope' })] },
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
throw new Error('Function should have thrown');
|
|
81
|
+
} catch (err) {
|
|
82
|
+
return (err as Error).message;
|
|
83
|
+
}
|
|
84
|
+
})();
|
|
85
|
+
|
|
86
|
+
expect(message).toContain('myteam.actors[0].folder');
|
|
87
|
+
expect(message).toContain('other.actors[0].actorFullName');
|
|
88
|
+
expect(message).toContain('other.tokenEnvVar');
|
|
89
|
+
});
|
|
90
|
+
});
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { LEGACY_PARSER } from '../../../../../../bin/utils/config/structures/legacy.js';
|
|
4
|
+
|
|
5
|
+
const entry = (fields: Record<string, unknown> = {}) => ({
|
|
6
|
+
folder: 'actors/shopify',
|
|
7
|
+
actorFullName: 'myteam/shopify',
|
|
8
|
+
tokenEnvVar: 'APIFY_TOKEN',
|
|
9
|
+
...fields,
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
describe('LEGACY_PARSER', () => {
|
|
13
|
+
it("returns the entries as written — normalizing is not this layer's job", () => {
|
|
14
|
+
const raw = entry({ folder: 'actors/shopify/', overrideActorContext: ['packages/'] });
|
|
15
|
+
|
|
16
|
+
expect(LEGACY_PARSER.parse({ actors: [raw] })).toEqual([raw]);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('drops the "mode" discriminator rather than choking on it', () => {
|
|
20
|
+
expect(LEGACY_PARSER.parse({ mode: 'legacy', actors: [entry()] })).toEqual([entry()]);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('rejects a file with no "actors" array', () => {
|
|
24
|
+
expect(() => LEGACY_PARSER.parse({ notActors: [] })).toThrow(/expected array[\s\S]*at actors/);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('rejects an empty "actors" array', () => {
|
|
28
|
+
expect(() => LEGACY_PARSER.parse({ actors: [] })).toThrow(/Too small/);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it.each([
|
|
32
|
+
['folder is missing', { folder: undefined }, /at actors\[0\]\.folder/],
|
|
33
|
+
['folder is not a string', { folder: 123 }, /at actors\[0\]\.folder/],
|
|
34
|
+
['tokenEnvVar is missing', { tokenEnvVar: undefined }, /at actors\[0\]\.tokenEnvVar/],
|
|
35
|
+
['overrideActorContext is not an array', { overrideActorContext: 'packages' }, /expected array/],
|
|
36
|
+
['overrideActorContext holds non-strings', { overrideActorContext: [123] }, /overrideActorContext\[0\]/],
|
|
37
|
+
])('rejects an entry where %s', (_name, fields, expected) => {
|
|
38
|
+
expect(() => LEGACY_PARSER.parse({ actors: [entry(fields)] })).toThrow(expected);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// The "owner/name" halves are checked against the platform's own username/actor-name rules.
|
|
42
|
+
it.each([
|
|
43
|
+
['no slash', 'shopify-scraper'],
|
|
44
|
+
['an empty owner', '/shopify'],
|
|
45
|
+
['an empty name', 'myteam/'],
|
|
46
|
+
['more than two halves', 'myteam/shopify/extra'],
|
|
47
|
+
])('rejects an actorFullName with %s', (_name, actorFullName) => {
|
|
48
|
+
expect(() => LEGACY_PARSER.parse({ actors: [entry({ actorFullName })] })).toThrow(
|
|
49
|
+
/at actors\[0\]\.actorFullName/,
|
|
50
|
+
);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Uppercase owners are legal on the platform; ./base.test.ts covers why that is easy to lose.
|
|
54
|
+
it.each([['myteam/shopify'], ['my.team/web-scraper'], ['my_team/a1'], ['apify/web-scraper'], ['MyTeam/shopify']])(
|
|
55
|
+
'accepts the actorFullName %s',
|
|
56
|
+
(actorFullName) => {
|
|
57
|
+
expect(() => LEGACY_PARSER.parse({ actors: [entry({ actorFullName })] })).not.toThrow();
|
|
58
|
+
},
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
it('reports every invalid field at once instead of only the first', () => {
|
|
62
|
+
const message = (() => {
|
|
63
|
+
try {
|
|
64
|
+
LEGACY_PARSER.parse({
|
|
65
|
+
actors: [{ folder: 123, actorFullName: 'shopify', overrideActorContext: 'nope' }],
|
|
66
|
+
});
|
|
67
|
+
throw new Error('Function should have thrown');
|
|
68
|
+
} catch (err) {
|
|
69
|
+
return (err as Error).message;
|
|
70
|
+
}
|
|
71
|
+
})();
|
|
72
|
+
|
|
73
|
+
expect(message).toContain('actors[0].folder');
|
|
74
|
+
expect(message).toContain('actors[0].actorFullName');
|
|
75
|
+
expect(message).toContain('actors[0].tokenEnvVar');
|
|
76
|
+
expect(message).toContain('actors[0].overrideActorContext');
|
|
77
|
+
});
|
|
78
|
+
});
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
|
|
5
|
+
import { afterEach, describe, expect, it } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import { safeReadJsonObjectFile } from '../../../../bin/utils/json-file.js';
|
|
8
|
+
|
|
9
|
+
// These tests read real fixture files rather than mocking `node:fs`: the whole point of
|
|
10
|
+
// `safeReadJsonObjectFile` is how it reacts to real stat/readFile outcomes (missing path, directory,
|
|
11
|
+
// symlink, unreadable file), so mocking those away would only assert the mock back to itself.
|
|
12
|
+
const FIXTURE_DIR = fileURLToPath(new URL('../../../fixtures/bin/utils/files/', import.meta.url));
|
|
13
|
+
|
|
14
|
+
const fixture = (name: string) => path.join(FIXTURE_DIR, name);
|
|
15
|
+
|
|
16
|
+
const NESTED_FIXTURE = {
|
|
17
|
+
storages: { datasets: { default: { title: 'Default' } } },
|
|
18
|
+
overrideActorContext: ['../shared', '.'],
|
|
19
|
+
changelog: null,
|
|
20
|
+
nested: [{ deep: [1, 2, { deeper: true }] }],
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
describe('safeReadJsonObjectFile', () => {
|
|
24
|
+
describe('valid JSON objects', () => {
|
|
25
|
+
it('returns the parsed contents, preserving nested objects, arrays and null values', () => {
|
|
26
|
+
const result = safeReadJsonObjectFile(fixture('nested.json'));
|
|
27
|
+
|
|
28
|
+
expect(result).toStrictEqual({ success: true, contents: NESTED_FIXTURE });
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('accepts an empty JSON object', () => {
|
|
32
|
+
const result = safeReadJsonObjectFile(fixture('empty-object.json'));
|
|
33
|
+
|
|
34
|
+
expect(result).toStrictEqual({ success: true, contents: {} });
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('reads files that are not named *.json', () => {
|
|
38
|
+
const result = safeReadJsonObjectFile(fixture('no-json-extension.actorrc'));
|
|
39
|
+
|
|
40
|
+
expect(result).toStrictEqual({ success: true, contents: { a: 1 } });
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe('filesystem-level handling', () => {
|
|
45
|
+
it('fails when the path does not exist', () => {
|
|
46
|
+
const filePath = fixture('does-not-exist.json');
|
|
47
|
+
|
|
48
|
+
const result = safeReadJsonObjectFile(filePath);
|
|
49
|
+
|
|
50
|
+
expect(result).toMatchObject({
|
|
51
|
+
success: false,
|
|
52
|
+
reason: 'unreadable',
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// The fixture is named `directory.json` on purpose — the check is `stat().isFile()`, not the extension.
|
|
57
|
+
it('fails when the path is a directory', () => {
|
|
58
|
+
const filePath = fixture('directory.json');
|
|
59
|
+
|
|
60
|
+
const result = safeReadJsonObjectFile(filePath);
|
|
61
|
+
|
|
62
|
+
expect(result).toMatchObject({
|
|
63
|
+
success: false,
|
|
64
|
+
reason: 'not-a-file',
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// Regression guard: `lstat` would report the link itself as "not a file" and reject it, even
|
|
69
|
+
// though `readFile` follows symlinks and would have read the target without complaint.
|
|
70
|
+
it('follows a symlink to its target file', () => {
|
|
71
|
+
const result = safeReadJsonObjectFile(fixture('symlink-to-valid.json'));
|
|
72
|
+
|
|
73
|
+
expect(result).toStrictEqual({ success: true, contents: NESTED_FIXTURE });
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// Git only tracks the executable bit, so this is the one case whose fixture cannot carry its own
|
|
77
|
+
// mode — `unreadable.json` is committed readable and stripped of permissions just for this test.
|
|
78
|
+
describe('unreadable file', () => {
|
|
79
|
+
const filePath = fixture('unreadable.json');
|
|
80
|
+
|
|
81
|
+
afterEach(async () => fs.chmod(filePath, 0o644));
|
|
82
|
+
|
|
83
|
+
it.runIf(process.getuid?.() !== 0)('fails when the file exists but cannot be read', async () => {
|
|
84
|
+
await fs.chmod(filePath, 0o000);
|
|
85
|
+
|
|
86
|
+
const result = safeReadJsonObjectFile(filePath);
|
|
87
|
+
|
|
88
|
+
expect(result).toMatchObject({
|
|
89
|
+
success: false,
|
|
90
|
+
reason: 'unreadable',
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
describe('JSON that is not an object', () => {
|
|
97
|
+
it('rejects a JSON array', () => {
|
|
98
|
+
const filePath = fixture('array.json');
|
|
99
|
+
|
|
100
|
+
const result = safeReadJsonObjectFile(filePath);
|
|
101
|
+
|
|
102
|
+
expect(result).toMatchObject({
|
|
103
|
+
success: false,
|
|
104
|
+
reason: 'not-an-object',
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('rejects JSON null', () => {
|
|
109
|
+
const filePath = fixture('null.json');
|
|
110
|
+
|
|
111
|
+
const result = safeReadJsonObjectFile(filePath);
|
|
112
|
+
|
|
113
|
+
expect(result).toMatchObject({
|
|
114
|
+
success: false,
|
|
115
|
+
reason: 'not-an-object',
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('rejects a JSON primitive', () => {
|
|
120
|
+
const filePath = fixture('number.json');
|
|
121
|
+
|
|
122
|
+
const result = safeReadJsonObjectFile(filePath);
|
|
123
|
+
|
|
124
|
+
expect(result).toMatchObject({
|
|
125
|
+
success: false,
|
|
126
|
+
reason: 'not-an-object',
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
describe('unparseable files', () => {
|
|
132
|
+
it('fails on malformed JSON', () => {
|
|
133
|
+
const filePath = fixture('malformed.json');
|
|
134
|
+
|
|
135
|
+
const result = safeReadJsonObjectFile(filePath);
|
|
136
|
+
|
|
137
|
+
expect(result).toMatchObject({ success: false, reason: 'invalid-json' });
|
|
138
|
+
expect((result as { failure: Error }).failure.message).toContain(`Failed to parse ${filePath}`);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('fails on an empty file', () => {
|
|
142
|
+
const filePath = fixture('empty.json');
|
|
143
|
+
|
|
144
|
+
const result = safeReadJsonObjectFile(filePath);
|
|
145
|
+
|
|
146
|
+
expect(result).toMatchObject({ success: false, reason: 'invalid-json' });
|
|
147
|
+
expect((result as { failure: Error }).failure.message).toContain(`Failed to parse ${filePath}`);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it.fails('reads jsonc', () => {
|
|
151
|
+
const filePath = fixture('comments.jsonc');
|
|
152
|
+
|
|
153
|
+
const result = safeReadJsonObjectFile(filePath);
|
|
154
|
+
|
|
155
|
+
expect(result).toMatchObject({ success: true, contents: { a: 3 } });
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
});
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { Actor, ActorClient, ActorRun } from 'apify-client';
|
|
2
|
+
import { ApifyClient } from 'apify-client';
|
|
3
|
+
import type { TestContext } from 'vitest';
|
|
4
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
5
|
+
|
|
6
|
+
import { DEFAULT_TEST_ACTOR_TIMEOUT_SECS } from '../../lib/consts.js';
|
|
7
|
+
import { _private } from '../../lib/lib.js';
|
|
8
|
+
import * as UtilsModule from '../../lib/utils.js';
|
|
9
|
+
|
|
10
|
+
const { createStartRunFn } = _private;
|
|
11
|
+
|
|
12
|
+
describe('createStartRunFn()', () => {
|
|
13
|
+
const actorCallMock = vi.fn(async () => Promise.resolve({ id: 'fake-run-id' } as ActorRun));
|
|
14
|
+
const testContext = { task: {}, annotate: vi.fn() as TestContext['annotate'] } as TestContext;
|
|
15
|
+
|
|
16
|
+
vi.spyOn(UtilsModule, 'sleep').mockResolvedValue(undefined);
|
|
17
|
+
|
|
18
|
+
beforeEach(vi.clearAllMocks);
|
|
19
|
+
|
|
20
|
+
it('should apply default timeout to actor runs', async () => {
|
|
21
|
+
// Arrange
|
|
22
|
+
|
|
23
|
+
vi.spyOn(ApifyClient.prototype, 'actor').mockReturnValue({
|
|
24
|
+
call: actorCallMock as ActorClient['call'],
|
|
25
|
+
get: vi.fn(async () => ({ defaultRunOptions: {} }) as Actor) as ActorClient['get'],
|
|
26
|
+
} as ActorClient);
|
|
27
|
+
|
|
28
|
+
// Act
|
|
29
|
+
await createStartRunFn('123', testContext)({ input: {} });
|
|
30
|
+
|
|
31
|
+
// Assert
|
|
32
|
+
expect(actorCallMock).toHaveBeenCalledTimes(1);
|
|
33
|
+
expect(actorCallMock).toHaveBeenCalledWith(
|
|
34
|
+
{},
|
|
35
|
+
{ build: undefined, log: null, timeout: DEFAULT_TEST_ACTOR_TIMEOUT_SECS },
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('should respect the default actor run timeout if less', async () => {
|
|
40
|
+
// Arrange
|
|
41
|
+
vi.spyOn(ApifyClient.prototype, 'actor').mockReturnValue({
|
|
42
|
+
call: actorCallMock as ActorClient['call'],
|
|
43
|
+
get: vi.fn(async () => ({ defaultRunOptions: { timeoutSecs: 60 } }) as Actor) as ActorClient['get'],
|
|
44
|
+
} as ActorClient);
|
|
45
|
+
|
|
46
|
+
// Act
|
|
47
|
+
await createStartRunFn('123', testContext)({ input: {} });
|
|
48
|
+
|
|
49
|
+
// Assert
|
|
50
|
+
expect(actorCallMock).toHaveBeenCalledTimes(1);
|
|
51
|
+
expect(actorCallMock).toHaveBeenCalledWith({}, { build: undefined, log: null, timeout: 60 });
|
|
52
|
+
});
|
|
53
|
+
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.test.d.ts","sourceRoot":"","sources":["../../../../test/unit/bin/utils.test.ts"],"names":[],"mappings":""}
|