~ knotless Search/
↑↓ move↵ open⇧↵ all results

johnandersen777.bsky.social/socialweb-computer-console

console.socialweb.computer: kcp, kubectl and ssh compiled to wasm in a browser tab with a terminal; a kcp fork

0
0
main
121 lines · 8.2 KB Raw History
// Copyright 2026 The KCP Authors.
// SPDX-License-Identifier: Apache-2.0

import { friendlyError, GRACE_MS, signInLinkRequest, signInURL, signInView } from "./identity_ui.js";

function assertEquals(actual, expected, label = "value") {
  const a = JSON.stringify(actual);
  const e = JSON.stringify(expected);
  if (a !== e) throw new Error(`${label}: expected ${e}, got ${a}`);
}

const now = Date.parse("2026-10-24T12:00:00Z");
const URL1 = "https://bsky.social/oauth/authorize?client_id=x&state=s1";
const awaiting = {
  phase: "AwaitingAuthorization",
  pendingAction: { kind: "visitUrl", url: URL1, expiresAt: "2026-10-24T12:05:00Z" },
};
const ALICE = "did:plc:lpfuqerea3deuoyrn7ojser4";
const ready = { phase: "Ready", did: ALICE, handle: "johnandersen777.bsky.social" };
const at = (local, identity = awaiting) => signInView({ identity, local, now });

Deno.test("signInURL refuses expired, opened, and other-handle URLs", () => {
  assertEquals(signInURL(awaiting, {}, now), URL1);
  assertEquals(signInURL(awaiting, { used: [URL1] }, now), "", "used");
  assertEquals(signInURL(awaiting, {}, Date.parse("2026-10-24T12:06:00Z")), "", "expired");
  assertEquals(signInURL(awaiting, { typed: "bob.test", applied: "alice.test" }, now), "", "handle changed");
  assertEquals(signInURL(awaiting, { typed: "@Alice.test ", applied: "alice.test" }, now), URL1, "normalized");
  assertEquals(signInURL({ phase: "Ready" }, {}, now), "");
  const hinted = { ...awaiting, pendingAction: { ...awaiting.pendingAction, url: `${URL1}&login_hint=alice.test` } };
  assertEquals(signInURL(hinted, { typed: "alice.test", applied: "alice.test" }, now), hinted.pendingAction.url);
  assertEquals(signInURL(hinted, { typed: "bob.test", applied: "bob.test" }, now), "", "issued for the old handle");
});

Deno.test("signInView walks signed out to signed in", () => {
  assertEquals(signInView({ requester: false }).state, "starting");
  const out = at({}, { phase: "Pending" });
  assertEquals([out.state, out.primary.action, out.primary.disabled], ["signedOut", "start", undefined]);
  // A URL already issued opens on the first tap: iOS allows a popup only in the tap.
  assertEquals(at({}).primary.action, "open");
  const preparing = at({ step: "preparing", since: now - 1000, used: [URL1] });
  assertEquals([preparing.state, preparing.busy], ["preparing", true]);
  const continueTap = at({ step: "preparing", since: now - 1000 });
  assertEquals([continueTap.state, continueTap.primary.label], ["ready", "Continue sign-in"]);
  const authorizing = at({ step: "authorizing", url: URL1 });
  assertEquals([authorizing.state, authorizing.busy, authorizing.secondary.action], ["authorizing", true, "cancel"]);
  assertEquals(at({ step: "completing", since: now, used: [URL1] }).state, "completing");
  const done = at({ step: "completing", since: now, account: { did: ALICE, handle: "alice.bsky.social" } }, ready);
  assertEquals([done.state, done.text, done.secondary.action], ["signedIn", "Signed in as alice.bsky.social", "signOut"]);
});

Deno.test("signInView shows the session's account, not the typed handle", () => {
  const unresolved = at({ typed: "" }, ready);
  assertEquals(unresolved.text, `Signed in as ${ALICE}`, "falls back to the DID");
  const mismatch = at({ typed: "johnandersen777.bsky.social", account: { did: ALICE, handle: "alice.bsky.social" } }, ready);
  assertEquals([mismatch.tone, mismatch.text, mismatch.primary.label],
    ["warn", "Signed in as alice.bsky.social, not johnandersen777.bsky.social.", "Switch account"]);
  const stale = at({ account: { did: "did:plc:other", handle: "bob.test" } }, ready);
  assertEquals(stale.text, `Signed in as ${ALICE}`, "a handle resolved for another DID is ignored");
});

