sailfork / t3code-github / .repos/ alchemy-effect/ packages/ integration-tests/ test/ Axiom/ Monitor.test.ts

import * as Axiom from "@alchemy.run/axiom";
import { expect } from "@alchemy.run/test";
import * as Test from "@alchemy.run/test/Alchemy";
import * as Output from "alchemy/Output";
import * as Provider from "alchemy/Provider";
import * as Effect from "effect/Effect";
import { MinimumLogLevel } from "effect/References";

const { test } = Test.make({ providers: Axiom.providers() });

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

// Axiom credentials are resolved via the AuthProvider (env method reads
// AXIOM_TOKEN / AXIOM_API_KEY). When neither is present the suite can't talk
// to the real API, so skipIf-gate the live list test. On an entitled run the
// failure mode without creds is an `AuthError`:
//   "Axiom env credentials not found. Set AXIOM_TOKEN (or AXIOM_API_KEY)."
const hasAxiomCreds = !!(process.env.AXIOM_TOKEN || process.env.AXIOM_API_KEY);

// Deterministic names so re-runs reconcile the same resources rather than
// piling up duplicates. The monitor's APL query needs a real dataset target.
const DATASET_NAME = "alchemy-test-monitor-list";
const MONITOR_NAME = "alchemy-test-monitor-list";

test.provider.skipIf(!hasAxiomCreds)(
  "list enumerates the deployed monitor",
  (stack) =>
    Effect.gen(function* () {
      yield* stack.destroy();

      const deployed = yield* stack.deploy(
        Effect.gen(function* () {
          const dataset = yield* Axiom.Dataset("ListDataset", {
            name: DATASET_NAME,
          });
          return yield* Axiom.Monitor("ListMonitor", {
            name: MONITOR_NAME,
            description: "alchemy list() lifecycle op test",
            type: "Threshold",
            // Reference the dataset's OUTPUT so the monitor create waits for
            // the dataset create (a bare name string carries no graph edge —
            // Axiom rejects a monitor whose APL targets a missing dataset).
            aplQuery: Output.interpolate`['${dataset.name}'] | summarize count()`,
            intervalMinutes: 5,
            rangeMinutes: 5,
            operator: "Above",
            threshold: 1,
            notifierIds: [],
            alertOnNoData: false,
            resolvable: true,
          });
        }),
      );

      const provider = yield* Provider.findProvider(Axiom.Monitor);
      const all = yield* provider.list();

      // `getMonitors` returns the full account-wide list in one shot; the
      // freshly created monitor must be present, hydrated into the exact `read`
      // Attributes shape (so `.id` / `.type` are directly usable by `delete`).
      const found = all.find((m) => m.id === deployed.id);
      expect(found).toBeDefined();
      expect(found?.name).toEqual(MONITOR_NAME);
      expect(found?.type).toEqual("Threshold");

      yield* stack.destroy();
    }).pipe(logLevel),
  {
    tags: ["provider:axiom", "provider:axiom:dataset", "provider:axiom:monitor", "live"],
    timeout: 120_000,
  },
);