office_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. package office
  2. import (
  3. "archive/zip"
  4. "bytes"
  5. "strings"
  6. "testing"
  7. )
  8. // 1x1 red PNG for image round-trip tests
  9. const testPngDataURL = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="
  10. func samplePresentation() *Presentation {
  11. return &Presentation{
  12. Size: []int{960, 540},
  13. Theme: "clean",
  14. Slides: []*Slide{
  15. {
  16. ID: "s1",
  17. Notes: "hello notes",
  18. Objects: []*Object{
  19. {Type: "text", X: 80, Y: 60, W: 800, H: 90, Z: 1,
  20. Props: Props{HTML: "Title line&amp;more<br>second line", FontSize: 44, Color: "#202124", Align: "center", Bold: true}},
  21. {Type: "shape", X: 100, Y: 200, W: 200, H: 160, Z: 2,
  22. Props: Props{Kind: "star", Fill: "#e07b1f", Stroke: "#333333", StrokeW: 2, Text: "star text", FontSize: 18}},
  23. {Type: "line", X: 400, Y: 300, W: -120, H: 80, Z: 3,
  24. Props: Props{Stroke: "#4c9be8", StrokeW: 3, ArrowEnd: true}},
  25. {Type: "image", X: 500, Y: 100, W: 200, H: 150, Z: 4,
  26. Props: Props{Src: testPngDataURL, Fit: "contain"}},
  27. {Type: "table", X: 60, Y: 380, W: 400, H: 120, Z: 5,
  28. Props: Props{Rows: [][]string{{"h1", "h2"}, {"a", "b"}}, HeaderRow: true, FontSize: 16}},
  29. },
  30. },
  31. {
  32. ID: "s2",
  33. Bg: "#123456",
  34. Objects: []*Object{
  35. {Type: "chart", X: 240, Y: 110, W: 480, H: 320, Z: 1,
  36. Props: Props{Png: testPngDataURL}},
  37. },
  38. },
  39. },
  40. }
  41. }
  42. func TestBuildPptxStructure(t *testing.T) {
  43. data, err := BuildPptx(samplePresentation())
  44. if err != nil {
  45. t.Fatalf("BuildPptx failed: %v", err)
  46. }
  47. zr, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
  48. if err != nil {
  49. t.Fatalf("output is not a valid zip: %v", err)
  50. }
  51. wantParts := []string{
  52. "[Content_Types].xml",
  53. "_rels/.rels",
  54. "ppt/presentation.xml",
  55. "ppt/_rels/presentation.xml.rels",
  56. "ppt/slideMasters/slideMaster1.xml",
  57. "ppt/slideLayouts/slideLayout1.xml",
  58. "ppt/theme/theme1.xml",
  59. "ppt/slides/slide1.xml",
  60. "ppt/slides/slide2.xml",
  61. "ppt/slides/_rels/slide1.xml.rels",
  62. "ppt/media/image1.png",
  63. "ppt/media/image2.png",
  64. }
  65. have := map[string]bool{}
  66. for _, f := range zr.File {
  67. have[f.Name] = true
  68. }
  69. for _, p := range wantParts {
  70. if !have[p] {
  71. t.Errorf("missing expected pptx part: %s", p)
  72. }
  73. }
  74. }
  75. func TestBuildPptxErrors(t *testing.T) {
  76. tests := []struct {
  77. name string
  78. pres *Presentation
  79. }{
  80. {"nil presentation", nil},
  81. {"no slides", &Presentation{Slides: []*Slide{}}},
  82. }
  83. for _, tc := range tests {
  84. t.Run(tc.name, func(t *testing.T) {
  85. if _, err := BuildPptx(tc.pres); err == nil {
  86. t.Errorf("expected error, got nil")
  87. }
  88. })
  89. }
  90. }
  91. func TestParsePptxInvalid(t *testing.T) {
  92. tests := []struct {
  93. name string
  94. data []byte
  95. }{
  96. {"garbage bytes", []byte("this is not a zip file at all")},
  97. {"empty", []byte{}},
  98. }
  99. for _, tc := range tests {
  100. t.Run(tc.name, func(t *testing.T) {
  101. if _, err := ParsePptx(tc.data); err == nil {
  102. t.Errorf("expected error, got nil")
  103. }
  104. })
  105. }
  106. }
  107. func TestPptxRoundtrip(t *testing.T) {
  108. src := samplePresentation()
  109. data, err := BuildPptx(src)
  110. if err != nil {
  111. t.Fatalf("BuildPptx failed: %v", err)
  112. }
  113. got, err := ParsePptx(data)
  114. if err != nil {
  115. t.Fatalf("ParsePptx failed on own output: %v", err)
  116. }
  117. if len(got.Slides) != 2 {
  118. t.Fatalf("slide count = %d, want 2", len(got.Slides))
  119. }
  120. s1 := got.Slides[0]
  121. types := map[string]int{}
  122. for _, o := range s1.Objects {
  123. types[o.Type]++
  124. }
  125. // slide 1: text + shape + line + image + table
  126. for _, want := range []string{"text", "shape", "line", "image", "table"} {
  127. if types[want] != 1 {
  128. t.Errorf("slide 1 object types = %v, want one %q", types, want)
  129. }
  130. }
  131. // text content survives (flattened to lines)
  132. var textObj *Object
  133. for _, o := range s1.Objects {
  134. if o.Type == "text" {
  135. textObj = o
  136. }
  137. }
  138. if textObj == nil {
  139. t.Fatal("no text object found after roundtrip")
  140. }
  141. if !strings.Contains(textObj.Props.HTML, "Title line&amp;more") ||
  142. !strings.Contains(textObj.Props.HTML, "second line") {
  143. t.Errorf("text content lost in roundtrip: %q", textObj.Props.HTML)
  144. }
  145. if !textObj.Props.Bold {
  146. t.Errorf("bold flag lost in roundtrip")
  147. }
  148. if textObj.Props.Align != "center" {
  149. t.Errorf("align = %q, want center", textObj.Props.Align)
  150. }
  151. // 44px -> 33pt -> back to 44px
  152. if textObj.Props.FontSize < 43 || textObj.Props.FontSize > 45 {
  153. t.Errorf("font size = %v, want ~44", textObj.Props.FontSize)
  154. }
  155. // position tolerance: EMU conversion is lossless at these scales
  156. if textObj.X < 79 || textObj.X > 81 {
  157. t.Errorf("text X = %v, want ~80", textObj.X)
  158. }
  159. // shape kind + text survive
  160. for _, o := range s1.Objects {
  161. if o.Type == "shape" {
  162. if o.Props.Kind != "star" {
  163. t.Errorf("shape kind = %q, want star", o.Props.Kind)
  164. }
  165. if o.Props.Text != "star text" {
  166. t.Errorf("shape text = %q, want %q", o.Props.Text, "star text")
  167. }
  168. }
  169. if o.Type == "line" {
  170. // negative W encodes direction; must survive via flipH
  171. if o.W >= 0 {
  172. t.Errorf("line W = %v, want negative (flipH lost)", o.W)
  173. }
  174. if !o.Props.ArrowEnd {
  175. t.Errorf("line arrowEnd lost in roundtrip")
  176. }
  177. }
  178. if o.Type == "image" {
  179. if !strings.HasPrefix(o.Props.Src, "data:image/png;base64,") {
  180. t.Errorf("image src is not a png data URL: %.40s", o.Props.Src)
  181. }
  182. }
  183. if o.Type == "table" {
  184. if len(o.Props.Rows) != 2 || len(o.Props.Rows[0]) != 2 {
  185. t.Fatalf("table dims = %dx%d, want 2x2", len(o.Props.Rows), len(o.Props.Rows[0]))
  186. }
  187. // cells come back as the editor's rich HTML - compare the
  188. // text they render, not the markup around it
  189. if cellText(o.Props.Rows[0][0]) != "h1" || cellText(o.Props.Rows[1][1]) != "b" {
  190. t.Errorf("table content lost: %v", o.Props.Rows)
  191. }
  192. if !strings.Contains(o.Props.Rows[0][0], "font-weight:700") {
  193. t.Errorf("header row bold lost: %q", o.Props.Rows[0][0])
  194. }
  195. if !o.Props.HeaderRow {
  196. t.Errorf("headerRow flag lost")
  197. }
  198. }
  199. }
  200. // slide 2: explicit background + chart-as-image
  201. s2 := got.Slides[1]
  202. if s2.Bg != "#123456" {
  203. t.Errorf("slide 2 bg = %q, want #123456", s2.Bg)
  204. }
  205. if len(s2.Objects) != 1 || s2.Objects[0].Type != "image" {
  206. t.Errorf("slide 2: chart should import as one image, got %+v", s2.Objects)
  207. }
  208. }
  209. func TestTableCellHtmlFlatten(t *testing.T) {
  210. // table cells store a limited HTML subset in the editor; the pptx
  211. // writer must flatten it to text paragraphs (never leak literal tags)
  212. pres := &Presentation{
  213. Slides: []*Slide{{
  214. Objects: []*Object{{
  215. Type: "table", X: 10, Y: 10, W: 400, H: 100, Z: 1,
  216. Props: Props{Rows: [][]string{{"<b>x</b>&amp;y<br>z", "plain"}}},
  217. }},
  218. }},
  219. }
  220. data, err := BuildPptx(pres)
  221. if err != nil {
  222. t.Fatalf("BuildPptx failed: %v", err)
  223. }
  224. if bytes.Contains(data, []byte("&lt;b&gt;")) {
  225. t.Errorf("literal <b> tag leaked into the pptx output")
  226. }
  227. got, err := ParsePptx(data)
  228. if err != nil {
  229. t.Fatalf("ParsePptx failed: %v", err)
  230. }
  231. cell := got.Slides[0].Objects[0].Props.Rows[0][0]
  232. if cellText(cell) != "x&y\nz" {
  233. t.Errorf("cell text after roundtrip = %q, want %q", cellText(cell), "x&y\nz")
  234. }
  235. if !strings.Contains(cell, "font-weight:700") {
  236. t.Errorf("cell bold run lost in roundtrip: %q", cell)
  237. }
  238. }
  239. // cellText renders a table cell's storage HTML down to the text it shows,
  240. // so tests can assert on content without pinning the exact markup
  241. func cellText(html string) string {
  242. lines := htmlToLines(html)
  243. out := make([]string, 0, len(lines))
  244. for _, l := range lines {
  245. l = strings.TrimSpace(strings.ReplaceAll(l, "&#8203;", ""))
  246. if l != "" {
  247. out = append(out, l)
  248. }
  249. }
  250. return strings.Join(out, "\n")
  251. }
  252. func TestParsePresentationJSON(t *testing.T) {
  253. tests := []struct {
  254. name string
  255. json string
  256. wantErr bool
  257. }{
  258. {"valid", `{"size":[960,540],"slides":[{"objects":[]}]}`, false},
  259. {"no slides", `{"size":[960,540],"slides":[]}`, true},
  260. {"invalid json", `{not json`, true},
  261. {"empty", ``, true},
  262. }
  263. for _, tc := range tests {
  264. t.Run(tc.name, func(t *testing.T) {
  265. _, err := ParsePresentationJSON(tc.json)
  266. if (err != nil) != tc.wantErr {
  267. t.Errorf("err = %v, wantErr = %v", err, tc.wantErr)
  268. }
  269. })
  270. }
  271. }
  272. func TestHtmlToLines(t *testing.T) {
  273. tests := []struct {
  274. name string
  275. in string
  276. want []string
  277. }{
  278. {"plain", "hello", []string{"hello"}},
  279. {"br split", "a<br>b", []string{"a", "b"}},
  280. {"br self-closing", "a<br/>b", []string{"a", "b"}},
  281. {"divs", "<div>a</div><div>b</div>", []string{"a", "b"}},
  282. {"tags stripped", "<b>bold</b> text", []string{"bold text"}},
  283. {"entities", "a &amp; b &lt;c&gt;", []string{"a & b <c>"}},
  284. {"empty", "", []string{""}},
  285. }
  286. for _, tc := range tests {
  287. t.Run(tc.name, func(t *testing.T) {
  288. got := htmlToLines(tc.in)
  289. if len(got) != len(tc.want) {
  290. t.Fatalf("lines = %q, want %q", got, tc.want)
  291. }
  292. for i := range got {
  293. if got[i] != tc.want[i] {
  294. t.Errorf("line %d = %q, want %q", i, got[i], tc.want[i])
  295. }
  296. }
  297. })
  298. }
  299. }
  300. func TestHexColor(t *testing.T) {
  301. tests := []struct {
  302. in, fallback, want string
  303. }{
  304. {"#aabbcc", "000000", "AABBCC"},
  305. {"aabbcc", "000000", "AABBCC"},
  306. {"#abc", "000000", "AABBCC"},
  307. {"nonsense", "112233", "112233"},
  308. {"", "112233", "112233"},
  309. {"#12345", "FFFFFF", "FFFFFF"},
  310. }
  311. for _, tc := range tests {
  312. if got := hexColor(tc.in, tc.fallback); got != tc.want {
  313. t.Errorf("hexColor(%q) = %q, want %q", tc.in, got, tc.want)
  314. }
  315. }
  316. }
  317. func TestDecodeDataURL(t *testing.T) {
  318. tests := []struct {
  319. name string
  320. in string
  321. wantOk bool
  322. wantExt string
  323. }{
  324. {"png", testPngDataURL, true, "png"},
  325. {"jpeg", "data:image/jpeg;base64,aGVsbG8=", true, "jpeg"},
  326. {"not data url", "https://example.com/x.png", false, ""},
  327. {"no base64 marker", "data:image/png,rawdata", false, ""},
  328. {"bad base64", "data:image/png;base64,!!!!", false, ""},
  329. }
  330. for _, tc := range tests {
  331. t.Run(tc.name, func(t *testing.T) {
  332. _, ext, ok := decodeDataURL(tc.in)
  333. if ok != tc.wantOk {
  334. t.Fatalf("ok = %v, want %v", ok, tc.wantOk)
  335. }
  336. if ok && ext != tc.wantExt {
  337. t.Errorf("ext = %q, want %q", ext, tc.wantExt)
  338. }
  339. })
  340. }
  341. }
  342. func TestEmuConversion(t *testing.T) {
  343. if pxToEmu(960) != 9144000 {
  344. t.Errorf("pxToEmu(960) = %d, want 9144000", pxToEmu(960))
  345. }
  346. if px := emuToPx(9144000, 1.0); px < 959.9 || px > 960.1 {
  347. t.Errorf("emuToPx(9144000) = %v, want 960", px)
  348. }
  349. }
  350. func TestHtmlToLinesLists(t *testing.T) {
  351. tests := []struct {
  352. name string
  353. in string
  354. want []string
  355. }{
  356. {"bullet list", "intro<ul><li>alpha</li><li>beta</li></ul>",
  357. []string{"intro", "• alpha", "• beta"}},
  358. {"numbered list", "<ol><li>one</li><li>two</li><li>three</li></ol>",
  359. []string{"1. one", "2. two", "3. three"}},
  360. {"nested", "<ul><li>a<ul><li>a1</li></ul></li><li>b</li></ul>",
  361. []string{"• a", " • a1", "• b"}},
  362. }
  363. for _, tc := range tests {
  364. t.Run(tc.name, func(t *testing.T) {
  365. got := htmlToLines(tc.in)
  366. // drop trailing empties for comparison
  367. for len(got) > 0 && strings.TrimSpace(got[len(got)-1]) == "" {
  368. got = got[:len(got)-1]
  369. }
  370. if len(got) != len(tc.want) {
  371. t.Fatalf("lines = %q, want %q", got, tc.want)
  372. }
  373. for i := range got {
  374. if got[i] != tc.want[i] {
  375. t.Errorf("line %d = %q, want %q", i, got[i], tc.want[i])
  376. }
  377. }
  378. })
  379. }
  380. }