Skip to content

feat: ad-hoc Actors built from repository files in testActor - #245

Open
janbuchar wants to merge 24 commits into
ccr-86660633-vf05vgfrom
throwaway-actors
Open

janbuchar wants to merge 24 commits into
ccr-86660633-vf05vgfrom
throwaway-actors

Conversation

@janbuchar

@janbuchar janbuchar commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

Lets testActor take an ad-hoc Actor: one that isn't in apify-test-tools.config.json and is built from files in the repository when the test first runs it. This is for library repositories (crawlee, apify-sdk-js, actor-scraper), whose tests need Actors built against the checkout rather than a published version. See "Ad-hoc Actors" in docs/lib.md.

  • defineActor(name, { source, token, memory, env, permissions, syncDelayMs }) describes the Actor; testActor(actor, …) builds it on the first run() and pins every run to that build. The Actor is created if missing and kept afterwards
  • token is explicit: the Actor is built, run and cleaned up with it, and lives under its account. An ad-hoc Actor is never in ACTOR_BUILDS, so it can't get a token the way other Actors do in v1. A missing token fails that Actor's tests only
  • the build goes through the same code as build-from-local: ActorSource collects the files, ApifyBuilder.buildFromLocalSource zips, uploads and builds version 0.98. build-from-local now uses both too. Those files moved to bin/from-local-files/
  • ActorSource.dirs(...) overlays directories, .pin(packs) points package.json at pnpm pack tarballs of the code under test (npm overrides or pnpm.overrides), .with(step) applies a custom step
  • inlineMain(filename, fn) is a step that ships a top-level function of the test file as the Actor's entry point. It's type-checked on its own with the project's tsc, so anything borrowed from the rest of the file fails as TS2304. A tsconfig.json that doesn't cover the generated file is an error too
  • builds from local source are safe to overlap, for build-from-local as well: each zip is stored under a name with its hash, and a build that started from somebody else's upload is detected through actVersion.tarballUrl, aborted and repeated. Ad-hoc runs pass memory and permission level themselves instead of reading them from the Actor
  • a local build is waited for and its zip deleted as soon as it is over, for build-from-local and ad-hoc Actors alike: the signed URL on the version would otherwise keep the source downloadable. Version 0.98 can't be rebuilt from the Console afterwards (closes Delete build source code after 'build-from-local' #274)
  • builds of ad-hoc Actors older than 3 days are deleted after the test file; delete-old-builds sweeps up zips an interrupted build left behind after a week, and the undated one from before zips were dated
  • syncDelayMs replaces the 20 s wait after a run for ad-hoc Actors; run() takes no input and then sends none; buildActor and pollUntil are exported

Notes for review

@janbuchar janbuchar added t-tooling Issues with this label are in the ownership of the tooling team. adhoc Ad-hoc unplanned task added during the sprint. labels Nov 9, 2026
@janbuchar janbuchar self-assigned this Nov 9, 2026
@janbuchar
janbuchar requested a review from barjin November 9, 2026 15:01
@metalwarrior665

Copy link
Copy Markdown
Member

Thanks. Considering this is sort of a library inside a library, I would say there are 3 options:

  1. Keep as in your PR. Some agents and readers might be confused that there is a separate way to do a similar thing.
  2. Move this to a separate repo/package, and we just export a few things that you might want to use us for.
  3. Reuse more of what we already have. This would integrate it with our code and thus increased its complexity but maybe we figure out how this would be useful for general public too. Also, you could make a step towards us maybe by precommitting whole Actors in the library repos (they can share Dockerfile and parts of src) and using apify-test-tools as is, instead of a new way of creating them inside tests.

I think 3 will be quite a bit of work unless we find a good angle so I'm leaning 2 but ofc understand that not having to maintain yet another package is good for you.

We will think it through

@janbuchar

janbuchar commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator Author

Thanks. Considering this is sort of a library inside a library, I would say there are 3 options:

Thanks @metalwarrior665 for the quick feedback. "Library inside a library" is fair, but the package already spans a vitest library, a CLI (get-affected-actors, build, build-from-local, delete-old-builds, notify) and a set of reusable GitHub workflows. A helper that builds the Actor under test sits next to build-from-local, which already uploads local source to build an Actor.

  1. Keep as in your PR. Some agents and readers might be confused that there is a separate way to do a similar thing.

