Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup improvements #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

qedi-r
Copy link
Contributor

@qedi-r qedi-r commented Feb 11, 2025

This automatically adds new MAC address as a list on the index page for adoption or ignoring permanently.

@bkuhlmann
Copy link
Member

bkuhlmann commented Feb 17, 2025

Hey Ryan. 👋 Sorry about the recent flurry of work going on this repository. I'm trying to get this project setup so all specs pass (including the CI builds) and making Docker easier to use too. Due to this, your branch is out of date. Would you mind rebasing to pick up the latest changes?

I think we should try to get this folded in but I'll need to take a closer look after your rebase.

@qedi-r
Copy link
Contributor Author

qedi-r commented Feb 17, 2025

Updated - I xit'ed the two failing tests that require an image to display. I assume we don't want to also support returning the setup image as a base64 encoded file.

Copy link
Member

@bkuhlmann bkuhlmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, I think there are some good ideas here but this needs some refinement first before this can be folded in.

Also, it'd be really nice to rebase your feature branch off of master because too many commits are being merged which makes this code harder to review.

Also, there are a bunch of ideas at play in this work so would be good to extract this out into smaller reviews if that make sense.

app.rb Outdated
@@ -3,6 +3,7 @@
require 'sinatra'
require 'sinatra/activerecord'
require 'debug'
require 'uri'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💭 This is being required but not used. Am I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. I thought it was used in the URI parser on line 17, but I guess if that went in without it, it's included by default. I'll remove.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

def change
add_column :devices, :adopted, :boolean, default: false, null: false

binding.break
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🪓 Looks like this debug statement was accidentally left in and is safe to remove.

app.rb Outdated
@@ -123,7 +165,7 @@ def devices_form(device)
special_function: 'sleep'
}.to_json
else
{ status: 404 }.to_json
return 404, { status: 404 }.to_json
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎨 Can this be used instead since it's more idiomatic Sinatra?

status 404
{ status: 404 }.to_json

ℹ️ We should also start adhering to RFC 9457 in order to render consistent errors. Can't tackle this now but planting the seed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, having a goal like that makes sense. I'm not experienced in Sinatra, and don't know the idioms (ruby generally seems to have it's fair amount). I just thought we should at least stop returning 200s for errors.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, no worries. My goal is to keep the code super consistent so it's easier for others to contribute. If patterns start to diverge then it's easy for folks to get confused and even do the wrong thing.

app.rb Outdated
# FIRMWARE SETUP
get '/api/setup/' do
content_type :json
@device = Device.find_by_mac_address(env['HTTP_ID']) # => ie "41:B4:10:39:A1:24"

if @device
status = 404
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎨 Could we encapsulate this functionality into a specialized object instead? Ideally, we should have something like this (which leverages the Command Pattern):

get "/api/setup/" do
  code, body = Register.new.call(**atrributes)

  status code
  body  
end

This would help keep the routes concise and clean while allowing registration functionality to be more easily tested without brining in the entire Rack stack.

db/schema.rb Outdated
ActiveRecord::Schema[8.0].define(version: 2024_12_12_172428) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_catalog.plpgsql"
ActiveRecord::Schema[8.0].define(version: 2025_02_09_163618) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💭 Is there a design, specification, or issue related to how these tables are suppose to be used. I think this might need to be extracted to a different code review because this seems like separate work to support?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should only be in the schedules branch - it was added here unintentionally - squashing the branch will fix it and remove it from the review.

app.rb Outdated
erb :"index"
end

def is_valid_mac(mac_address)
return mac_address && mac_address.match(/^[A-Fa-f0-9][048Cc26AaEe][-:]([A-Fa-f0-9]{2}[-:]){4}[A-Fa-f0-9]{2}$/)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎨 Could we encapsulate this functionality into a specialized object instead? This would also improve testing because you could test in isolation without having to incorporate the entire Rack stack.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm. good point. I should have noticed the testing implications here. 👍

Copy link
Member

@bkuhlmann bkuhlmann Feb 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, there are several other routes that need this too. I plan to fix that up soon so people have better examples on how to unit test and then fold in more routes like this for easier testing.

@@ -107,7 +107,7 @@ def is_valid_mac(mac_address)
end

get '/setup/adopt/:id' do
unadopted_device = UnadoptedDevice.find(id)
unadopted_device = UnadoptedDevice.find(params[:id])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎨 Can this be rebased into the previous commit? This looks like a fixup commit. Not need to have it exist on it's own.

Gemfile.lock Outdated
@@ -170,6 +170,7 @@ DEPENDENCIES
rspec
sinatra-activerecord
sqlite3
timecop
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🪓 Can this be removed since I don't see this gem used anywhere?

Gemfile Outdated
@@ -29,8 +29,10 @@ group :development, :test do
gem 'nokogiri'
gem 'rack-test'
gem 'rspec'
gem 'rubocop'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🪓 I agree we need to bring in RuboCop so this is a good catch...but I'd rather bring in the full tooling as managed by Caliber because there are a bunch of other corrections we need to make to this repository first. Let's delete this commit for now.

})
header('ACCESS_TOKEN', dev.api_key)
_, body = get_json '/api/display/'
expect(body['reset_firmware']).to eq(false)
end

it 'test_it_has_a_base64_display_path_with_a_device' do
# requries having a last image to display
xit 'test_it_has_a_base64_display_path_with_a_device_in_the_query_params' do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛑 Can we fix these instead of disabling them? It's important to keep all specs passing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I just kind of assumed these were the ones you referred to that we expect these to be broken. Easy to fix though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, thanks for fixing up. I need to work on improving the test suite more but this is a good start.

@bkuhlmann bkuhlmann mentioned this pull request Feb 17, 2025
@qedi-r qedi-r force-pushed the setup-improvements branch 2 times, most recently from 29fc8e9 to 9f6f098 Compare February 18, 2025 03:50
@qedi-r qedi-r force-pushed the setup-improvements branch from 9f6f098 to 7514e67 Compare February 20, 2025 12:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants