1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
require_relative "test_helper"
require "rack/test"
require "tempfile"

class TestApp < Minitest::Test
  include Rack::Test::Methods

  def app
    Domus::Web
  end

  def domus = Domus::Web.opts.fetch(:app)

  # Every test starts from the shared seed baseline, so dev and the suite
  # exercise the same Domus::Seeds path. Tests that need a clean slate call
  # #wipe; tests that assert on counts compare deltas around the action.
  def setup
    wipe
    Domus::Seeds.call(domus)
  end

  def wipe
    domus.db[:asset_attachments].delete
    domus.db[:assets].delete
    domus.db[:files].delete
  end

  def test_root_renders_home
    get "/"
    assert_equal 200, last_response.status
    assert_includes last_response.body, "Domus"
    assert_includes last_response.body, "Recent assets"
  end

  def test_root_lists_recent_assets_newest_first
    wipe
    now = Time.now
    domus.db[:assets].insert(name: "Older asset", created_at: now - 86_400)
    domus.db[:assets].insert(name: "Newer asset", created_at: now)

    get "/"
    assert_equal 200, last_response.status
    body = last_response.body
    assert_includes body, "Older asset"
    assert_includes body, "Newer asset"
    assert_operator body.index("Newer asset"), :<, body.index("Older asset")
  end

  def test_root_links_each_asset_to_its_detail_page
    id = domus.db[:assets].insert(name: "Laptop", created_at: Time.now)

    get "/"
    assert_equal 200, last_response.status
    assert_includes last_response.body, %(href="/assets/#{id}")
  end

  def test_root_empty_state
    wipe
    get "/"
    assert_equal 200, last_response.status
    assert_includes last_response.body, "Nothing tracked yet."
  end

  def test_root_capture_actions_open_picker_in_place
    get "/"
    body = last_response.body
    # The capture flow is embedded, so the split button opens the picker in
    # place (primary + alternate) rather than navigating away.
    assert_includes body, "captureApp()"
    assert_includes body, "capturePrimary()"
    assert_includes body, "captureAlternate()"
    refute_includes body, 'href="/capture"'
  end

  def test_asset_detail_renders_title
    id = domus.db[:assets].insert(name: "Bosch 800 dishwasher", created_at: Time.now)

    get "/assets/#{id}"
    assert_equal 200, last_response.status
    body = last_response.body
    assert_includes body, "Bosch 800 dishwasher"
    assert_includes body, "Assets" # breadcrumb back to the index
  end

  def test_asset_detail_renders_description
    id = domus.db[:assets].insert(
      name: "Bosch 800 dishwasher",
      description: "Stainless interior, third rack.\n\nReplaces the GE that flooded.",
      created_at: Time.now
    )

    get "/assets/#{id}"
    assert_equal 200, last_response.status
    body = last_response.body
    assert_includes body, "Stainless interior, third rack."
    assert_includes body, "Replaces the GE that flooded."
  end

  def test_asset_detail_omits_description_when_absent
    id = domus.db[:assets].insert(name: "Untitled", created_at: Time.now)

    get "/assets/#{id}"
    assert_equal 200, last_response.status
    refute_includes last_response.body, 'class="desc"'
  end

  def test_asset_detail_missing_is_404
    get "/assets/999999"
    assert_equal 404, last_response.status
  end

  def test_asset_detail_renders_attached_images
    now = Time.now
    asset_id = domus.db[:assets].insert(name: "Dishwasher", created_at: now)
    file_id = domus.db[:files].insert(extension: ".png", created_at: now)
    domus.db[:asset_attachments].insert(asset_id:, file_id:, created_at: now)

    get "/assets/#{asset_id}"
    assert_equal 200, last_response.status
    assert_includes last_response.body, %(src="/files/#{file_id}.png")
  end

  def test_asset_detail_without_images_shows_photos_add_affordance
    id = domus.db[:assets].insert(name: "Bare", created_at: Time.now)

    get "/assets/#{id}"
    assert_equal 200, last_response.status
    # The photos section always renders (with the add affordance); there
    # just aren't any <img> tiles when nothing is attached.
    assert_includes last_response.body, "addphoto"
    refute_includes last_response.body, "<img"
  end

  def test_get_file_serves_stored_image
    post "/files", "file" => upload("photo.png", "image/png", "fake-png-bytes")
    file = domus.db[:files].order(:id).last

    get "/files/#{file[:id]}#{file[:extension]}"
    assert_equal 200, last_response.status
    assert_equal "image/png", last_response.headers["Content-Type"]
    assert_includes last_response.headers["Cache-Control"].to_s, "immutable"
    assert_equal "fake-png-bytes", last_response.body
  end

  def test_get_missing_file_is_404
    get "/files/999999.png"
    assert_equal 404, last_response.status
  end

  def test_upload_image_saves_file_and_redirects
    before = domus.db[:files].count
    post "/files", "file" => upload("photo.png", "image/png", "fake-png-bytes")

    assert_equal 302, last_response.status
    assert_equal before + 1, domus.db[:files].count
    row = domus.db[:files].order(:id).last
    assert_equal ".png", row[:extension]
    assert_equal "fake-png-bytes", File.read(domus.file_path(row))
  end

  def test_upload_without_file_is_rejected
    before = domus.db[:files].count
    post "/files", {}

    assert_equal 422, last_response.status
    assert_equal before, domus.db[:files].count
  end

  def test_upload_rejects_non_image
    before = domus.db[:files].count
    post "/files", "file" => upload("notes.txt", "text/plain", "hello")

    assert_equal 422, last_response.status
    assert_equal before, domus.db[:files].count
  end

  def test_upload_rejects_unsupported_extension
    before = domus.db[:files].count
    post "/files", "file" => upload("sketch.svg", "image/svg+xml", "<svg/>")

    assert_equal 422, last_response.status
    assert_equal before, domus.db[:files].count
  end

  def test_upload_rejects_oversized_file
    before = domus.db[:files].count
    oversized = "x" * (Domus::Web::MAX_UPLOAD_BYTES + 1)
    post "/files", "file" => upload("huge.png", "image/png", oversized)

    assert_equal 422, last_response.status
    assert_equal before, domus.db[:files].count
  end

  def test_upload_with_asset_name_creates_asset_and_attachment
    before = domus.db[:assets].count
    post "/files", "file" => upload("photo.png", "image/png", "bytes"), "asset_names[]" => "Laptop"

    assert_equal 302, last_response.status
    assert_equal before + 1, domus.db[:assets].count
    asset = domus.db[:assets].order(:id).last
    assert_equal "Laptop", asset[:name]
    file = domus.db[:files].order(:id).last
    attachment = domus.db[:asset_attachments].where(asset_id: asset[:id]).first
    refute_nil attachment
    assert_equal file[:id], attachment[:file_id]
  end

  def test_upload_with_multiple_asset_names_creates_all
    assets_before = domus.db[:assets].count
    attachments_before = domus.db[:asset_attachments].count
    post "/files", "file" => upload("photo.png", "image/png", "bytes"),
      "asset_names[]" => ["Camera", "Laptop"]

    assert_equal 302, last_response.status
    assert_equal assets_before + 2, domus.db[:assets].count
    assert_equal attachments_before + 2, domus.db[:asset_attachments].count
  end

  def test_upload_with_blank_asset_names_ignored
    assets_before = domus.db[:assets].count
    attachments_before = domus.db[:asset_attachments].count
    post "/files", "file" => upload("photo.png", "image/png", "bytes"),
      "asset_names[]" => ["", "  "]

    assert_equal 302, last_response.status
    assert_equal assets_before, domus.db[:assets].count
    assert_equal attachments_before, domus.db[:asset_attachments].count
  end

  def test_upload_without_asset_names_creates_no_assets
    assets_before = domus.db[:assets].count
    attachments_before = domus.db[:asset_attachments].count
    post "/files", "file" => upload("photo.png", "image/png", "bytes")

    assert_equal 302, last_response.status
    assert_equal assets_before, domus.db[:assets].count
    assert_equal attachments_before, domus.db[:asset_attachments].count
  end

  private

  def upload(filename, type, contents)
    file = Tempfile.new(filename)
    file.binmode
    file.write(contents)
    file.rewind
    Rack::Test::UploadedFile.new(file.path, type, original_filename: filename)
  end
end