deploy.test.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * tests/3 — Deploy-to-clean-account smoke test.
  3. *
  4. * Asserts that a clean `wrangler deploy` of this Worker into an empty account
  5. * completes in under 4 seconds (Spin's MVP target). This test is gated on
  6. * CLOUDFLARE_API_TOKEN + CLOUDFLARE_ACCOUNT_ID being set in the environment;
  7. * skipped in normal CI runs.
  8. *
  9. * Run with:
  10. * CLOUDFLARE_API_TOKEN=... CLOUDFLARE_ACCOUNT_ID=... \
  11. * npx vitest run test/deploy.test.ts
  12. */
  13. import { describe, expect, it } from 'vitest';
  14. import { execSync, spawnSync } from 'node:child_process';
  15. const HAS_CREDS = Boolean(process.env.CLOUDFLARE_API_TOKEN && process.env.CLOUDFLARE_ACCOUNT_ID);
  16. const TARGET_MS = 4000;
  17. const describeIfCreds = HAS_CREDS ? describe : describe.skip;
  18. describeIfCreds('deploy-to-clean-account speed', () => {
  19. const workerName = `turnstile-siteverify-deploytest-${Date.now()}`;
  20. it(`completes wrangler deploy in under ${TARGET_MS}ms`, () => {
  21. const start = Date.now();
  22. const result = spawnSync(
  23. 'npx',
  24. ['wrangler', 'deploy', '--name', workerName, '--dry-run', '--outdir', '/tmp/spin-deploy-test'],
  25. {
  26. cwd: process.cwd(),
  27. env: { ...process.env },
  28. encoding: 'utf-8',
  29. },
  30. );
  31. const elapsed = Date.now() - start;
  32. if (result.status !== 0) {
  33. throw new Error(`wrangler deploy --dry-run failed:\n${result.stderr}`);
  34. }
  35. expect(elapsed).toBeLessThan(TARGET_MS);
  36. }, 30_000);
  37. it('cleans up the test Worker', () => {
  38. // Idempotent — if the dry-run never produced a real Worker, this is a no-op.
  39. execSync(`npx wrangler delete --name ${workerName} 2>&1 || true`, { stdio: 'ignore' });
  40. });
  41. });
  42. describe('deploy artifacts present', () => {
  43. it('wrangler.toml has the expected name', () => {
  44. const fs = require('node:fs');
  45. const text = fs.readFileSync('wrangler.toml', 'utf-8');
  46. expect(text).toMatch(/^name\s*=\s*"turnstile-siteverify"/m);
  47. });
  48. it('wrangler.toml configures the post-deploy assets directory', () => {
  49. const fs = require('node:fs');
  50. const text = fs.readFileSync('wrangler.toml', 'utf-8');
  51. expect(text).toMatch(/\[assets\]/);
  52. expect(text).toMatch(/directory\s*=\s*"\.\/public"/);
  53. });
  54. it('public/post-deploy.html exists', () => {
  55. const fs = require('node:fs');
  56. expect(fs.existsSync('public/post-deploy.html')).toBe(true);
  57. });
  58. });