| 1 | import { describe, expect, it, spyOn } from "bun:test"; |
| 2 | import { log } from "@clack/prompts"; |
| 3 | import { Injected, injectLinkTags } from "../src/commands/inject"; |
| 4 | |
| 5 | const atUri = "at://did:plc:abc123/app.bsky.feed.post/xyz"; |
| 6 | const publicationUri = "at://did:plc:def456/app.bsky.feed.generator/main"; |
| 7 | |
| 8 | describe("injectLinkTags", () => { |
| 9 | describe("neither tag needs injection", () => { |
| 10 | it("returns AlreadyPresent when both tags already exist", () => { |
| 11 | const content = `<html><head> |
| 12 | <link rel="site.standard.document" href="${atUri}"> |
| 13 | <link rel="site.standard.publication" href="${publicationUri}"> |
| 14 | </head></html>`; |
| 15 | const result = injectLinkTags( |
| 16 | false, |
| 17 | "test.html", |
| 18 | content, |
| 19 | atUri, |
| 20 | publicationUri, |
| 21 | ); |
| 22 | expect(result).toBe(Injected.AlreadyPresent); |
| 23 | }); |
| 24 | }); |
| 25 | |
| 26 | describe("one tag needs injection", () => { |
| 27 | it("injects only documentLinkTag when publicationLinkTag is already present", () => { |
| 28 | const content = `<html><head> |
| 29 | <link rel="site.standard.publication" href="${publicationUri}"> |
| 30 | </head></html>`; |
| 31 | const result = injectLinkTags( |
| 32 | false, |
| 33 | "test.html", |
| 34 | content, |
| 35 | atUri, |
| 36 | publicationUri, |
| 37 | ); |
| 38 | expect(typeof result).toBe("string"); |
| 39 | expect(result as string).toContain( |
| 40 | `<link rel="site.standard.document" href="${atUri}">`, |
| 41 | ); |
| 42 | expect( |
| 43 | result as string, |
| 44 | ).not.toContain(`<link rel="site.standard.publication" href="${publicationUri}"> |
| 45 | <link rel="site.standard.publication"`); |
| 46 | }); |
| 47 | |
| 48 | it("injects only publicationLinkTag when documentLinkTag is already present", () => { |
| 49 | const content = `<html><head> |
| 50 | <link rel="site.standard.document" href="${atUri}"> |
| 51 | </head></html>`; |
| 52 | const result = injectLinkTags( |
| 53 | false, |
| 54 | "test.html", |
| 55 | content, |
| 56 | atUri, |
| 57 | publicationUri, |
| 58 | ); |
| 59 | expect(typeof result).toBe("string"); |
| 60 | expect(result as string).toContain( |
| 61 | `<link rel="site.standard.publication" href="${publicationUri}">`, |
| 62 | ); |
| 63 | }); |
| 64 | }); |
| 65 | |
| 66 | describe("both tags need injection", () => { |
| 67 | it("injects both tags when neither is present", () => { |
| 68 | const content = "<html><head>\n</head></html>"; |
| 69 | const result = injectLinkTags( |
| 70 | false, |
| 71 | "test.html", |
| 72 | content, |
| 73 | atUri, |
| 74 | publicationUri, |
| 75 | ); |
| 76 | expect(typeof result).toBe("string"); |
| 77 | expect(result as string).toContain( |
| 78 | `<link rel="site.standard.document" href="${atUri}">`, |
| 79 | ); |
| 80 | expect(result as string).toContain( |
| 81 | `<link rel="site.standard.publication" href="${publicationUri}">`, |
| 82 | ); |
| 83 | }); |
| 84 | |
| 85 | it("injects tags before </head>", () => { |
| 86 | const content = "<html><head>\n</head><body></body></html>"; |
| 87 | const result = injectLinkTags( |
| 88 | false, |
| 89 | "test.html", |
| 90 | content, |
| 91 | atUri, |
| 92 | publicationUri, |
| 93 | ) as string; |
| 94 | const headCloseIndex = result.indexOf("</head>"); |
| 95 | expect(result.indexOf('rel="site.standard.document"')).toBeLessThan( |
| 96 | headCloseIndex, |
| 97 | ); |
| 98 | expect(result.indexOf('rel="site.standard.publication"')).toBeLessThan( |
| 99 | headCloseIndex, |
| 100 | ); |
| 101 | }); |
| 102 | |
| 103 | it("returns Skipped when no </head> is found", () => { |
| 104 | const warnSpy = spyOn(log, "warn").mockImplementation(() => {}); |
| 105 | const content = "<html><body>No head tag here</body></html>"; |
| 106 | const result = injectLinkTags( |
| 107 | false, |
| 108 | "test.html", |
| 109 | content, |
| 110 | atUri, |
| 111 | publicationUri, |
| 112 | ); |
| 113 | expect(result).toBe(Injected.Skipped); |
| 114 | expect(warnSpy).toHaveBeenCalledWith( |
| 115 | " No </head> found in test.html, skipping", |
| 116 | ); |
| 117 | warnSpy.mockRestore(); |
| 118 | }); |
| 119 | |
| 120 | it("returns Faked and does not modify content during dry run", () => { |
| 121 | const messageSpy = spyOn(log, "message").mockImplementation(() => {}); |
| 122 | const content = "<html><head>\n</head></html>"; |
| 123 | const result = injectLinkTags( |
| 124 | true, |
| 125 | "test.html", |
| 126 | content, |
| 127 | atUri, |
| 128 | publicationUri, |
| 129 | ); |
| 130 | expect(result).toBe(Injected.Faked); |
| 131 | expect(messageSpy).toHaveBeenCalledWith(" Would inject into: test.html"); |
| 132 | expect(messageSpy).toHaveBeenCalledWith( |
| 133 | ` <link rel="site.standard.document" href="${atUri}">`, |
| 134 | ); |
| 135 | expect(messageSpy).toHaveBeenCalledWith( |
| 136 | ` <link rel="site.standard.publication" href="${publicationUri}">`, |
| 137 | ); |
| 138 | messageSpy.mockRestore(); |
| 139 | }); |
| 140 | }); |
| 141 | }); |