Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

11.5 KB
Raw Blame
import * as Cloudflare from "@/Cloudflare";
import { CloudflareEnvironment } from "@/Cloudflare/CloudflareEnvironment";
import { findZoneByName } from "@/Cloudflare/Zone/lookup";
import * as Provider from "@/Provider";
import * as Test from "@/Test/Alchemy";
import * as managedTransforms from "@distilled.cloud/cloudflare/managed-transforms";
import { describe, expect } from "alchemy-test";
import * as Effect from "effect/Effect";
import { MinimumLogLevel } from "effect/References";
import * as Schedule from "effect/Schedule";
const { test } = Test.make({ providers: Cloudflare.providers() });

// The account-wide `list` test intermittently fails with a `Forbidden`
// (fresh-token 403 blip) because — unlike every other call in this suite —
// `provider.list()` is not wrapped in a `Forbidden` retry. Skipped by default;
// set RUN_MANAGED_TRANSFORMS_LIST_TEST=1 to run it. (Alternatively it could be
// fixed by retrying the typed `Forbidden` around `provider.list()`.)
const runManagedTransformsListTest =
  !!process.env.RUN_MANAGED_TRANSFORMS_LIST_TEST;

const logLevel = Effect.provideService(
  MinimumLogLevel,
  process.env.DEBUG ? "Debug" : "Info",
);

const zoneName =
  process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us";

const resolveZoneId = Effect.gen(function* () {
  const { accountId } = yield* yield* CloudflareEnvironment;
  const zone = yield* findZoneByName({ accountId, name: zoneName });
  if (!zone) {
    return yield* Effect.die(
      new Error(`zone "${zoneName}" not found in account`),
    );
  }
  return zone.id;
});

// The scoped API token the test harness mints propagates eventually-
// consistently across Cloudflare's edge — a fresh token intermittently 403s.
// Ride out the blips on the test's own out-of-band verification calls by
// retrying the typed `Forbidden` error (patched into the managed-transforms
// operations' error unions).
const forbiddenRetrySchedule = Schedule.exponential("500 millis");

const listTransforms = (zoneId: string) =>
  managedTransforms.listManagedTransforms({ zoneId }).pipe(
    Effect.retry({
      while: (e) => e._tag === "Forbidden",
      schedule: forbiddenRetrySchedule,
      times: 8,
    }),
  );

const enabledOf = (
  transforms: readonly { id: string; enabled: boolean }[] | null,
  id: string,
) => (transforms ?? []).find((t) => t.id === id)?.enabled;

// The free-plan test zone exposes exactly two managed transforms — both
// response transforms (`managed_request_headers` is `null`: every managed
// request transform is plan-gated above free). The test manages one and
// leaves the other untouched throughout.
const MANAGED_ID = "remove_x-powered-by_header";
const UNMANAGED_ID = "add_security_headers";

/**
 * Normalize the baseline so reruns are stable: force both transforms back
 * to disabled before (and after) the test body runs.
 */
const normalizeBaseline = (zoneId: string) =>
  Effect.gen(function* () {
    const live = yield* listTransforms(zoneId);
    const delta = [MANAGED_ID, UNMANAGED_ID]
      .filter((id) => enabledOf(live.managedResponseHeaders, id) === true)
      .map((id) => ({ id, enabled: false }));
    if (delta.length === 0) return;
    yield* managedTransforms
      .patchManagedTransform({
        zoneId,
        managedRequestHeaders: [],
        managedResponseHeaders: delta,
      })
      .pipe(
        Effect.retry({
          while: (e) => e._tag === "Forbidden",
          schedule: forbiddenRetrySchedule,
          times: 8,
        }),
      );
  });