Which part would read as a second way, creating the Actor or asserting on it? The assertion side is the existing one. The harness reuses RunTestResult, toFinishWith, the matchers and extendExpect, and the test it hands out mirrors testActor's context. The only new thing is how an Actor comes to exist, pre-registered or built from the checkout. That is a one-sentence distinction in the docs.

  1. Move this to a separate repo/package, and we just export a few things that you might want to use us for.

Two problems. "A few things" would in practice be RunTestResult, extendExpect/checkRun, collectNonIgnoredFiles and the delete-old-builds logic, so today's internals become public API with semver obligations, for one consumer. And we'd have two packages wrapping vitest and apify-client, drifting apart in versions and in where a new feature belongs.

Worth noting what a second consumer of the shared code already found: getStatistics was pinned to SDK_CRAWLER_STATISTICS_0 (#111, silently undefined on Crawlee v4), check-ignore blew the argv limit on any checkout with node_modules, and RunTestResult was documented as exported but not reachable at runtime. A separate package wouldn't have hit those.

  1. Reuse more of what we already have.

That's what I tried to do, within reason. If you see a concrete seam I missed, point at it.

Also, you could make a step towards us maybe by precommitting whole Actors in the library repos (they can share Dockerfile and parts of src) and using apify-test-tools as is, instead of a new way of creating them inside tests.

That doesn't work for a library because we need to pin Actor dependencies. The point of our tests is to run the Actor against the library code in the checkout (the PR branch) rather than a published version. A pre-registered Actor called from a test runs whatever was built last, so every test run has to build first, from source that carries the unpublished packages (tarballs plus overrides, so transitive dependencies resolve to them too).

Once every run builds with injected artifacts, a pre-existing Actor gives you nothing, and "only rebuild what changed" is moot because every change to the library changes every Actor.

For context, all three repos (crawlee, JS SDK, Python SDK) already build their test Actors from the checkout, each with its own hand-rolled runner. This PR exists to unify that mechanism so that adding a test, or moving one between repos, is cheap, and to reuse it for the JS client and actor-scraper next.

I think 3 will be quite a bit of work unless we find a good angle so I'm leaning 2 but ofc understand that not having to maintain yet another package is good for you.

It isn't only good for us. Splitting overlapping functionality across two packages owned by two teams is a net loss for both.

We will think it through

Appreciated. I know this landed out of nowhere, but the crawlee and SDK majors would benefit greatly from this, so I'd be grateful for a quick turnaround.

@janbuchar

janbuchar commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator Author

Notes from talking to @JuanGalilea:

  1. the main, previously unseen benefit, of this PR is that our libs can drop a lot of boilerplate (e.g. -4K LOC in crawlee — refactor: Restructure e2e tests using apify-test-tools crawlee#4375) and write e2e tests like https://github.com/apify/crawlee/blob/restructure-e2e-tests/test/e2e/cheerio-impit.test.ts
  2. also, e2e tests with throwaway Actors can be run "locally" (pushing to the developer's Apify account), shortening the feedback loop during development — no need to push, trigger CI, etc.
  3. while it is possible to override Actor dependencies using build-from-local, using this for libraries would add friction (maintaining a JSON of test Actors, keeping all the boilerplate, ...)
    • also, the throwaway Actor harness exercises much of the build-from-local code, which benefits that feature
    • in all fairness, the JSON config and Actor boilerplate could probably be generated on the consumer side (i.e., crawlee/sdk/...), but it would mean more copypasted boilerplate. With this PR, libraries only need harness.ts and global-setup.ts, once per repo

@JuanGalilea

Copy link
Copy Markdown
Contributor

additionaly, this might make it benefitial for our own actor-oriented libraries (like apify-actor-utils) so we can use this one to get better testing on those.

@janbuchar

Copy link
Copy Markdown
Collaborator Author

A key difference between build-from-local and our approach is that the former compiles dependency sources for every Actor, while the harness makes tarballs of the packages and installs them during the docker build.

On my machine, the tarball approach is roughly 8 times faster, which, projected to crawlee's e2e tests means roughly an hour of time spent re-installing dev dependencies and building crawlee.

@JuanGalilea JuanGalilea left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it all seems reasonable, my main gripes are on how its exposed to users.
This is more aimed to be used to test tools used to build actors instead of actors themselves, so i'd expose it as a completely separate thing (like its done with apify-test-tools/harness).

See comments below for what i think needs changing

Comment thread index.ts Outdated
@@ -1,2 +1,4 @@
export { describe, testActor, testStandbyActor, ExpectStatic } from './lib/lib.js';
export { RunTestResult } from './lib/run-test-result.js';
export { useHarness, type VitestHarness } from './lib/harness-vitest.js';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want this on the main export?

Harness is a pretty niche usecase imo

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I arrived at the same conclusion actually. The harness is gone and the ad-hoc actors are tested using the existing testActor interface.

Comment thread docs/lib.md Outdated
`TESTER_APIFY_TOKEN` can be the token of any account with enough memory for the runs. To run only some tests, pass a specific file instead of `test/platform`, or filter by
name with vitest's `-t <name>` when running vitest directly. See [Running locally](cli.md#running-locally) for the full flow.

## Throwaway Actors for library repositories

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would throw this whole section to its own doc file.

Harness is a pretty different usecase since its not about testing the actor itself in a way.

There we could explain the idea and use of it without confusing users that just want to test their actors.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the latest changes, this is about building ad-hoc Actors for later use via testActor. But I can still see it in its own doc file. Thoughts?

@JuanGalilea
JuanGalilea self-requested a review November 10, 2026 14:37

@JuanGalilea JuanGalilea left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

meant to request changes on the previous one 😓
skill issue much?

@metalwarrior665

Copy link
Copy Markdown
Member

Let's not make any changes yet pls. I want to focus now on clearing a few pending PRs and then we will sync about this in few days.

@metalwarrior665 metalwarrior665 mentioned this pull request Nov 12, 2026
4 tasks done
@janbuchar janbuchar changed the title feat: Throwaway Actor harness for testing libraries feat: ad-hoc Actors built from repository files in testActor Nov 12, 2026

@metalwarrior665 metalwarrior665 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is fine like this, commented a few potential refactors. The maintenance burden seems relatively small. Let's wait for others to review.

Comment thread bin/from-local-files/source-files.ts Resolved
Comment thread bin/source-files.ts Resolved
Comment thread lib/ad-hoc-actor.ts Resolved
Comment thread lib/run-test-result.ts Resolved
Comment thread lib/ad-hoc-actor.ts Resolved
Comment thread bin/build.ts Resolved

@JuanGalilea JuanGalilea left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all i could uncover, but it was taking too long to review :sadge:

Comment thread lib/ad-hoc-actor.ts Resolved
Comment thread lib/ad-hoc-actor.ts Resolved
Comment thread bin/from-local-files/actor-source.ts Resolved
Comment thread test/unit/ad-hoc.test.ts Resolved
@janbuchar

Copy link
Copy Markdown
Collaborator Author

Thanks for the feedback @JuanGalilea @metalwarrior665 @gytelio! I believe that everything has been addressed, please re-check.

@metalwarrior665

Copy link
Copy Markdown
Member

Pls wait with the merge after #252 which should merge on Monday as that one is already quite big release. We will communicate this separately.

Patai5 and others added 6 commits December 1, 2026 07:53
Discussed on slack:
https://apify.slack.com/archives/C0AMFHY27FB/p1794319738563179

---

Turns out that the file changed so much that git doesn't even think it's
the same one anymore (I renamed it) 😭 It is a lot of changes and mostly
clauded so let me know if anything looks wrong to you :P

Changes:
- **Duplicate check**: A cheap deduplication agent runs first, which
will make sure the issue gets deduplicated from the already opened GH
issues.
- The recurring issues will now be nicely and deterministically reported
in a **recurring** comment that will get edited each time and updated
with the new failures.
~- **Renamed** `public_platform-tests-claude-investigate-and-fix.yaml`
=> `public_platform-tests-claude-investigate.yaml`.~

It's a breaking change. ~Both the workflow is renamed~ and the NPM_TOKEN
removal is a breaking change.
add postfix comment option so teams can tag specific people (probably
themselves) on stale warning comments
@janbuchar
janbuchar changed the base branch from master to ccr-86660633-vf05vg December 3, 2026 14:27
@janbuchar
janbuchar added this pull request to stack #273 December 3, 2026 14:28

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

adhoc Ad-hoc unplanned task added during the sprint. t-tooling Issues with this label are in the ownership of the tooling team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Delete build source code after 'build-from-local'

5 participants