"""The machine sheet against the local stack (Fase 2, front 1a). Run inside the API container: python -m tests.machine_log_test """ from uuid import uuid4 from app.core import db from tests.smoke_test import Client from tests.users_test import PASSWORD, order_fixture def final_stub(c, oid, owner): uid = uuid4() c.execute('''INSERT INTO dtf_local.uploads(id,owner,name,size,object_key,multipart_id,complete, expires_at,scan_state) VALUES(%s,%s,'machine-log-test.png',1,%s,'',true,now()+interval '1 day','clean')''', (uid, owner, f'machine-log-test/{uid}')) c.execute('''INSERT INTO dtf_local.order_files(id,order_id,upload_id,item_index,kind,note,created_by) VALUES(%s,%s,%s,0,'final','machine log test','test')''', (uuid4(), oid, uid)) def run(): su = Client() su.call('/session') tag = uuid4().hex[:6] machine = su.call('/operator/machines', {'name': f'Ficha {tag}', 'products': ['textil']}, operator=True)['id'] other = su.call('/operator/machines', {'name': f'Outra {tag}', 'products': ['textil']}, operator=True)['id'] email = f'ficha-{tag}@example.test' su.call('/operator/users', {'name': 'Ficha', 'email': email, 'role': 'operador', 'password': PASSWORD}, operator=True) op = Client() op.call('/session') op.call('/operator/login', {'email': email, 'password': PASSWORD}) op.call('/operator/me/machine', {'machine_id': machine}) owner = uuid4() # A clean starts the cycle, then two sheets leave Imprimindo for Revisão: # 12 m since the last clean, above the 10 m limit. op.call(f'/operator/machines/{machine}/entries', {'kind': 'event', 'item': 'clean'}) printed = [] with db.connect() as c: for _ in range(2): _, oid = order_fixture(c, owner, 'file', metres='6') c.execute("UPDATE dtf_local.orders SET state='imp', machine_id=%s WHERE id=%s", (machine, oid)) printed.append(str(oid)) for oid in printed: op.call(f'/operator/orders/{oid}/move', {'state': 'rev', 'version': 0}) board = op.call('/operator/board') row = next(m for m in board['machines'] if m['id'] == machine) clean = [a for a in row['alerts'] if a['key'] == 'clean'] assert clean and clean[0]['blocking'], row['alerts'] assert clean[0]['text'].endswith('limpe antes do próximo arquivo'), clean[0] # The next card into Imprimindo is refused before the server moves it. with db.connect() as c: _, first = order_fixture(c, owner, 'file') final_stub(c, first, owner) c.execute("UPDATE dtf_local.orders SET state='tra' WHERE id=%s", (first,)) refused = op.call(f'/operator/orders/{first}/move', {'state': 'imp', 'version': 0}, expected=409) assert refused['detail'].startswith('Antes do próximo arquivo: '), refused # "Adiar uma vez" lets exactly one card through; the next is refused again, # and the same alert cannot be postponed twice in one cycle. op.call(f'/operator/machines/{machine}/alerts/clean/snooze', {}) op.call(f'/operator/orders/{first}/move', {'state': 'imp', 'version': 0}) with db.connect() as c: _, second = order_fixture(c, owner, 'file') final_stub(c, second, owner) c.execute("UPDATE dtf_local.orders SET state='tra' WHERE id=%s", (second,)) refused = op.call(f'/operator/orders/{second}/move', {'state': 'imp', 'version': 0}, expected=409) assert 'Antes do próximo arquivo' in refused['detail'], refused op.call(f'/operator/machines/{machine}/alerts/clean/snooze', {}, expected=409) # Recording the clean resets the counter and releases the queue. op.call(f'/operator/machines/{machine}/entries', {'kind': 'event', 'item': 'clean', 'from_alert': True}) sheet = op.call(f'/operator/machines/{machine}/sheet') assert float(sheet['counters']['clean']['metres']) == 0, sheet['counters'] assert not [a for a in sheet['alerts'] if a['key'] == 'clean'], sheet['alerts'] op.call(f'/operator/orders/{second}/move', {'state': 'imp', 'version': 0}) # A supply without cost inherits the last cost of that same item. first_supply = op.call(f'/operator/machines/{machine}/entries', {'kind': 'supply', 'item': 'ink_w', 'quantity': 1, 'cost_cents': 18000}) assert first_supply['cost_cents'] == 18000, first_supply second_supply = op.call(f'/operator/machines/{machine}/entries', {'kind': 'supply', 'item': 'ink_w', 'quantity': 0.5}) assert second_supply['cost_cents'] == 18000, second_supply # An operator enters only on the machine of their session; Gestão anywhere. op.call(f'/operator/machines/{other}/entries', {'kind': 'event', 'item': 'clean'}, expected=403) gestao_email = f'gestao-{tag}@example.test' su.call('/operator/users', {'name': 'Gestão', 'email': gestao_email, 'role': 'gestao', 'password': PASSWORD}, operator=True) gestao = Client() gestao.call('/session') gestao.call('/operator/login', {'email': gestao_email, 'password': PASSWORD}) ges = gestao.call(f'/operator/machines/{other}/entries', {'kind': 'supply', 'item': 'film', 'quantity': 1, 'cost_cents': 0}) assert ges['cost_cents'] == 0, ges # Preço, data e mínimos are saved and the sheet returns them. su.call(f'/operator/machines/{machine}/sheet', {'price_cents': 1234500, 'installed_on': '2026-01-15', 'minimums': {'ink_w': 5}}, operator=True) sheet = op.call(f'/operator/machines/{machine}/sheet') assert sheet['machine']['price_cents'] == 1234500, sheet['machine'] assert sheet['machine']['installed_on'] == '2026-01-15', sheet['machine'] assert float(sheet['minimums']['ink_w']) == 5, sheet['minimums'] assert sheet['catalog']['supplies']['ink_w'] == ['Tinta branca', 'L'], sheet['catalog'] su.call(f'/operator/machines/{machine}/sheet', {'price_cents': 1234500, 'installed_on': '2026-01-15', 'minimums': {'ink_w': None}}, operator=True) assert 'ink_w' not in op.call(f'/operator/machines/{machine}/sheet')['minimums'] print('PASS: clean alert blocks and releases the pull, one snooze per cycle, ' 'supply cost inheritance, operator scope and the machine data') if __name__ == '__main__': run()