This commit is contained in:
thePR0M3TH3AN committed 2025-11-12 11:06:10 -05:00
1 parent 000127f88c
commit 071d78dd6c
194 files changed
+28793 -3944

No files matched your search

@@ -0,0 +1,96 @@
import test from "node:test";
import assert from "node:assert/strict";
import { JSDOM } from "jsdom";
import { Application } from "../../js/app.js";
const VIEW_HTML = `<!DOCTYPE html>
<html>
<body>
<div id="chrome">
<button id="loginButton" class="btn"></button>
<button id="logoutButton" class="hidden"></button>
<button id="uploadButton" class="hidden" hidden></button>
<button id="profileButton" class="hidden" hidden></button>
<span id="userStatus" class="hidden"></span>
<span id="userPubKey"></span>
<button id="closeLoginModal"></button>
</div>
<a id="subscriptionsLink" class="hidden"></a>
</body>
</html>`;
test("hydrateSidebarNavigation reveals chrome controls for authenticated viewers", () => {
const dom = new JSDOM(VIEW_HTML, { url: "https://example.com" });
const { window } = dom;
const previousWindow = global.window;
const previousDocument = global.document;
global.window = window;
global.document = window.document;
const previousHTMLElement = global.HTMLElement;
global.HTMLElement = window.HTMLElement;
try {
const app = Object.create(Application.prototype);
app.appChromeController = {
receivedElements: null,
setElements(elements) {
this.receivedElements = elements;
},
};
app.userStatus = window.document.getElementById("userStatus");
app.userPubKey = window.document.getElementById("userPubKey");
app.subscriptionsLink = null;
app.isUserLoggedIn = () => true;
app.log = () => {};
app.showError = () => {};
app.showSuccess = () => {};
app.showStatus = () => {};
app.hydrateSidebarNavigation();
const uploadButton = window.document.getElementById("uploadButton");
const profileButton = window.document.getElementById("profileButton");
const loginButton = window.document.getElementById("loginButton");
const logoutButton = window.document.getElementById("logoutButton");
const subscriptionsLink = window.document.getElementById("subscriptionsLink");
const closeLoginButton = window.document.getElementById("closeLoginModal");
assert.equal(app.uploadButton, uploadButton);
assert.equal(app.profileButton, profileButton);
assert.equal(app.loginButton, loginButton);
assert.equal(app.logoutButton, logoutButton);
assert.equal(app.closeLoginModalBtn, closeLoginButton);
assert.equal(uploadButton.classList.contains("hidden"), false);
assert.equal(uploadButton.hasAttribute("hidden"), false);
assert.equal(profileButton.classList.contains("hidden"), false);
assert.equal(profileButton.hasAttribute("hidden"), false);
assert.equal(loginButton.classList.contains("hidden"), true);
assert.equal(loginButton.hasAttribute("hidden"), true);
assert.equal(logoutButton.classList.contains("hidden"), false);
assert.equal(subscriptionsLink.classList.contains("hidden"), false);
assert.ok(app.appChromeController.receivedElements);
assert.equal(
app.appChromeController.receivedElements.uploadButton,
uploadButton,
);
assert.equal(
app.appChromeController.receivedElements.profileButton,
profileButton,
);
} finally {
global.window = previousWindow;
global.document = previousDocument;
if (previousHTMLElement === undefined) {
delete global.HTMLElement;
} else {
global.HTMLElement = previousHTMLElement;
}
}
});
+132
View File
@@ -0,0 +1,132 @@
import test from "node:test";
import assert from "node:assert/strict";
import { Application } from "../../js/app.js";
import { nostrClient } from "../../js/nostrClientFacade.js";
const HEX64_REGEX = /^[0-9a-f]{64}$/i;
function createTestApp(pubkey) {
const app = {
_pubkey: pubkey,
normalizeHexPubkey(value) {
if (typeof value !== "string") {
return null;
}
const trimmed = value.trim();
if (!trimmed) {
return null;
}
if (HEX64_REGEX.test(trimmed)) {
return trimmed.toLowerCase();
}
return null;
},
isUserLoggedIn: Application.prototype.isUserLoggedIn,
};
Object.defineProperty(app, "pubkey", {
get() {
return this._pubkey;
},
set(value) {
this._pubkey = value;
},
});
return app;
}
test("isUserLoggedIn returns false when no user pubkey is set", () => {
const previousPubkey = nostrClient.pubkey;
const previousSessionActor = nostrClient.sessionActor;
try {
nostrClient.pubkey = null;
nostrClient.sessionActor = null;
const app = createTestApp(null);
assert.equal(app.isUserLoggedIn(), false);
} finally {
nostrClient.pubkey = previousPubkey;
nostrClient.sessionActor = previousSessionActor;
}
});
test("isUserLoggedIn treats extension logins as authenticated", () => {
const previousPubkey = nostrClient.pubkey;
const previousSessionActor = nostrClient.sessionActor;
try {
const pubkey = "893a31fa5a6481295bb76ea2dd8d5ec70640e6e6ad398e6547d771e8256af11f";
nostrClient.pubkey = pubkey;
nostrClient.sessionActor = { pubkey };
const app = createTestApp(pubkey);
assert.equal(app.isUserLoggedIn(), true);
} finally {
nostrClient.pubkey = previousPubkey;
nostrClient.sessionActor = previousSessionActor;
}
});
test("isUserLoggedIn guards against mismatched nostrClient state", () => {
const previousPubkey = nostrClient.pubkey;
const previousSessionActor = nostrClient.sessionActor;
try {
const pubkey = "a".repeat(64);
nostrClient.pubkey = "b".repeat(64);
nostrClient.sessionActor = { pubkey };
const app = createTestApp(pubkey);
assert.equal(app.isUserLoggedIn(), false);
} finally {
nostrClient.pubkey = previousPubkey;
nostrClient.sessionActor = previousSessionActor;
}
});
test("isUserLoggedIn ignores anonymous session actor mismatches", () => {
const previousPubkey = nostrClient.pubkey;
const previousSessionActor = nostrClient.sessionActor;
try {
const pubkey = "c".repeat(64);
nostrClient.pubkey = pubkey;
nostrClient.sessionActor = {
pubkey: "d".repeat(64),
privateKey: "e".repeat(64),
createdAt: Date.now(),
};
const app = createTestApp(pubkey);
assert.equal(app.isUserLoggedIn(), true);
} finally {
nostrClient.pubkey = previousPubkey;
nostrClient.sessionActor = previousSessionActor;
}
});
test("isUserLoggedIn rejects mismatched managed session actors", () => {
const previousPubkey = nostrClient.pubkey;
const previousSessionActor = nostrClient.sessionActor;
try {
const pubkey = "f".repeat(64);
nostrClient.pubkey = pubkey;
nostrClient.sessionActor = {
pubkey: "0".repeat(64),
source: "nsec",
};
const app = createTestApp(pubkey);
assert.equal(app.isUserLoggedIn(), false);
} finally {
nostrClient.pubkey = previousPubkey;
nostrClient.sessionActor = previousSessionActor;
}
});