Files
bitvid/tests/share-nostr-auth-gate.test.mjs
thePR0M3TH3ANandClaude Opus 5 f48150ed6b [ci] Fix the stale tests that had CI red
CI has been failing on unstable for weeks, across four unit files and a set of
e2e specs. None of it was a product regression: every failure is a test that
encodes behavior a later commit deliberately changed. Each is corrected against
the spec that superseded it, not adjusted until it passed.

Unit:

- video-modal-moderation: the fixture set flat `blurThumbnail`/`trustedMuteCount`,
  but normalizeVideoModerationContext derives shouldShow from
  `original.blurThumbnail`/`trustedCount`, so the assertion ran against a
  context whose early return had just deleted the blur it was checking. 4525c1c6
  also made a lone trusted mute a non-blocking ranking signal (its sibling test
  was renamed to say exactly that; this one was missed). Fixture now mirrors
  what moderationDecorator emits.
- trustBootstrap: asserted a subscription to onWhitelistChange that 18e08bf2
  deliberately removed -- buildTrustedSeeds derives seeds from the super admin,
  editors and fallback seeds, so a whitelist change cannot alter them. The
  removal is now pinned so it is not reinstated by reflex.
- moderation-service: 18e08bf2 removed `status.whitelisted ||` from
  isTrustedReporter (being allowed to publish is not a statement that your
  reports carry weight). A whitelisted reporter now counts toward the report
  total but not toward trusted.
- playbackService_forcedSource: forced P2P now falls back to a hosted URL when
  the torrent definitively fails -- playbackService documents the change and its
  rollback. The old assertion did not merely fail, it HUNG: forced sources skip
  the start timeout, so the now-reachable hosted attempt waited forever on a
  <video> that never emits.

E2E:

- ui-modals: asserted exactly one `.bv-modal` per component page, but
  profile-modal.html also bundles #storageCorsModal (d7ad1516). Requires at
  least one and drives the primary, which is what the test's own injected
  querySelector already used.
- coverage-utility-modules: used `author:abc123`, which 9afbc0cb correctly began
  rejecting as undecodable. Uses a real pubkey for the success path and pins the
  rejection separately.
- webseed-playback: asserted the FINAL src equals the CDN URL while also
  tolerating "No playable source found" in its status check -- contradictory,
  since a failed attempt calls resetVideoElement() and clears src. Headless CI
  cannot decode the archive.org H.264 sample. Now records src attachments and
  asserts the URL was attached, which is the behavior under test.
- nostr-runtime-coverage: asserted a legacy plaintext session-actor entry reads
  back as null. Since 8692dac6 added per-account storage, readV1Entry PURGES the
  plaintext entry and the reader falls back to the sole remaining encrypted
  account -- so null no longer describes the security property. Asserts the
  property directly: no plaintext key survives, in the entry or in raw storage.

Also drops the syncAuthUiState share-gate wiring added in d141c316. It was
redundant: Application.updateShareNostrAuthState() already fires on init, login,
logout, pubkey-change and signer-change and forwards to
videoModal.setShareNostrAuthState(). That call short-circuited on its own
`if (!this.videoModal?.setShareNostrAuthState) return;` guard purely because the
method did not exist -- adding it (d141c316) was the whole fix. The tests now
pin that contract instead of a second, competing code path.

All 416 unit files, all three CI shards, both DM suites and lint are green.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-07-28 17:37:24 -04:00

109 lines
4.3 KiB
JavaScript

