A music player that connects to your cloud/distributed storage.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

chore: supplement components tests

+168
+168
tests/components/supplement/last.fm/test.ts
··· 1 + import { describe, it } from "@std/testing/bdd"; 2 + import { expect } from "@std/expect"; 3 + 4 + import { testWeb } from "@tests/common/index.ts"; 5 + 6 + describe("components/supplement/last.fm", () => { 7 + // Initial state 8 + 9 + it("isAuthenticated returns false initially", async () => { 10 + const result = await testWeb(async () => { 11 + const mod = await import("~/components/supplement/last.fm/element.js"); 12 + const el = new mod.CLASS(); 13 + document.body.append(el); 14 + return el.isAuthenticated(); 15 + }); 16 + 17 + expect(result).toBe(false); 18 + }); 19 + 20 + it("isAuthenticating returns false initially", async () => { 21 + const result = await testWeb(async () => { 22 + const mod = await import("~/components/supplement/last.fm/element.js"); 23 + const el = new mod.CLASS(); 24 + document.body.append(el); 25 + return el.isAuthenticating(); 26 + }); 27 + 28 + expect(result).toBe(false); 29 + }); 30 + 31 + it("handle returns null initially", async () => { 32 + const result = await testWeb(async () => { 33 + const mod = await import("~/components/supplement/last.fm/element.js"); 34 + const el = new mod.CLASS(); 35 + document.body.append(el); 36 + return el.handle(); 37 + }); 38 + 39 + expect(result).toBe(null); 40 + }); 41 + 42 + // Session restore from localStorage 43 + 44 + it("restores session from localStorage on connectedCallback", async () => { 45 + const result = await testWeb(async () => { 46 + localStorage.setItem( 47 + "diffuse/supplement/last.fm/session", 48 + JSON.stringify({ key: "test-session-key", name: "testuser" }), 49 + ); 50 + 51 + const mod = await import("~/components/supplement/last.fm/element.js"); 52 + const el = new mod.CLASS(); 53 + document.body.append(el); 54 + 55 + // #tryRestore is async; wait for the microtasks to settle 56 + await new Promise((r) => setTimeout(r, 10)); 57 + 58 + return { authenticated: el.isAuthenticated(), handle: el.handle() }; 59 + }); 60 + 61 + expect(result.authenticated).toBe(true); 62 + expect(result.handle).toBe("testuser"); 63 + }); 64 + 65 + // Sign out 66 + 67 + it("signOut clears isAuthenticated and handle", async () => { 68 + const result = await testWeb(async () => { 69 + localStorage.setItem( 70 + "diffuse/supplement/last.fm/session", 71 + JSON.stringify({ key: "test-session-key", name: "testuser" }), 72 + ); 73 + 74 + const mod = await import("~/components/supplement/last.fm/element.js"); 75 + const el = new mod.CLASS(); 76 + document.body.append(el); 77 + 78 + await new Promise((r) => setTimeout(r, 10)); 79 + 80 + el.signOut(); 81 + 82 + return { authenticated: el.isAuthenticated(), handle: el.handle() }; 83 + }); 84 + 85 + expect(result.authenticated).toBe(false); 86 + expect(result.handle).toBe(null); 87 + }); 88 + 89 + it("signOut removes session from localStorage", async () => { 90 + const result = await testWeb(async () => { 91 + localStorage.setItem( 92 + "diffuse/supplement/last.fm/session", 93 + JSON.stringify({ key: "test-session-key", name: "testuser" }), 94 + ); 95 + 96 + const mod = await import("~/components/supplement/last.fm/element.js"); 97 + const el = new mod.CLASS(); 98 + document.body.append(el); 99 + 100 + el.signOut(); 101 + 102 + return localStorage.getItem("diffuse/supplement/last.fm/session"); 103 + }); 104 + 105 + expect(result).toBe(null); 106 + }); 107 + 108 + it("signOut on unauthenticated element does not throw", async () => { 109 + const result = await testWeb(async () => { 110 + const mod = await import("~/components/supplement/last.fm/element.js"); 111 + const el = new mod.CLASS(); 112 + document.body.append(el); 113 + el.signOut(); 114 + return el.isAuthenticated(); 115 + }); 116 + 117 + expect(result).toBe(false); 118 + }); 119 + 120 + // Unauthenticated API calls 121 + 122 + it("nowPlaying throws when not authenticated", async () => { 123 + const result = await testWeb(async () => { 124 + const mod = await import("~/components/supplement/last.fm/element.js"); 125 + const el = new mod.CLASS(); 126 + document.body.append(el); 127 + 128 + try { 129 + await el.nowPlaying({ 130 + $type: "sh.diffuse.output.track", 131 + id: "t1", 132 + uri: "https://example.com/track.mp3", 133 + }); 134 + return false; 135 + } catch (err) { 136 + return err instanceof Error && 137 + err.message.includes("Not authenticated"); 138 + } 139 + }); 140 + 141 + expect(result).toBe(true); 142 + }); 143 + 144 + it("scrobble throws when not authenticated", async () => { 145 + const result = await testWeb(async () => { 146 + const mod = await import("~/components/supplement/last.fm/element.js"); 147 + const el = new mod.CLASS(); 148 + document.body.append(el); 149 + 150 + try { 151 + await el.scrobble( 152 + { 153 + $type: "sh.diffuse.output.track", 154 + id: "t1", 155 + uri: "https://example.com/track.mp3", 156 + }, 157 + Date.now(), 158 + ); 159 + return false; 160 + } catch (err) { 161 + return err instanceof Error && 162 + err.message.includes("Not authenticated"); 163 + } 164 + }); 165 + 166 + expect(result).toBe(true); 167 + }); 168 + });