Skip to content

Commit 43a1100

Browse files
authored
A rounding gap under 0.05 hours needs no deflation reason (#114)
1 parent 65e782d commit 43a1100

8 files changed

Lines changed: 90 additions & 25 deletions

File tree

‎app/javascript/controllers/deflate_controller.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { Controller } from "@hotwired/stimulus"
1111
// where `claimed` is the coding alone), and a reason is due only below the most the rules allow.
1212
export default class extends Controller {
1313
static targets = [ "coding", "art", "artMax", "total", "hold", "why", "whyRequired", "whyOptional" ]
14-
static values = { claimed: Number, hoursClaimed: Number, cap: Number, artClaimed: Number, artTop: Number, artPercent: Number, earlierCoding: { type: Number, default: 0 }, earlierArt: { type: Number, default: 0 } }
14+
static values = { claimed: Number, hoursClaimed: Number, cap: Number, artClaimed: Number, artTop: Number, artPercent: Number, earlierCoding: { type: Number, default: 0 }, earlierArt: { type: Number, default: 0 }, roundingGap: Number }
1515

1616
setCoding(event) {
1717
const percent = Number(event.currentTarget.dataset.percent)
@@ -85,8 +85,8 @@ export default class extends Controller {
8585
// hold and rounded down, so it never passes the hold (Ship::Limits#art_top).
8686
#artAllowed(coding) { return coding >= this.capValue ? this.artTopValue : this.#floor(Math.min(this.artClaimedValue, this.#artHold(coding))) }
8787

88-
// A figure a hundredth or more under its top is a deflation, as the server counts it (Ship::Limits#approve).
89-
#short(top, value) { return top - value > 0.01 - 1e-9 }
88+
// A figure ROUNDING_GAP or more under its top is a deflation, as the server counts it (Ship::Limits#approve).
89+
#short(top, value) { return top - value > this.roundingGapValue - 1e-9 }
9090

9191
#round(hours) { return Math.round(hours * 100) / 100 }
9292

‎app/models/ship/limits.rb‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ class Ship::Limits
1111
ART_CAP_PERCENT = 30
1212
# Under this many hours apart is form rounding, not a cut.
1313
TOLERANCE = BigDecimal("0.01")
14+
# A figure the reviewer types within this many hours of its top is the top, and needs no deflation reason. Lapse shows art to the
15+
# hundredth, so adding two rounded figures can land a few hundredths under the most (ship 49: 17.3 + 1.27 against 18.6). The review
16+
# page reads this too (deflate_controller.js, through data-deflate-rounding-gap-value), so the two never disagree.
17+
ROUNDING_GAP = BigDecimal("0.05")
1418

1519
attr_reader :claimed, :ai_coding, :hackatime_ai, :reviewer_ai, :art, :lapse, :lapse_coding
1620

@@ -71,7 +75,7 @@ def coding_top
7175
def art_top(coding) = coding >= coding_top ? [ most - coding, 0 ].max : art_allowed_for(coding).floor(2)
7276

7377
# The coding and art hours the reviewer asked for, held to the limits as playground's approve_review! does: coding
74-
# to the AI cap, art to the art hold on that coding. nil asks for the most; a figure within a rounding of the most is
78+
# to the AI cap, art to the art hold on that coding. nil asks for the most; a figure within ROUNDING_GAP of the most is
7579
# the most. Returns [coding, art, below], where below says the reviewer went under what the rules allow, so "why
7680
# deflated" is due. The AI cut and the art hold need no reason. Both figures are in hundredths.
7781
def approve(coding_hours, art_hours) = approve_new(0, 0, coding_hours, art_hours)
@@ -91,7 +95,7 @@ def approve_new(earlier_coding, earlier_art, coding_hours, art_hours)
9195
coding = typed(coding_hours, top_coding)
9296
top_art = new_art_top(earlier_coding, earlier_art, coding)
9397
art = typed(art_hours, top_art)
94-
[ coding, art, top_coding - coding >= TOLERANCE || top_art - art >= TOLERANCE ]
98+
[ coding, art, top_coding - coding >= ROUNDING_GAP || top_art - art >= ROUNDING_GAP ]
9599
end
96100

97101
def ai_cut = coding - coding_cap
@@ -121,7 +125,7 @@ def hours(seconds) = seconds.to_i.to_d / 3600
121125
# within a rounding of it.
122126
def typed(figure, top)
123127
value = figure.nil? ? top : figure.to_d.clamp(0, top)
124-
top - value < TOLERANCE ? top : value.round(2)
128+
top - value < ROUNDING_GAP ? top : value.round(2)
125129
end
126130

127131
def ai_reason

‎app/views/admin/ships/_verdict.html.erb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
data: { turbo: false, controller: "deflate verdict draft", draft_url_value: draft_admin_ship_path(ship), verdict_picked_value: picked.to_s,
8484
deflate_claimed_value: coding_claimed.to_f, deflate_hours_claimed_value: limits.claimed.to_f, deflate_cap_value: coding_top.to_f,
8585
deflate_art_claimed_value: (art ? art_claimed.to_f : 0), deflate_art_top_value: (art ? art_top_at_cap.to_f : 0),
86-
deflate_art_percent_value: Ship::Limits::ART_CAP_PERCENT,
86+
deflate_art_percent_value: Ship::Limits::ART_CAP_PERCENT, deflate_rounding_gap_value: Ship::Limits::ROUNDING_GAP.to_f,
8787
deflate_earlier_coding_value: prior_coding.to_f, deflate_earlier_art_value: prior_art.to_f } do |form| %>
8888
<%= form.hidden_field :queue, value: params[:queue].presence, id: nil if params[:queue].present? %>
8989

‎test/controllers/admin/mcp_controller_test.rb‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,13 @@ class Admin::McpControllerTest < ActionDispatch::IntegrationTest
275275
Ship::EarlierShip.token = nil
276276
end
277277

278-
test "review_ship stores exactly the most the rules allow, and asks for words at any hundredth below it (D-111)" do
278+
test "review_ship stores exactly the most the rules allow, and asks for words at ROUNDING_GAP or more below it (D-111)" do
279279
ship = make_ship
280280
users(:ana).streak_activities.update_all(coded_seconds: 19_080)
281281
ship.update_columns(hours: 5.3, ai_coding_seconds: 0, art_seconds: 5542)
282282
Web.adapter = ->(_method, _url, _headers) { Web::Response.new(status: 200, headers: {}, body: "[]") }
283283

284-
below = mcp_tool(@token, "review_ship", { ship_id: ship.id, decision: "approved", coding_hours: 3.75, art_hours: 1.54, technical_features: "A second ball." })
284+
below = mcp_tool(@token, "review_ship", { ship_id: ship.id, decision: "approved", coding_hours: 3.5, art_hours: 1.54, technical_features: "A second ball." })
285285
assert_equal false, below["decided"]
286286
assert_match(/Say why you approve fewer hours/, below["message"])
287287

@@ -290,6 +290,28 @@ class Admin::McpControllerTest < ActionDispatch::IntegrationTest
290290
assert_equal [ BigDecimal("5.3"), nil ], [ ship.reload.review_hours, ship.deflation_reason ]
291291
end
292292

293+
test "review_ship takes a rounding gap under 0.05 hours as the most, with no reason (ship 49)" do
294+
ship = make_ship
295+
users(:ana).streak_activities.update_all(coded_seconds: 19.0 * 3600)
296+
ship.update_columns(hours: 18.6, ai_coding_seconds: 0, art_seconds: (1.2733 * 3600).round)
297+
Web.adapter = ->(_method, _url, _headers) { Web::Response.new(status: 200, headers: {}, body: "[]") }
298+
299+
done = mcp_tool(@token, "review_ship", { ship_id: ship.id, decision: "approved", coding_hours: 17.3, art_hours: 1.27, technical_features: "A second ball." })
300+
assert_equal true, done["decided"], done["message"]
301+
assert_equal [ BigDecimal("18.6"), nil ], [ ship.reload.review_hours, ship.deflation_reason ]
302+
end
303+
304+
test "review_ship still asks for a reason on a gap of 0.1 hours (ship 49's numbers)" do
305+
ship = make_ship
306+
users(:ana).streak_activities.update_all(coded_seconds: 19.0 * 3600)
307+
ship.update_columns(hours: 18.6, ai_coding_seconds: 0, art_seconds: (1.2733 * 3600).round)
308+
Web.adapter = ->(_method, _url, _headers) { Web::Response.new(status: 200, headers: {}, body: "[]") }
309+
310+
short = mcp_tool(@token, "review_ship", { ship_id: ship.id, decision: "approved", coding_hours: 17.3, art_hours: 1.17, technical_features: "A second ball." })
311+
assert_equal false, short["decided"]
312+
assert_match(/Say why you approve fewer hours/, short["message"])
313+
end
314+
293315
test "save_review_draft merges any subset into the draft, decides nothing, tells nobody and is logged (D-135)" do
294316
ship = make_ship
295317
ship.update_columns(art_seconds: 3600, lapse_seconds: 3600)

‎test/controllers/admin/ships_controller_test.rb‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -732,23 +732,35 @@ def stub_two_commits
732732
assert_select "#admin-ship-airtable", { text: /Deflation Justification/, count: 0 }, "nothing was deflated"
733733
end
734734

735-
test "a reason is due once a figure goes below the most, even by a hundredth (D-111)" do
735+
test "a reason is due once a figure goes ROUNDING_GAP or more below the most (D-111)" do
736736
users(:ana).streak_activities.update_all(coded_seconds: 19_080)
737737
@ship.update_columns(hours: 5.3, ai_coding_seconds: 0, art_seconds: 5542)
738738
sign_in_as(users(:orpheus))
739-
post review_admin_ship_path(@ship), params: { decision: "approved", coding_hours: "3.75", art_hours: "1.54", technical_features: "Two-ball physics." }
739+
post review_admin_ship_path(@ship), params: { decision: "approved", coding_hours: "3.5", art_hours: "1.54", technical_features: "Two-ball physics." }
740740
assert_response :unprocessable_entity
741741
assert_select ".ash__error", "Say why you approve fewer hours than the rules allow"
742742
assert_select "[data-deflate-target=why]:not([hidden])", 1, "the box for it is open"
743743
assert @ship.reload.in_review?
744744

745-
post review_admin_ship_path(@ship), params: { decision: "approved", coding_hours: "3.75", art_hours: "1.54", technical_features: "Two-ball physics.",
745+
post review_admin_ship_path(@ship), params: { decision: "approved", coding_hours: "3.5", art_hours: "1.54", technical_features: "Two-ball physics.",
746746
deflation_reason: "A menu was cut." }
747747
assert_redirected_to admin_ship_path(@ship)
748-
assert_equal [ BigDecimal("3.75"), BigDecimal("1.53"), "A menu was cut." ], [ @ship.reload.review_coding_hours, @ship.review_art_hours, @ship.deflation_reason ],
748+
assert_equal [ BigDecimal("3.5"), BigDecimal("1.5"), "A menu was cut." ], [ @ship.reload.review_coding_hours, @ship.review_art_hours, @ship.deflation_reason ],
749749
"the art is held to its share of the coding, rounded down"
750750
end
751751

752+
test "the review page passes the rounding gap to the deflate controller, and a gap under it needs no reason (ship 49)" do
753+
users(:ana).streak_activities.update_all(coded_seconds: 19_080)
754+
@ship.update_columns(hours: 5.3, ai_coding_seconds: 0, art_seconds: 5542)
755+
sign_in_as(users(:orpheus))
756+
get admin_ship_path(@ship)
757+
assert_select "form[data-deflate-rounding-gap-value=?]", Ship::Limits::ROUNDING_GAP.to_f.to_s
758+
759+
post review_admin_ship_path(@ship), params: { decision: "approved", coding_hours: "3.74", art_hours: "1.53", technical_features: "Two-ball physics." }
760+
assert_redirected_to admin_ship_path(@ship)
761+
assert_equal [ BigDecimal("5.3"), nil ], [ @ship.reload.review_hours, @ship.deflation_reason ]
762+
end
763+
752764
test "an approved ship with a cut that has no reason is not sent, and the page says so (D-111)" do
753765
unified_ysws_has_no_record
754766
@ship.update_columns(ai_coding_seconds: 3 * 3600, art_seconds: 0, hours: 10)

‎test/models/ship/limits_test.rb‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,12 @@ def lapse_limits(claimed, ai: 0, art: 0, lapse: 0, coding: 0)
135135

136136
test "below the top coding the art top is the art hold on that coding, rounded down" do
137137
l = limits(BigDecimal("5.3"), art: 5542.0 / 3600)
138-
assert_equal [ BigDecimal("3.74"), BigDecimal("1.53"), true ], l.approve(BigDecimal("3.74"), nil), "the art claimed is 1.5394, rounded down"
139-
assert_equal [ BigDecimal("3.76"), BigDecimal("1.5"), true ], l.approve(nil, BigDecimal("1.5"))
138+
assert_equal [ BigDecimal("3.5"), BigDecimal("1.5"), true ], l.approve(BigDecimal("3.5"), nil), "the art claimed is 1.5394, held to 1.5 beside 3.5 coding"
139+
assert_equal [ BigDecimal("3.76"), BigDecimal("1.4"), true ], l.approve(nil, BigDecimal("1.4"))
140140
assert_equal [ BigDecimal("3.76"), BigDecimal("1.54"), false ], l.approve(BigDecimal("3.755"), BigDecimal("1.535")), "within a rounding of the top is the top"
141-
assert_equal [ BigDecimal("3.74"), BigDecimal("1.53"), true ], l.approve(BigDecimal("3.74"), 9), "art typed above its top is held to it"
141+
assert_equal [ BigDecimal("3.5"), BigDecimal("1.5"), true ], l.approve(BigDecimal("3.5"), 9), "art typed above its top is held to it"
142+
assert_equal [ BigDecimal("3.76"), BigDecimal("1.54"), false ], l.approve(BigDecimal("3.74"), nil), "0.02 under is a rounding, so the top (ROUNDING_GAP)"
143+
assert_equal [ BigDecimal("3.76"), BigDecimal("1.44"), true ], l.approve(nil, BigDecimal("1.44")), "0.1 under needs a reason"
142144
end
143145

144146
test "typed figures are kept to the hundredth" do

‎test/models/ship_review_stages_test.rb‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,37 @@ class ShipReviewStagesTest < ActiveSupport::TestCase
103103
assert_operator uncut, :>, 20
104104
end
105105

106-
test "a figure a hundredth below the most needs a reason, and then the ship stores that figure (D-111)" do
106+
test "a figure well below the most needs a reason, and then the ship stores that figure (D-111)" do
107107
@ship.update_columns(hours: 5.3, ai_coding_seconds: 0, art_seconds: 5542)
108-
assert_not @ship.decide!("approved", by: @reviewer, coding_hours: "3.75", technical_features: "A rhythm grid.")
108+
assert_not @ship.decide!("approved", by: @reviewer, coding_hours: "3.5", technical_features: "A rhythm grid.")
109109
assert_equal [ "Say why you approve fewer hours than the rules allow" ], @ship.errors[:deflation_reason]
110110
assert @ship.reload.in_review?
111111

112-
assert @ship.decide!("approved", by: @reviewer, coding_hours: "3.75", technical_features: "A rhythm grid.", deflation_reason: "A menu was cut.")
112+
assert @ship.decide!("approved", by: @reviewer, coding_hours: "3.5", technical_features: "A rhythm grid.", deflation_reason: "A menu was cut.")
113113
@ship.reload
114-
assert_equal [ BigDecimal("3.75"), BigDecimal("1.53"), BigDecimal("5.28") ], [ @ship.review_coding_hours, @ship.review_art_hours, @ship.review_hours ]
114+
assert_equal [ BigDecimal("3.5"), BigDecimal("1.5"), BigDecimal("5") ], [ @ship.review_coding_hours, @ship.review_art_hours, @ship.review_hours ]
115115
assert_equal "A menu was cut.", @ship.deflation_reason, "the limits cut nothing, so only the reviewer's words"
116116
end
117117

118+
test "a gap under Ship::Limits::ROUNDING_GAP is rounding: it needs no reason and stores the most (ship 49)" do
119+
# Coding 17.3 and the Lapse art 1.2733 shown as 1.27, against a most of 18.6.
120+
@project.user.streak_activities.create!(activity_date: (Program::STARTS_AT + 2.days).to_date, coded_seconds: (8.6 * 3600).to_i)
121+
@ship.update_columns(hours: 18.6, ai_coding_seconds: 0, art_seconds: (1.2733 * 3600).round)
122+
limits = @ship.limits(@ship.counted_hours)
123+
assert_equal BigDecimal("18.6"), limits.most
124+
assert @ship.decide!("approved", by: @reviewer, coding_hours: "17.3", art_hours: "1.27", technical_features: "A rhythm grid.")
125+
@ship.reload
126+
assert_equal BigDecimal("18.6"), @ship.review_hours
127+
assert_nil @ship.deflation_reason
128+
end
129+
130+
test "a gap of 0.1 hours still needs a reason" do
131+
@project.user.streak_activities.create!(activity_date: (Program::STARTS_AT + 2.days).to_date, coded_seconds: (8.6 * 3600).to_i)
132+
@ship.update_columns(hours: 18.6, ai_coding_seconds: 0, art_seconds: (1.2733 * 3600).round)
133+
assert_not @ship.decide!("approved", by: @reviewer, coding_hours: "17.3", art_hours: "1.17", technical_features: "A rhythm grid.")
134+
assert_equal [ "Say why you approve fewer hours than the rules allow" ], @ship.errors[:deflation_reason]
135+
end
136+
118137
test "hours that went down with no words to say why can't be approved, even when no figure was typed below the limits (D-111)" do
119138
nothing_to_say = Struct.new(:claimed, :reason) { def approve(*) = [ BigDecimal("2"), BigDecimal("2"), false ] }.new(BigDecimal("5.3"), nil)
120139
@ship.define_singleton_method(:limits) { |*| nothing_to_say }

‎test/system/admin_review_test.rb‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class AdminReviewTest < ApplicationSystemTestCase
101101

102102
# D-111: 5.3 counted hours, no AI coding and 5542 s of art. Coding 3.76 and art 1.54 add up to the 5.3 claimed, and the form sends
103103
# them as they stand. A hundredth under either is a deflation, and the box for its reason opens.
104-
test "the form's own figures add up to the hours claimed with no reason, and a hundredth below asks for one (D-111)" do
104+
test "the form's own figures add up to the hours claimed with no reason, and 0.05 or more below asks for one (D-111)" do
105105
users(:ana).streak_activities.update_all(coded_seconds: 19_080)
106106
@ship.update_columns(hours: 5.3, ai_coding_seconds: 0, art_seconds: 5542)
107107
visit admin_ship_path(@ship)
@@ -110,16 +110,19 @@ class AdminReviewTest < ApplicationSystemTestCase
110110
assert_selector "[data-deflate-target=total]", text: "5.3"
111111
assert_no_field "Why deflated", visible: :visible
112112

113-
fill_in "coding_hours", with: "3.75"
113+
fill_in "coding_hours", with: "3.5"
114114
assert_field "Why deflated", visible: :visible
115115
fill_in "art_hours", with: "1.54"
116-
assert_equal "1.53", find("input[name=art_hours]").value, "art held at 30% of the lower coding, rounded down"
117-
assert_selector "[data-deflate-target=total]", text: "5.28"
116+
assert_equal "1.5", find("input[name=art_hours]").value, "art held at 30% of the lower coding, rounded down"
117+
assert_selector "[data-deflate-target=total]", text: "5"
118118

119119
fill_in "coding_hours", with: "3.76"
120-
fill_in "art_hours", with: "1.53"
120+
fill_in "art_hours", with: "1.4"
121121
assert_field "Why deflated", visible: :visible
122122

123+
fill_in "art_hours", with: "1.53"
124+
assert_no_field "Why deflated", visible: :visible, wait: 1 # a rounding, under Ship::Limits::ROUNDING_GAP
125+
123126
fill_in "art_hours", with: "1.54"
124127
assert_no_field "Why deflated", visible: :visible
125128
fill_in "Specific technical features", with: "A second ball with its own physics."
@@ -195,6 +198,9 @@ class AdminReviewTest < ApplicationSystemTestCase
195198
assert_no_selector "#admin-ship-deflation-says", visible: :all
196199

197200
fill_in "coding_hours", with: "7.49"
201+
assert_no_field "Why deflated", visible: :visible, wait: 1 # under Ship::Limits::ROUNDING_GAP is rounding
202+
203+
fill_in "coding_hours", with: "7.4"
198204
assert_field "Why deflated", visible: :visible
199205
assert_selector "[data-deflate-target=whyRequired]", text: /Required/
200206
assert_no_selector "#admin-ship-deflation-says", visible: :all

0 commit comments

Comments
 (0)