// SCN-share-nostr-gate
//
// The share menu's "Share on Nostr" item is gated on `isLoggedIn && hasSigner`
// (js/ui/components/videoMenuRenderers.js). VideoModal initialized
// `shareNostrAuthState` to {false, false} and NOTHING ever wrote to it, so the
// item rendered permanently disabled — it looked like inert text, and the click
// delegation in bindShareMenuActions() bails on `button.disabled`.
//
// Two halves have to hold for the button to work:
// 1. VideoModal.setShareNostrAuthState() records the state.
// 2. syncAuthUiState() pushes real auth state in, and re-runs on auth change
// (it previously ran only once at bootstrap).
import test from "node:test";
import assert from "node:assert/strict";
import { VideoModal } from "../js/ui/components/VideoModal.js";
const PUBKEY = "b".repeat(64);
// --- VideoModal setter ----------------------------------------------------
function makeModal() {
const modal = Object.create(VideoModal.prototype);
modal.shareNostrAuthState = { isLoggedIn: false, hasSigner: false };
modal.closedPopover = 0;
modal.modalSharePopover = {
close: () => {
modal.closedPopover += 1;
},
};
return modal;
}
test("setShareNostrAuthState records the gate", () => {
const modal = makeModal();
assert.equal(modal.setShareNostrAuthState({ isLoggedIn: true, hasSigner: true }), true);
assert.deepEqual(modal.shareNostrAuthState, { isLoggedIn: true, hasSigner: true });
});
test("setShareNostrAuthState is idempotent and only closes a stale menu once", () => {
const modal = makeModal();
modal.setShareNostrAuthState({ isLoggedIn: true, hasSigner: true });
assert.equal(modal.closedPopover, 1, "open menu must re-render against new state");
// No change -> no churn. Closing the popover on every no-op sync would slam
// the menu shut under the user.
assert.equal(modal.setShareNostrAuthState({ isLoggedIn: true, hasSigner: true }), false);
assert.equal(modal.closedPopover, 1);
});
test("setShareNostrAuthState coerces and defaults safely", () => {
const modal = makeModal();
modal.setShareNostrAuthState({ isLoggedIn: "yes", hasSigner: 1 });
assert.deepEqual(modal.shareNostrAuthState, { isLoggedIn: true, hasSigner: true });
modal.setShareNostrAuthState();
assert.deepEqual(modal.shareNostrAuthState, { isLoggedIn: false, hasSigner: false });
});
test("setShareNostrAuthState tolerates no popover", () => {
const modal = makeModal();
modal.modalSharePopover = null;
assert.doesNotThrow(() =>
modal.setShareNostrAuthState({ isLoggedIn: true, hasSigner: true })
);
});
// --- app wiring ----------------------------------------------------------
// bitvid ALREADY routed auth changes to the modal: Application
// .updateShareNostrAuthState() is called on init, login, logout, pubkey-change
// and signer-change, and it forwards to videoModal.setShareNostrAuthState().
// That method simply did not exist, so the call short-circuited on its own
// `if (!this.videoModal?.setShareNostrAuthState) return;` guard and the gate
// stayed shut forever. These pin the contract that guard depends on.
test("the app's updateShareNostrAuthState contract is satisfied", async () => {
const { readFile } = await import("node:fs/promises");
const source = await readFile(new URL("../js/app.js", import.meta.url), "utf8");
// The guard short-circuits when the method is absent, which is exactly how
// this stayed broken silently. If the method is ever renamed, fail loudly.
assert.match(source, /videoModal\?\.setShareNostrAuthState/);
assert.match(source, /videoModal\.setShareNostrAuthState\(\{/);
// The lifecycle points that must keep the gate in sync.
for (const reason of ["init", "signer-change", "pubkey-change"]) {
assert.ok(
source.includes(`reason: "${reason}"`),
`app.js should refresh the share gate on ${reason}`
);
}
assert.equal(
typeof VideoModal.prototype.setShareNostrAuthState,
"function",
"VideoModal must expose the method app.js calls"
);
});
test("login and logout both refresh the gate", async () => {
const { readFile } = await import("node:fs/promises");
const source = await readFile(
new URL("../js/app/authSessionCoordinator.js", import.meta.url),
"utf8"
);
assert.ok(source.includes('reason: "auth-login"'));
assert.ok(source.includes('reason: "auth-logout"'));
});