2023-09-29 14:46:13 -04:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'json'
|
|
|
|
require 'open3'
|
|
|
|
|
|
|
|
EXE = File.expand_path('exe/referator', __dir__).freeze
|
|
|
|
|
2023-09-29 19:52:45 -04:00
|
|
|
RAW_REFS = {
|
|
|
|
self: {
|
|
|
|
foo: {
|
|
|
|
'kind' => 'self',
|
|
|
|
'id' => '/blog/foo',
|
|
|
|
'slug' => 'blog-foo',
|
|
|
|
'text' => 'Foo',
|
|
|
|
}.freeze,
|
|
|
|
bar: {
|
|
|
|
'kind' => 'self',
|
|
|
|
'id' => '/blog/bar',
|
|
|
|
'slug' => 'blog-bar',
|
|
|
|
'text' => 'Bar',
|
|
|
|
}.freeze,
|
|
|
|
}.freeze,
|
|
|
|
link: {
|
|
|
|
example_com: {
|
|
|
|
'kind' => 'link',
|
|
|
|
'id' => 'example_com',
|
|
|
|
'slug' => 'example-com',
|
|
|
|
'url' => 'https://example.com',
|
|
|
|
'text' => 'Example Domain',
|
|
|
|
}.freeze,
|
|
|
|
causa_arcana_com: {
|
|
|
|
'kind' => 'link',
|
|
|
|
'id' => 'causa_arcana_com',
|
|
|
|
'slug' => 'causa-arcana-com',
|
|
|
|
'url' => 'https://causa-arcana.com',
|
|
|
|
'text' => 'Causa Arcana',
|
|
|
|
}.freeze,
|
|
|
|
}.freeze,
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
REFS = {
|
|
|
|
self: {
|
|
|
|
foo: RAW_REFS[:self][:foo].merge(
|
|
|
|
'anchor' => 'self-blog-foo',
|
|
|
|
'fragment' => '#self-blog-foo',
|
|
|
|
).freeze,
|
|
|
|
bar: RAW_REFS[:self][:bar].merge(
|
|
|
|
'anchor' => 'self-blog-bar',
|
|
|
|
'fragment' => '#self-blog-bar',
|
|
|
|
).freeze,
|
|
|
|
}.freeze,
|
|
|
|
link: {
|
|
|
|
example_com: RAW_REFS[:link][:example_com].merge(
|
|
|
|
'anchor' => 'link-example-com',
|
|
|
|
'fragment' => '#link-example-com',
|
|
|
|
).freeze,
|
|
|
|
causa_arcana_com: RAW_REFS[:link][:causa_arcana_com].merge(
|
|
|
|
'anchor' => 'link-causa-arcana-com',
|
|
|
|
'fragment' => '#link-causa-arcana-com',
|
|
|
|
).freeze,
|
|
|
|
}.freeze,
|
|
|
|
}.freeze
|
|
|
|
|
2023-09-29 14:46:13 -04:00
|
|
|
@stdin, @stdout, @wait_thr = Open3.popen2 EXE, chdir: __dir__
|
|
|
|
|
|
|
|
@stdin.sync = true
|
|
|
|
@stdout.sync = true
|
|
|
|
|
|
|
|
at_exit do
|
|
|
|
@stdin.close
|
|
|
|
@stdout.close
|
|
|
|
raise 'Script error' unless @wait_thr.value.success?
|
|
|
|
end
|
|
|
|
|
|
|
|
def cmd(name, data)
|
|
|
|
@stdin.puts "#{name} #{data.to_json}"
|
|
|
|
result = @stdout.gets.chomp
|
|
|
|
if result.start_with? 'OK '
|
2023-09-29 20:27:26 -04:00
|
|
|
result = JSON.parse(result[3..]).freeze
|
|
|
|
yield result if block_given?
|
|
|
|
result
|
2023-09-29 14:46:13 -04:00
|
|
|
elsif result.start_with? 'ERR '
|
|
|
|
raise "Error response: #{result[4..]}"
|
|
|
|
else
|
|
|
|
raise 'Invalid response'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-09-29 19:40:39 -04:00
|
|
|
#################
|
|
|
|
# REGISTER_KIND #
|
|
|
|
#################
|
|
|
|
|
2023-09-29 14:46:13 -04:00
|
|
|
cmd :REGISTER_KIND, :self
|
|
|
|
cmd :REGISTER_KIND, :link
|
|
|
|
|
2023-09-29 19:40:39 -04:00
|
|
|
###################
|
|
|
|
# REGISTER_FORMAT #
|
|
|
|
###################
|
|
|
|
|
2023-09-29 14:46:13 -04:00
|
|
|
cmd :REGISTER_FORMAT, { name: :html,
|
2023-09-29 16:38:39 -04:00
|
|
|
stdin: { notes: nil },
|
|
|
|
args: ['test/render.rb',
|
|
|
|
{ template: nil },
|
|
|
|
{ format: nil }] }
|
2023-09-29 19:52:45 -04:00
|
|
|
|
2023-09-29 17:02:57 -04:00
|
|
|
cmd :REGISTER_FORMAT, { name: :json,
|
|
|
|
stdin: { notes: nil },
|
|
|
|
args: ['test/render.rb',
|
|
|
|
{ template: nil },
|
|
|
|
{ format: nil }] }
|
2023-09-29 14:46:13 -04:00
|
|
|
|
2023-09-29 19:58:26 -04:00
|
|
|
################
|
|
|
|
# REGISTER_REF #
|
|
|
|
################
|
|
|
|
|
|
|
|
cmd :REGISTER_REF, RAW_REFS[:self][:foo]
|
|
|
|
cmd :REGISTER_REF, RAW_REFS[:self][:bar]
|
|
|
|
cmd :REGISTER_REF, RAW_REFS[:link][:example_com]
|
|
|
|
cmd :REGISTER_REF, RAW_REFS[:link][:causa_arcana_com]
|
2023-09-29 14:46:13 -04:00
|
|
|
|
2023-09-29 19:40:39 -04:00
|
|
|
#######
|
|
|
|
# REF #
|
|
|
|
#######
|
|
|
|
|
2023-09-29 20:27:26 -04:00
|
|
|
cmd :REF, { kind: :self, id: '/blog/foo' } do |result|
|
2023-09-29 19:52:45 -04:00
|
|
|
raise unless result == REFS[:self][:foo]
|
|
|
|
end
|
2023-09-29 14:46:13 -04:00
|
|
|
|
2023-09-29 20:27:26 -04:00
|
|
|
cmd :REF, { kind: :self, id: '/blog/bar' } do |result|
|
2023-09-29 19:52:45 -04:00
|
|
|
raise unless result == REFS[:self][:bar]
|
2023-09-29 14:46:13 -04:00
|
|
|
end
|
2023-09-29 19:40:39 -04:00
|
|
|
|
2023-09-29 20:27:26 -04:00
|
|
|
cmd :REF, { kind: :link, id: :example_com } do |result|
|
2023-09-29 19:52:45 -04:00
|
|
|
raise unless result == REFS[:link][:example_com]
|
|
|
|
end
|
2023-09-29 14:46:13 -04:00
|
|
|
|
2023-09-29 20:27:26 -04:00
|
|
|
cmd :REF, { kind: :link, id: :causa_arcana_com } do |result|
|
2023-09-29 19:52:45 -04:00
|
|
|
raise unless result == REFS[:link][:causa_arcana_com]
|
2023-09-29 14:46:13 -04:00
|
|
|
end
|
|
|
|
|
2023-09-29 19:40:39 -04:00
|
|
|
############
|
2023-09-29 20:27:26 -04:00
|
|
|
# MAKE_REF #
|
2023-09-29 19:40:39 -04:00
|
|
|
############
|
|
|
|
|
2023-09-29 20:27:26 -04:00
|
|
|
cmd :MAKE_REF, { kind: :self, id: '/blog/foo' } do |result|
|
|
|
|
raise unless result == REFS[:self][:foo]
|
|
|
|
end
|
|
|
|
|
|
|
|
cmd :MAKE_REF, { kind: :link, id: :example_com } do |result|
|
|
|
|
raise unless result == REFS[:link][:example_com]
|
|
|
|
end
|
|
|
|
|
|
|
|
cmd :MAKE_REF, { kind: :self, id: '/blog/bar' } do |result|
|
|
|
|
raise unless result == REFS[:self][:bar]
|
|
|
|
end
|
|
|
|
|
|
|
|
cmd :MAKE_REF, { kind: :link, id: :causa_arcana_com } do |result|
|
|
|
|
raise unless result == REFS[:link][:causa_arcana_com]
|
|
|
|
end
|
|
|
|
|
|
|
|
##############
|
|
|
|
# FETCH_NOTE #
|
|
|
|
##############
|
|
|
|
|
|
|
|
cmd :FETCH_NOTE, { kind: :self, id: '/blog/foo' } do |result|
|
2023-09-29 19:52:45 -04:00
|
|
|
raise unless result == REFS[:self][:foo].merge('index' => 1)
|
2023-09-29 19:40:39 -04:00
|
|
|
end
|
|
|
|
|
2023-09-29 20:27:26 -04:00
|
|
|
cmd :FETCH_NOTE, { kind: :self, id: '/blog/bar' } do |result|
|
|
|
|
raise unless result == REFS[:self][:bar].merge('index' => 2)
|
2023-09-29 19:40:39 -04:00
|
|
|
end
|
|
|
|
|
2023-09-29 20:27:26 -04:00
|
|
|
cmd :FETCH_NOTE, { kind: :link, id: :example_com } do |result|
|
|
|
|
raise unless result == REFS[:link][:example_com].merge('index' => 3)
|
2023-09-29 19:40:39 -04:00
|
|
|
end
|
|
|
|
|
2023-09-29 20:27:26 -04:00
|
|
|
cmd :FETCH_NOTE, { kind: :link, id: :causa_arcana_com } do |result|
|
2023-09-29 19:52:45 -04:00
|
|
|
raise unless result == REFS[:link][:causa_arcana_com].merge('index' => 4)
|
2023-09-29 19:40:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
####################
|
|
|
|
# RENDER_FOOTNOTES #
|
|
|
|
####################
|
2023-09-29 14:46:13 -04:00
|
|
|
|
2023-09-29 20:27:26 -04:00
|
|
|
cmd :RENDER_FOOTNOTES, :html do |result|
|
2023-09-29 17:19:05 -04:00
|
|
|
expected = <<~HTML
|
2023-09-29 14:46:13 -04:00
|
|
|
<h2>Self references</h2>
|
|
|
|
|
|
|
|
<ol>
|
|
|
|
<li id="self-blog-foo" value="1">
|
|
|
|
<a href="/blog/foo.html">Foo</a>
|
|
|
|
</li>
|
2023-09-29 20:27:26 -04:00
|
|
|
<li id="self-blog-bar" value="2">
|
2023-09-29 14:46:13 -04:00
|
|
|
<a href="/blog/bar.html">Bar</a>
|
|
|
|
</li>
|
|
|
|
</ol>
|
|
|
|
|
|
|
|
<h2>Link references</h2>
|
|
|
|
|
|
|
|
<ol>
|
2023-09-29 20:27:26 -04:00
|
|
|
<li id="link-example-com" value="3">
|
2023-09-29 14:46:13 -04:00
|
|
|
<a href="https://example.com">Example Domain</a>
|
|
|
|
</li>
|
|
|
|
<li id="link-causa-arcana-com" value="4">
|
|
|
|
<a href="https://causa-arcana.com">Causa Arcana</a>
|
|
|
|
</li>
|
|
|
|
</ol>
|
|
|
|
HTML
|
|
|
|
|
|
|
|
raise unless result == expected
|
|
|
|
end
|
2023-09-29 19:40:39 -04:00
|
|
|
|
2023-09-29 20:27:26 -04:00
|
|
|
cmd :RENDER_FOOTNOTES, :json do |result|
|
2023-09-29 17:19:05 -04:00
|
|
|
expected = <<~JSON
|
2023-09-29 17:02:57 -04:00
|
|
|
{
|
|
|
|
"self": [
|
|
|
|
{
|
|
|
|
"index": 1,
|
|
|
|
"kind": "self",
|
|
|
|
"id": "/blog/foo",
|
|
|
|
"slug": "blog-foo",
|
|
|
|
"anchor": "self-blog-foo",
|
|
|
|
"fragment": "#self-blog-foo",
|
|
|
|
"url": "/blog/foo.html",
|
|
|
|
"text": "Foo"
|
|
|
|
},
|
|
|
|
{
|
2023-09-29 20:27:26 -04:00
|
|
|
"index": 2,
|
2023-09-29 17:02:57 -04:00
|
|
|
"kind": "self",
|
|
|
|
"id": "/blog/bar",
|
|
|
|
"slug": "blog-bar",
|
|
|
|
"anchor": "self-blog-bar",
|
|
|
|
"fragment": "#self-blog-bar",
|
|
|
|
"url": "/blog/bar.html",
|
|
|
|
"text": "Bar"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"link": [
|
|
|
|
{
|
2023-09-29 20:27:26 -04:00
|
|
|
"index": 3,
|
2023-09-29 17:02:57 -04:00
|
|
|
"kind": "link",
|
|
|
|
"id": "example_com",
|
|
|
|
"slug": "example-com",
|
|
|
|
"anchor": "link-example-com",
|
|
|
|
"fragment": "#link-example-com",
|
|
|
|
"url": "https://example.com",
|
|
|
|
"text": "Example Domain"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"index": 4,
|
|
|
|
"kind": "link",
|
|
|
|
"id": "causa_arcana_com",
|
|
|
|
"slug": "causa-arcana-com",
|
|
|
|
"anchor": "link-causa-arcana-com",
|
|
|
|
"fragment": "#link-causa-arcana-com",
|
|
|
|
"url": "https://causa-arcana.com",
|
|
|
|
"text": "Causa Arcana"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
JSON
|
|
|
|
|
|
|
|
raise unless result == expected
|
|
|
|
end
|