|
| 1 | +// Regression guards for the insights HTML parsers. The Instagram export |
| 2 | +// format is not stable — class names and label text change every few months — |
| 3 | +// and these parsers are the most fragile surface in the project. Any test |
| 4 | +// that goes red here is a strong signal that IG changed their layout. |
| 5 | +// |
| 6 | +// The fixtures below are minimal extracts of real exports, simplified to the |
| 7 | +// shape that each parser actually walks. They intentionally include the |
| 8 | +// extra wrapper divs and class noise that IG ships, so that selector changes |
| 9 | +// (e.g. dropping `_2piu`) are caught. |
| 10 | + |
| 11 | +import { describe, it, expect } from "vitest"; |
| 12 | +import JSZip from "jszip"; |
| 13 | +import { parseInsights } from "@/lib/insights-parser"; |
| 14 | + |
| 15 | +async function buildZip(files: Record<string, string>): Promise<JSZip> { |
| 16 | + const zip = new JSZip(); |
| 17 | + for (const [path, content] of Object.entries(files)) { |
| 18 | + zip.file(path, content); |
| 19 | + } |
| 20 | + // Round-trip through generateAsync so that the resulting JSZip behaves the |
| 21 | + // same as one loaded from disk (file metadata, not just in-memory shortcut). |
| 22 | + const blob = await zip.generateAsync({ type: "blob" }); |
| 23 | + return JSZip.loadAsync(blob); |
| 24 | +} |
| 25 | + |
| 26 | +describe("parseInsights — likedPosts (KO label)", () => { |
| 27 | + it("extracts usernames from `사용자 이름` rows", async () => { |
| 28 | + const html = ` |
| 29 | + <html><body> |
| 30 | + <div> |
| 31 | + <table> |
| 32 | + <tr><td>사용자 이름</td><td class="_2piu _a6_r">alice</td></tr> |
| 33 | + </table> |
| 34 | + </div> |
| 35 | + <div> |
| 36 | + <table> |
| 37 | + <tr><td>사용자 이름</td><td class="_2piu _a6_r">bob</td></tr> |
| 38 | + </table> |
| 39 | + </div> |
| 40 | + </body></html> |
| 41 | + `; |
| 42 | + const zip = await buildZip({ |
| 43 | + "your_instagram_activity/likes/liked_posts.html": html, |
| 44 | + }); |
| 45 | + const insights = await parseInsights(zip); |
| 46 | + const names = insights.topLikedAccounts.map((r) => r.name).sort(); |
| 47 | + expect(names).toEqual(["alice", "bob"]); |
| 48 | + }); |
| 49 | +}); |
| 50 | + |
| 51 | +describe("parseInsights — likedPosts (EN label)", () => { |
| 52 | + it("extracts usernames from `Username` rows", async () => { |
| 53 | + const html = ` |
| 54 | + <html><body> |
| 55 | + <table> |
| 56 | + <tr><td>Username</td><td class="_2piu _a6_r">carol</td></tr> |
| 57 | + <tr><td>Username</td><td class="_2piu _a6_r">dave</td></tr> |
| 58 | + </table> |
| 59 | + </body></html> |
| 60 | + `; |
| 61 | + const zip = await buildZip({ |
| 62 | + "your_instagram_activity/likes/liked_posts.html": html, |
| 63 | + }); |
| 64 | + const insights = await parseInsights(zip); |
| 65 | + const names = insights.topLikedAccounts.map((r) => r.name).sort(); |
| 66 | + expect(names).toEqual(["carol", "dave"]); |
| 67 | + }); |
| 68 | +}); |
| 69 | + |
| 70 | +describe("parseInsights — savedPosts (h2 usernames)", () => { |
| 71 | + it("collects single-token h2 entries", async () => { |
| 72 | + const html = ` |
| 73 | + <html><body> |
| 74 | + <h2>spaceship_one</h2> |
| 75 | + <h2>not a username</h2> |
| 76 | + <h2>cometchaser</h2> |
| 77 | + <h2>this_is_too_long_to_be_a_real_instagram_handle_xxxxxxxx</h2> |
| 78 | + </body></html> |
| 79 | + `; |
| 80 | + const zip = await buildZip({ |
| 81 | + "your_instagram_activity/saved/saved_posts.html": html, |
| 82 | + }); |
| 83 | + const insights = await parseInsights(zip); |
| 84 | + const names = insights.topSavedAccounts.map((r) => r.name).sort(); |
| 85 | + // "not a username" rejected (whitespace), 50+ char string rejected. |
| 86 | + expect(names).toEqual(["cometchaser", "spaceship_one"]); |
| 87 | + }); |
| 88 | +}); |
| 89 | + |
| 90 | +describe("parseInsights — profileSearches", () => { |
| 91 | + it("returns h2 names with extracted timestamps", async () => { |
| 92 | + const html = ` |
| 93 | + <html><body> |
| 94 | + <div> |
| 95 | + <h2>searched_user_1</h2> |
| 96 | + <div><div>3월 16, 2026 6:41 오후</div></div> |
| 97 | + </div> |
| 98 | + <div> |
| 99 | + <h2>searched_user_2</h2> |
| 100 | + <div><div>4월 1, 2026 9:00 오전</div></div> |
| 101 | + </div> |
| 102 | + </body></html> |
| 103 | + `; |
| 104 | + const zip = await buildZip({ |
| 105 | + "your_instagram_activity/recent_searches/profile_searches.html": html, |
| 106 | + }); |
| 107 | + const insights = await parseInsights(zip); |
| 108 | + expect(insights.profileSearches).toHaveLength(2); |
| 109 | + expect(insights.profileSearches[0].name).toBe("searched_user_1"); |
| 110 | + expect(insights.profileSearches[0].timestamp).toBeGreaterThan(0); |
| 111 | + }); |
| 112 | +}); |
| 113 | + |
| 114 | +describe("parseInsights — wordSearches", () => { |
| 115 | + it("extracts query text from 검색 / Search rows", async () => { |
| 116 | + const html = ` |
| 117 | + <html><body> |
| 118 | + <table> |
| 119 | + <tbody> |
| 120 | + <tr><td>검색<div><div>코딩</div></div></td><td class="_2piu">3월 16, 2026 6:41 오후</td></tr> |
| 121 | + </tbody> |
| 122 | + </table> |
| 123 | + <table> |
| 124 | + <tbody> |
| 125 | + <tr><td>Search<div><div>music</div></div></td><td class="_2piu">4월 1, 2026 9:00 오전</td></tr> |
| 126 | + </tbody> |
| 127 | + </table> |
| 128 | + </body></html> |
| 129 | + `; |
| 130 | + const zip = await buildZip({ |
| 131 | + "your_instagram_activity/recent_searches/word_or_phrase_searches.html": html, |
| 132 | + }); |
| 133 | + const insights = await parseInsights(zip); |
| 134 | + const queries = insights.wordSearches.map((r) => r.name).sort(); |
| 135 | + expect(queries).toEqual(["music", "코딩"]); |
| 136 | + }); |
| 137 | +}); |
| 138 | + |
| 139 | +describe("parseInsights — loginActivity", () => { |
| 140 | + it("counts ISO timestamps in h2 elements per hour", async () => { |
| 141 | + const html = ` |
| 142 | + <html><body> |
| 143 | + <h2>2026-04-01T09:23:00Z</h2> |
| 144 | + <h2>2026-04-01T09:45:00Z</h2> |
| 145 | + <h2>2026-04-01T18:01:00Z</h2> |
| 146 | + </body></html> |
| 147 | + `; |
| 148 | + const zip = await buildZip({ |
| 149 | + "security_and_login_information/login_activity.html": html, |
| 150 | + }); |
| 151 | + const insights = await parseInsights(zip); |
| 152 | + expect(insights.loginHours[9]).toBe(2); |
| 153 | + expect(insights.loginHours[18]).toBe(1); |
| 154 | + expect(insights.loginHours.reduce((a, b) => a + b, 0)).toBe(3); |
| 155 | + }); |
| 156 | + |
| 157 | + it("counts KO 오전/오후 cells in 12-hour clock", async () => { |
| 158 | + const html = ` |
| 159 | + <html><body> |
| 160 | + <table><tbody> |
| 161 | + <tr><td class="_2piu _a6_r">3월 16, 2026 6:41 오후</td></tr> |
| 162 | + <tr><td class="_2piu _a6_r">3월 16, 2026 6:50 오후</td></tr> |
| 163 | + <tr><td class="_2piu _a6_r">3월 16, 2026 9:00 오전</td></tr> |
| 164 | + </tbody></table> |
| 165 | + </body></html> |
| 166 | + `; |
| 167 | + const zip = await buildZip({ |
| 168 | + "security_and_login_information/login_activity.html": html, |
| 169 | + }); |
| 170 | + const insights = await parseInsights(zip); |
| 171 | + expect(insights.loginHours[18]).toBe(2); |
| 172 | + expect(insights.loginHours[9]).toBe(1); |
| 173 | + }); |
| 174 | +}); |
| 175 | + |
| 176 | +describe("parseInsights — chats", () => { |
| 177 | + it("extracts chat partner names from h2 a", async () => { |
| 178 | + const html = ` |
| 179 | + <html><body> |
| 180 | + <h2><a href="messages/inbox/alice">alice</a></h2> |
| 181 | + <h2><a href="messages/inbox/bob">bob</a></h2> |
| 182 | + </body></html> |
| 183 | + `; |
| 184 | + const zip = await buildZip({ |
| 185 | + "your_instagram_activity/messages/chats.html": html, |
| 186 | + }); |
| 187 | + const insights = await parseInsights(zip); |
| 188 | + expect(insights.chatNames.sort()).toEqual(["alice", "bob"]); |
| 189 | + }); |
| 190 | +}); |
| 191 | + |
| 192 | +describe("parseInsights — empty / missing files", () => { |
| 193 | + it("returns zeros when none of the source files exist", async () => { |
| 194 | + const zip = await buildZip({ |
| 195 | + "followers_and_following/followers_1.html": "<html></html>", |
| 196 | + }); |
| 197 | + const insights = await parseInsights(zip); |
| 198 | + expect(insights.topLikedAccounts).toEqual([]); |
| 199 | + expect(insights.topSavedAccounts).toEqual([]); |
| 200 | + expect(insights.profileSearches).toEqual([]); |
| 201 | + expect(insights.wordSearches).toEqual([]); |
| 202 | + expect(insights.chatNames).toEqual([]); |
| 203 | + expect(insights.loginHours).toEqual(new Array(24).fill(0)); |
| 204 | + }); |
| 205 | +}); |
0 commit comments