Login
gpcr/spec/review_spec.cr
Login

[edit]

spec/review_spec.cr @ trunk

file_utils

require 'file_utils'
scale = 0.75
fontscale = 1.500
box wid 1.645 ht 0.340 with .nw at (0.035, -0.035) fill 0xCBD5E1 color 0xCBD5E1
box wid 1.645 ht 0.340 with .nw at (0.000, 0.000) fill 0x7C83E6 color 0x7C83E6
box wid 0.300 ht 0.065 rad 0.018 at (0.370, -0.022) fill 0xffffff color 0xffffff
line from (0.140, 0.000) to (0.140, -0.340) color white
line from (1.505, 0.000) to (1.505, -0.340) color white
text "require 'file_utils'" color white at (0.823, -0.170)

crystal source

require "file_utils"

json block

{
  "kind": "require",
  "name": "file_utils",
  "doc": null,
  "children": []
}

spec

require 'spec'
scale = 0.75
fontscale = 1.500
box wid 1.380 ht 0.340 with .nw at (0.035, -0.455) fill 0xCBD5E1 color 0xCBD5E1
box wid 1.380 ht 0.340 with .nw at (0.000, -0.420) fill 0x7C83E6 color 0x7C83E6
box wid 0.300 ht 0.065 rad 0.018 at (0.370, -0.443) fill 0xffffff color 0xffffff
line from (0.140, -0.420) to (0.140, -0.760) color white
line from (1.240, -0.420) to (1.240, -0.760) color white
text "require 'spec'" color white at (0.690, -0.590)

crystal source

require "spec"

json block

{
  "kind": "require",
  "name": "spec",
  "doc": null,
  "children": []
}

../src/gpcr/review

require '../src/gpcr/review'
scale = 0.75
fontscale = 1.500
box wid 2.343 ht 0.340 with .nw at (0.035, -0.875) fill 0xCBD5E1 color 0xCBD5E1
box wid 2.343 ht 0.340 with .nw at (0.000, -0.840) fill 0x7C83E6 color 0x7C83E6
box wid 0.300 ht 0.065 rad 0.018 at (0.370, -0.863) fill 0xffffff color 0xffffff
line from (0.140, -0.840) to (0.140, -1.180) color white
line from (2.203, -0.840) to (2.203, -1.180) color white
text "require '../src/gpcr/review'" color white at (1.172, -1.010)

crystal source

require "../src/gpcr/review"

json block

{
  "kind": "require",
  "name": "../src/gpcr/review",
  "doc": null,
  "children": []
}

private def with_review_tmp(&)
  dir = File.join(Dir.tempdir, "gpcr-review-spec-#{Process.pid}-#{Time.utc.to_unix_ms}")
  FileUtils.mkdir_p(dir)
  begin
    yield dir
  ensure
    FileUtils.rm_rf(dir)
  end
end

private def fossil_available? : Bool
  Process.run("fossil", ["version"], output: Process::Redirect::Close, error: Process::Redirect::Close).success?
end

describe GPCR::Review do
  it "extracts only pikchr fenced blocks from rendered markdown" do
    markdown = <<-MD
    # Example

    ```pikchr toggle
    scale = 0.75
    box wid 1 ht 1
    ```

    ```crystal
    puts "not a diagram"
    ```

    ```pikchr
    text "second"
    ```
    MD

    fences = GPCR::Review.extract_pikchr_fences(markdown)
    fences.size.should eq(2)
    fences[0].should contain("box wid 1 ht 1")
    fences[1].should contain(%(text "second"))
  end

  it "renders extracted pikchr fences to numbered SVG files" do
    pending!("fossil binary not available to render pikchr") unless fossil_available?

    markdown = <<-MD
    ```pikchr
    scale = 0.75
    box wid 1 ht 1
    ```
    MD

    with_review_tmp do |dir|
      svgs = GPCR::Review.write_svgs(markdown, File.join(dir, "before"))
      svgs.size.should eq(1)
      svgs[0].filename.should eq("pikchr-001.svg")
      File.read(File.join(dir, "before", "pikchr-001.svg")).should contain("<svg")
    end
  end

  it "writes lvgl lowering artifacts next to SVG files" do
    markdown = <<-MD
    ```pikchr
    scale = 1
    box wid 1 ht 1 with .nw at (0,1) fill 0x000000 color 0x000000
    line from (0,1) to (1,0) color 0xFFFFFF
    ```

    ```pikchr
    scale = 1
    circle rad 0.1 at (0,0) fill 0x000000 color 0x000000
    ```
    MD

    with_review_tmp do |dir|
      files = GPCR::Review.write_lvgl_artifacts(markdown, File.join(dir, "before"))
      files.map(&.status).should eq(["lowered", "unsupported"])
      lowered = File.read(File.join(dir, "before", "lvgl-001.cr"))
      lowered.should contain("Lvgl::Object.new(screen)")
      lowered.should contain("Lvgl::Widgets::Line.new(screen)")
      File.read(File.join(dir, "before", "lvgl-002.txt")).should contain("PUS_UNSUPPORTED")
    end
  end

  it "writes side-by-side HTML and Markdown review indexes" do
    before = GPCR::Review::RevisionArtifact.new(
      "before",
      "abc123",
      "before.md",
      [GPCR::Review::SvgArtifact.new(1, "pikchr-001.svg")],
      [GPCR::Review::LvglArtifact.new(1, "lvgl-001.cr", "lowered")]
    )
    after = GPCR::Review::RevisionArtifact.new(
      "after",
      "def456",
      "after.md",
      [GPCR::Review::SvgArtifact.new(1, "pikchr-001.svg")],
      [GPCR::Review::LvglArtifact.new(1, "lvgl-001.txt", "unsupported")]
    )

    with_review_tmp do |dir|
      GPCR::Review.write_index_html(dir, [before, after])
      GPCR::Review.write_index_markdown(dir, [before, after])

      html = File.read(File.join(dir, "index.html"))
      html.should contain("Before <code>abc123</code> vs after <code>def456</code>")
      html.should contain("before/pikchr-001.svg")
      html.should contain("after/pikchr-001.svg")
      html.should contain("before/lvgl-001.cr")
      html.should contain("lvgl unsupported")

      md = File.read(File.join(dir, "index.md"))
      md.should contain("Before `abc123` vs after `def456`")
      md.should contain("![before diagram 1](before/pikchr-001.svg)")
      md.should contain("![after diagram 1](after/pikchr-001.svg)")
      md.should contain("[lvgl lowered](before/lvgl-001.cr)")
      md.should contain("[lvgl unsupported](after/lvgl-001.txt)")
    end
  end
end