describe.sequential("ManagedTransforms", () => {
  test.provider(
    "manages a named transform, updates in place, and restores it on destroy",
    (stack) =>
      Effect.gen(function* () {
        const zoneId = yield* resolveZoneId;

        yield* stack.destroy();
        yield* normalizeBaseline(zoneId);

        const baseline = yield* listTransforms(zoneId);
        expect(enabledOf(baseline.managedResponseHeaders, MANAGED_ID)).toBe(
          false,
        );
        expect(enabledOf(baseline.managedResponseHeaders, UNMANAGED_ID)).toBe(
          false,
        );

        yield* Effect.gen(function* () {
          // 1. Create (adopt the singleton) — enable one response transform.
          const created = yield* stack.deploy(
            Effect.gen(function* () {
              return yield* Cloudflare.ManagedTransforms.ManagedTransforms(
                "Transforms",
                {
                  zoneId,
                  responseHeaders: { [MANAGED_ID]: true },
                },
              );
            }),
          );
          expect(created.zoneId).toEqual(zoneId);
          expect(enabledOf(created.responseHeaders, MANAGED_ID)).toBe(true);
          // Snapshot captured the pre-management (normalized) state.
          expect(created.initialResponseHeaders[MANAGED_ID]).toBe(false);

          const live1 = yield* listTransforms(zoneId);
          expect(enabledOf(live1.managedResponseHeaders, MANAGED_ID)).toBe(
            true,
          );
          // The unmanaged transform is untouched.
          expect(enabledOf(live1.managedResponseHeaders, UNMANAGED_ID)).toBe(
            false,
          );

          // 2. Update in place — flip the managed transform off and take over
          //    the second one. Same identity (same zoneId), and the initial
          //    snapshot must remain sticky across updates.
          const updated = yield* stack.deploy(
            Effect.gen(function* () {
              return yield* Cloudflare.ManagedTransforms.ManagedTransforms(
                "Transforms",
                {
                  zoneId,
                  responseHeaders: {
                    [MANAGED_ID]: false,
                    [UNMANAGED_ID]: true,
                  },
                },
              );
            }),
          );
          expect(updated.zoneId).toEqual(zoneId);
          expect(enabledOf(updated.responseHeaders, MANAGED_ID)).toBe(false);
          expect(enabledOf(updated.responseHeaders, UNMANAGED_ID)).toBe(true);
          expect(updated.initialResponseHeaders[MANAGED_ID]).toBe(false);
          expect(updated.initialResponseHeaders[UNMANAGED_ID]).toBe(false);

          const live2 = yield* listTransforms(zoneId);
          expect(enabledOf(live2.managedResponseHeaders, MANAGED_ID)).toBe(
            false,
          );
          expect(enabledOf(live2.managedResponseHeaders, UNMANAGED_ID)).toBe(
            true,
          );

          // 3. Destroy — managed ids are restored to their snapshot values.
          yield* stack.destroy();

          const after = yield* listTransforms(zoneId);
          expect(enabledOf(after.managedResponseHeaders, MANAGED_ID)).toBe(
            false,
          );
          expect(enabledOf(after.managedResponseHeaders, UNMANAGED_ID)).toBe(
            false,
          );
        }).pipe(Effect.ensuring(normalizeBaseline(zoneId).pipe(Effect.ignore)));

        yield* stack.destroy();
      }).pipe(logLevel),
    { timeout: 240_000 },
  );

  test.provider(
    "destroy restores a transform that was enabled before management",
    (stack) =>
      Effect.gen(function* () {
        const zoneId = yield* resolveZoneId;

        yield* stack.destroy();
        yield* normalizeBaseline(zoneId);

        yield* Effect.gen(function* () {
          // Enable the transform out-of-band so the snapshot captures
          // `enabled: true` as the pre-management state.
          yield* managedTransforms
            .patchManagedTransform({
              zoneId,
              managedRequestHeaders: [],
              managedResponseHeaders: [{ id: MANAGED_ID, enabled: true }],
            })
            .pipe(
              Effect.retry({
                while: (e) => e._tag === "Forbidden",
                schedule: forbiddenRetrySchedule,
                times: 8,
              }),
            );

          // Manage it to disabled.
          const created = yield* stack.deploy(
            Effect.gen(function* () {
              return yield* Cloudflare.ManagedTransforms.ManagedTransforms(
                "Transforms",
                {
                  zoneId,
                  responseHeaders: { [MANAGED_ID]: false },
                },
              );
            }),
          );
          expect(created.initialResponseHeaders[MANAGED_ID]).toBe(true);
          expect(enabledOf(created.responseHeaders, MANAGED_ID)).toBe(false);

          const live = yield* listTransforms(zoneId);
          expect(enabledOf(live.managedResponseHeaders, MANAGED_ID)).toBe(
            false,
          );

          // Destroy — the transform goes back to enabled (its snapshot value).
          yield* stack.destroy();

          const after = yield* listTransforms(zoneId);
          expect(enabledOf(after.managedResponseHeaders, MANAGED_ID)).toBe(
            true,
          );
        }).pipe(Effect.ensuring(normalizeBaseline(zoneId).pipe(Effect.ignore)));

        yield* stack.destroy();
      }).pipe(logLevel),
    { timeout: 240_000 },
  );

  test.provider(
    "deploy with no transforms named adopts the singleton without writing",
    (stack) =>
      Effect.gen(function* () {
        const zoneId = yield* resolveZoneId;

        yield* stack.destroy();
        yield* normalizeBaseline(zoneId);

        const before = yield* listTransforms(zoneId);

        const adopted = yield* stack.deploy(
          Effect.gen(function* () {
            return yield* Cloudflare.ManagedTransforms.ManagedTransforms(
              "Transforms",
              {
                zoneId,
              },
            );
          }),
        );
        expect(adopted.zoneId).toEqual(zoneId);
        // Attributes surface the full observed catalog. Note: a fresh zone
        // reports `managed_request_headers: null` (normalized to `[]`) until
        // the first PATCH materializes the catalog, so only its array shape
        // is stable across runs.
        expect(Array.isArray(adopted.requestHeaders)).toBe(true);
        expect(adopted.responseHeaders.length).toBeGreaterThan(0);

        // Nothing named => no PATCH => live state unchanged.
        const afterDeploy = yield* listTransforms(zoneId);
        for (const t of before.managedResponseHeaders ?? []) {
          expect(enabledOf(afterDeploy.managedResponseHeaders, t.id)).toBe(
            t.enabled,
          );
        }

        // Destroy of an untouched singleton restores nothing and never errors.
        yield* stack.destroy();

        const afterDestroy = yield* listTransforms(zoneId);
        for (const t of before.managedResponseHeaders ?? []) {
          expect(enabledOf(afterDestroy.managedResponseHeaders, t.id)).toBe(
            t.enabled,
          );
        }
      }).pipe(logLevel),
    { timeout: 240_000 },
  );

  // Canonical `list()` test (zone-scoped singleton): there is no account-wide
  // API for the managed-transforms catalog, so `list()` enumerates every zone
  // via `listAllZones` and reads the singleton in each. Assert the result is
  // non-empty and contains the standing test zone.
  test.provider.skipIf(!runManagedTransformsListTest)(
    "list enumerates managed transforms across all zones",
    (stack) =>
      Effect.gen(function* () {
        const zoneId = yield* resolveZoneId;

        const provider = yield* Provider.findProvider(
          Cloudflare.ManagedTransforms.ManagedTransforms,
        );
        const all = yield* provider.list();

        expect(all.length).toBeGreaterThan(0);
        expect(all.some((t) => t.zoneId === zoneId)).toBe(true);

        // `stack` is unused here (the singleton always exists on every zone),
        // but keep the destroy bookend so the harness state stays clean.
        yield* stack.destroy();
      }).pipe(logLevel),
  );
});