comic/odin/tests/export_phase3.odin
2026-05-21 06:10:32 +02:00

97 lines
3.8 KiB
Odin

package tests
import "core:fmt"
import "core:os"
import "core:strings"
import "core:testing"
import "../src/adapters"
import "../src/core"
import "../src/shared"
join2 :: proc(a, b: string) -> string {
return fmt.aprintf("%s/%s", a, b)
}
dispose_export_fixture :: proc(layouts: ^[]core.Page_Layout, panel_images: ^map[string]core.Panel_Image) {
for _, img in panel_images^ {
delete(img.url)
}
delete(panel_images^)
for l in layouts^ {
delete(l.panels)
}
delete(layouts^)
}
setup_export_fixture :: proc(t: ^testing.T) -> (tmp_dir: string, layouts: []core.Page_Layout, panel_images: map[string]core.Panel_Image) {
err: os.Error
tmp_dir, err = os.make_directory_temp("", "comic-export-test-*", context.temp_allocator)
if err != nil {
testing.expect(t, false, "failed to create temp dir")
return "", nil, nil
}
src_img := join2(tmp_dir, "src_panel.png")
img_bytes := []byte{137, 80, 78, 71, 13, 10, 26, 10}
werr := os.write_entire_file(src_img, img_bytes)
testing.expect(t, werr == nil, "failed to write source image")
panel_id := "panel_1"
layout_panel := core.Page_Layout_Panel{panel_id = panel_id, panel_number = 1, layout_cell = core.Layout_Cell{x = 0, y = 0, w = 1, h = 1}}
layout_panels_dyn: [dynamic]core.Page_Layout_Panel
append(&layout_panels_dyn, layout_panel)
layout := core.Page_Layout{page_number = 1, pattern_id = "grid-2x2", panels = layout_panels_dyn[:], width = 1000, height = 1400}
layouts_dyn: [dynamic]core.Page_Layout
append(&layouts_dyn, layout)
panel_images = make(map[string]core.Panel_Image)
panel_images[panel_id] = core.Panel_Image{url = fmt.aprintf("file://%s", src_img), width = 100, height = 100, seed = 1, prompt = ""}
delete(src_img)
return tmp_dir, layouts_dyn[:], panel_images
}
@test
export_png_and_cbz_create_files :: proc(t: ^testing.T) {
tmp_dir, layouts, panel_images := setup_export_fixture(t)
defer os.remove_all(tmp_dir)
defer dispose_export_fixture(&layouts, &panel_images)
png_out := join2(tmp_dir, "out_png.zip")
defer delete(png_out)
cbz_out := join2(tmp_dir, "out_cbz.cbz")
defer delete(cbz_out)
png_err := adapters.export_comic(png_out, layouts, panel_images, adapters.Export_Options{format = .PNG, page_size = .A4, dpi = 300, quality = 90})
testing.expect(t, shared.is_ok(png_err), "png export failed")
testing.expect(t, os.exists(png_out), "png archive output should exist")
png_data, prerr := os.read_entire_file(png_out, context.temp_allocator)
testing.expect(t, prerr == nil, "failed reading png zip")
testing.expect(t, len(png_data) >= 2 && string(png_data[:2]) == "PK", "png export should be a zip archive")
cbz_err := adapters.export_comic(cbz_out, layouts, panel_images, adapters.Export_Options{format = .CBZ, page_size = .A4, dpi = 300, quality = 90})
testing.expect(t, shared.is_ok(cbz_err), "cbz export failed")
testing.expect(t, os.exists(cbz_out), "cbz output should exist")
cbz_data, crerr := os.read_entire_file(cbz_out, context.temp_allocator)
testing.expect(t, crerr == nil, "failed reading cbz")
testing.expect(t, len(cbz_data) >= 2 && string(cbz_data[:2]) == "PK", "cbz should be a zip archive")
}
@test
export_pdf_creates_pdf_file :: proc(t: ^testing.T) {
tmp_dir, layouts, panel_images := setup_export_fixture(t)
defer os.remove_all(tmp_dir)
defer dispose_export_fixture(&layouts, &panel_images)
pdf_out := join2(tmp_dir, "out.pdf")
defer delete(pdf_out)
pdf_err := adapters.export_comic(pdf_out, layouts, panel_images, adapters.Export_Options{format = .PDF, page_size = .A4, dpi = 300, quality = 90})
testing.expect(t, shared.is_ok(pdf_err), "pdf export failed")
testing.expect(t, os.exists(pdf_out), "pdf output should exist")
data, rerr := os.read_entire_file(pdf_out, context.temp_allocator)
testing.expect(t, rerr == nil, "failed to read pdf output")
testing.expect(t, strings.has_prefix(string(data), "%PDF-"), "pdf output should start with %PDF-")
}