Deno.test("signInView waits out the old status after a tap, then trusts it", () => {
  const failedStatus = { phase: "Failed", message: "oauthclient: the oauth callback state is unknown" };
  assertEquals(at({}, failedStatus).state, "signedOut", "nobody tapped yet");
  assertEquals(at({ step: "preparing", since: now - 1000 }, failedStatus).state, "preparing");
  const late = at({ step: "preparing", since: now - GRACE_MS - 1 }, failedStatus);
  assertEquals([late.state, late.text, late.primary.action],
    ["failed", "Sign-in failed: that sign-in link was already used or has expired", "start"]);
  assertEquals(at({ step: "preparing", since: now - 1000 }, ready).state, "preparing", "switching away from Ready");
  assertEquals(at({ step: "completing", since: now - GRACE_MS - 1, used: [URL1] }).state, "failed");
  assertEquals(at({ error: "access_denied" }).primary.action, "open", "a reissued URL opens on the retry tap");
  assertEquals(at({ error: "access_denied", used: [URL1] }).primary.action, "start");
  assertEquals(at({ step: "signingOut", since: now }, ready).state, "signingOut");
  assertEquals(at({}, { ...awaiting, phase: "SessionExpired" }).primary.label, "Sign in again");
});

Deno.test("signInView offers new permissions without leaving Signed in", () => {
  const upgrade = {
    ...ready,
    pendingAction: { kind: "signInAgain", reason: "ScopeUpgrade", url: URL1, expiresAt: "2026-10-24T12:05:00Z" },
  };
  const account = { did: ALICE, handle: "alice.bsky.social" };
  const v = at({ account }, upgrade);
  assertEquals([v.state, v.tone, v.text, v.primary.label, v.primary.action, v.secondary.action],
    ["signedIn", "warn", "Signed in as alice.bsky.social", "Grant permissions", "open", "signOut"]);
  // once opened (or expired) the tap asks socialweb for a new link, never a new sign-in
  assertEquals(at({ account, used: [URL1] }, upgrade).primary.action, "refresh");
  assertEquals(signInView({ identity: upgrade, local: { account }, now: Date.parse("2026-10-24T12:06:00Z") }).primary.action, "refresh");
  // a failed upgrade sign-in stays signed in, error under it, Sign out still there
  const failedUp = at({ account, error: "access_denied" }, upgrade);
  assertEquals([failedUp.state, failedUp.detail, failedUp.secondary.action],
    ["signedIn", "Granting permissions failed: the sign-in was declined", "signOut"]);
  // r133 shape without a URL: {kind, reason, message, missingScopes, handle}
  const noURL = { ...ready, pendingAction: { kind: "signInAgain", reason: "ScopeUpgrade", missingScopes: ["rpc:chat.atirc.revoke?aud=did:web:irc.fedfork.com%23atirc"] } };
  const start = at({ account }, noURL);
  assertEquals([start.state, start.primary.label, start.primary.action], ["signedIn", "Get a new sign-in link", "refresh"]);
  // a signInAgain on a Ready identity without reason ScopeUpgrade is not an upgrade, URL or not
  assertEquals(at({ account }, { ...ready, pendingAction: { kind: "signInAgain" } }).primary.action, "switch");
  assertEquals(at({ account }, { ...ready, pendingAction: { kind: "signInAgain", url: URL1 } }).primary.action, "switch");
  // done: socialweb clears pendingAction
  assertEquals(at({ account }, ready).primary.action, "switch");
  // any other error while Ready (a failed sign out) is shown, not swallowed
  const outFailed = at({ account, error: "kcp is still starting" }, ready);
  assertEquals([outFailed.state, outFailed.tone, outFailed.detail, outFailed.secondary.action],
    ["signedIn", "warn", "kcp is still starting", "signOut"]);
  // a typed other handle still says so first
  assertEquals(at({ account, typed: "bob.test" }, upgrade).text, "Signed in as alice.bsky.social, not bob.test.");
});

Deno.test("signInLinkRequest stamps spec.signInRequestedAt and nothing else", () => {
  const req = signInLinkRequest("/apis/socialweb.computer/v1alpha1", new Date("2026-10-31T01:00:00Z"));
  assertEquals([req.method, req.contentType], ["PATCH", "application/apply-patch+yaml"]);
  assertEquals(req.path, "/apis/socialweb.computer/v1alpha1/marketidentities/requester?fieldManager=socialweb.computer%2Fsignin&force=true");
  assertEquals(JSON.stringify(req.body.spec), JSON.stringify({ signInRequestedAt: "2026-10-31T01:00:00.000Z" }));
  assertEquals([req.body.kind, req.body.metadata.name], ["MarketIdentity", "requester"]);
});

Deno.test("friendlyError hides package prefixes", () => {
  assertEquals(friendlyError("marketidentity: resolving the configured handle x: not found"),
    "resolving the configured handle x: not found");
  assertEquals(friendlyError("OAuth popup was blocked"), "the browser blocked the sign-in tab; tap Try again");
});