iterate.com / cloud

git clone https://git.iterate.com/repos/cloud

cloud / internal-packages / installer / test / update-consent.mjsmain at aacbc74

// What an update from a deployment's card leaves off that a mind made today has, and the consent
// that its panel offers for it (src/update-consent.ts). Run: node test/update-consent.mjs
import assert from "node:assert/strict";
import { test } from "node:test";
import worker from "../worker/index.js";
import { SCOPES, TELEMETRY_SCOPE } from "../worker/scopes.js";
import { BOTH_CONSENT, leftOffOf } from "../src/update-consent.ts";

const stack = { telemetry: false, connected: false };
const plainAccess = { mayTurnOnTelemetry: false, mayConnectCloudflare: false };

test("an older stack and a plain access: the update leaves off telemetry and the connection", () => {
  assert.deepEqual(leftOffOf({ ...stack, ...plainAccess }), ["telemetry", "cloudflare"]);
});

test("a stack that has one of the two: the update leaves off the other alone", () => {
  assert.deepEqual(leftOffOf({ ...stack, telemetry: true, ...plainAccess }), ["cloudflare"]);
  assert.deepEqual(leftOffOf({ ...stack, connected: true, ...plainAccess }), ["telemetry"]);
  assert.deepEqual(leftOffOf({ telemetry: true, connected: true, ...plainAccess }), []);
});

test("an access from the consent for both: the update leaves nothing off, so no panel asks again", () => {
  const both = { mayTurnOnTelemetry: true, mayConnectCloudflare: true };
  assert.deepEqual(leftOffOf({ ...stack, ...both }), []);
  // an access from the consent for one of them: the panel asks for the other
  assert.deepEqual(leftOffOf({ ...stack, ...both, mayConnectCloudflare: false }), ["cloudflare"]);
  assert.deepEqual(leftOffOf({ ...stack, ...both, mayTurnOnTelemetry: false }), ["telemetry"]);
});

test("the panel's consent asks the token scope and keeps both wishes, as the wizard's does", async () => {
  const rows = new Map();
  const env = {
    CLIENT_ID: "client",
    SESSIONS: {
      get: async (key) => rows.get(key) ?? null,
      put: async (key, value) => void rows.set(key, value),
    },
  };
  const answer = await worker.fetch(new Request(`https://cloud.iterate.com${BOTH_CONSENT}`), env);
  const to = new URL(answer.headers.get("location"));
  assert.deepEqual(to.searchParams.get("scope").split(" "), [...SCOPES, TELEMETRY_SCOPE]);
  const { level, telemetry, cloudflare } = JSON.parse(
    rows.get(`state:${to.searchParams.get("state")}`),
  );
  assert.deepEqual(
    { level, telemetry, cloudflare },
    { level: "full", telemetry: true, cloudflare: true },
  );
});

test("a connection whose token an older release made is left off too, until the access may narrow it", () => {
  const older = { telemetry: true, connected: true, older: true };
  assert.deepEqual(leftOffOf({ ...older, ...plainAccess }), ["cloudflare"]);
  assert.deepEqual(leftOffOf({ ...older, ...plainAccess, mayConnectCloudflare: true }), []);
});