|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | + |
| 3 | +import { youtubeAdapter } from "../adapter.js"; |
| 4 | +import { resolveYouTubeCommunityPostTarget } from "../service.js"; |
| 5 | + |
| 6 | +describe("youtube subscription params extraction", () => { |
| 7 | + test("extracts subscribe and unsubscribe params for the target channel from ytInitialData", () => { |
| 8 | + const html = ` |
| 9 | + <html> |
| 10 | + <script> |
| 11 | + var ytInitialData = { |
| 12 | + "header": { |
| 13 | + "pageHeaderRenderer": { |
| 14 | + "content": { |
| 15 | + "pageHeaderViewModel": { |
| 16 | + "actions": { |
| 17 | + "flexibleActionsViewModel": { |
| 18 | + "actionsRows": [ |
| 19 | + { |
| 20 | + "actions": [ |
| 21 | + { |
| 22 | + "subscribeButtonViewModel": { |
| 23 | + "subscribeButtonContent": { |
| 24 | + "onTapCommand": { |
| 25 | + "innertubeCommand": { |
| 26 | + "subscribeEndpoint": { |
| 27 | + "channelIds": ["UCTARGET123"], |
| 28 | + "params": "SUBSCRIBE_TOKEN" |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + }, |
| 33 | + "unsubscribeButtonContent": { |
| 34 | + "onTapCommand": { |
| 35 | + "innertubeCommand": { |
| 36 | + "signalServiceEndpoint": { |
| 37 | + "actions": [ |
| 38 | + { |
| 39 | + "openPopupAction": { |
| 40 | + "popup": { |
| 41 | + "confirmDialogRenderer": { |
| 42 | + "confirmButton": { |
| 43 | + "buttonRenderer": { |
| 44 | + "serviceEndpoint": { |
| 45 | + "unsubscribeEndpoint": { |
| 46 | + "channelIds": ["UCTARGET123"], |
| 47 | + "params": "UNSUBSCRIBE_TOKEN" |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + ] |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + ] |
| 64 | + } |
| 65 | + ] |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + }; |
| 73 | + </script> |
| 74 | + </html> |
| 75 | + `; |
| 76 | + |
| 77 | + const adapter = youtubeAdapter as any; |
| 78 | + expect(adapter.extractSubscriptionMutationParams(html, "UCTARGET123", true)).toBe("SUBSCRIBE_TOKEN"); |
| 79 | + expect(adapter.extractSubscriptionMutationParams(html, "UCTARGET123", false)).toBe("UNSUBSCRIBE_TOKEN"); |
| 80 | + expect(adapter.extractSubscriptionMutationParams(html, "UCOTHER", true)).toBeUndefined(); |
| 81 | + }); |
| 82 | +}); |
| 83 | + |
| 84 | +describe("youtube community post target parsing", () => { |
| 85 | + test("normalizes canonical and community lb URLs to a /post target", () => { |
| 86 | + expect(resolveYouTubeCommunityPostTarget("https://www.youtube.com/post/Ugkx30z9Hvx7V3TbwDBOc8FEbcax4HDgLma7")).toEqual({ |
| 87 | + postId: "Ugkx30z9Hvx7V3TbwDBOc8FEbcax4HDgLma7", |
| 88 | + url: "https://www.youtube.com/post/Ugkx30z9Hvx7V3TbwDBOc8FEbcax4HDgLma7", |
| 89 | + }); |
| 90 | + |
| 91 | + expect(resolveYouTubeCommunityPostTarget("https://www.youtube.com/channel/UC123/community?lb=Ugkx30z9Hvx7V3TbwDBOc8FEbcax4HDgLma7")).toEqual({ |
| 92 | + postId: "Ugkx30z9Hvx7V3TbwDBOc8FEbcax4HDgLma7", |
| 93 | + url: "https://www.youtube.com/post/Ugkx30z9Hvx7V3TbwDBOc8FEbcax4HDgLma7", |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + test("accepts a bare YouTube community post id", () => { |
| 98 | + expect(resolveYouTubeCommunityPostTarget("Ugkx30z9Hvx7V3TbwDBOc8FEbcax4HDgLma7")).toEqual({ |
| 99 | + postId: "Ugkx30z9Hvx7V3TbwDBOc8FEbcax4HDgLma7", |
| 100 | + url: "https://www.youtube.com/post/Ugkx30z9Hvx7V3TbwDBOc8FEbcax4HDgLma7", |
| 101 | + }); |
| 102 | + }); |
| 103 | +}); |
0 commit comments