"""An order in Imprimindo changes machine: Gestão says which part the new one resumes from, the reason stays for the Revisor, and the old machine's .prt files from that part on are named for the operator to delete (Rodada 5, D4; Marcus, 2026-12-01).""" from uuid import uuid4 from app.core import db from tests.smoke_test import Client from tests.users_test import PASSWORD, order_fixture, signed_in def run(): client = Client() client.call('/session') tag = uuid4().hex[:6] machine = client.call('/operator/machines', {'name': f'Transfere A {tag}', 'products': ['textil']}, operator=True)['id'] other = client.call('/operator/machines', {'name': f'Transfere B {tag}', 'products': ['textil']}, operator=True)['id'] email = f'transfere-{tag}@example.test' operator_id = client.call('/operator/users', {'name': 'Operador', 'email': email, 'role': 'operador', 'password': PASSWORD}, operator=True)['id'] owner = uuid4() quote = order = None try: with db.connect() as c: quote, order = order_fixture(c, owner, 'file') upload = uuid4() c.execute('''INSERT INTO dtf_local.uploads(id,owner,name,size,object_key,multipart_id,complete, expires_at,scan_state) VALUES(%s,%s,'transfer-test.png',1,%s,'',true,now()+interval '1 day','clean')''', (upload, owner, f'originals/transfer-test-{upload}')) c.execute('''UPDATE dtf_local.orders SET state='imp', machine_id=%s, machine_chosen=true WHERE id=%s''', (machine, order)) for seq, state in enumerate(('consumed', 'consumed', 'written', 'queued'), 1): c.execute('''INSERT INTO dtf_local.hot_parts(order_id,round,seq,item_index,upload_id,name,state) VALUES(%s,1,%s,0,%s,%s,%s)''', (order, seq, upload, f'parte {seq}.png', state)) path = f'/operator/orders/{order}/transfer' # The reason is required, even if only spaces. answer = client.call(path, {'machine_id': other, 'reason': ' '}, operator=True, expected=422) assert answer['detail'] == 'Escreva o motivo da transferência.', answer # It only goes to another machine. answer = client.call(path, {'machine_id': machine, 'reason': 'quebrou'}, operator=True, expected=409) assert answer['detail'] == 'O pedido já está nessa máquina.', answer # The old machine took parts 1 and 2: the part to resume from is asked, # and it must be one of the order's. answer = client.call(path, {'machine_id': other, 'reason': 'quebrou'}, operator=True, expected=422) assert answer['detail'] == 'Escolha de qual parte a nova máquina recomeça.', answer answer = client.call(path, {'machine_id': other, 'reason': 'quebrou', 'resume_from': 9}, operator=True, expected=422) assert answer['detail'] == 'Essa parte não existe neste pedido.', answer # Part 1 was printed, part 2 was waiting in the Hoson queue: the new # round resumes from part 2, renumbered, and the old machine's part 2 # .prt is to be deleted; parts 3 and 4 leave its folder. assert client.call(path, {'machine_id': other, 'reason': ' quebrou a cabeça ', 'resume_from': 2}, operator=True) == {'ok': True, 'delete_prt': ['parte 2.prt']} with db.connect() as c: parts = c.execute('''SELECT round,seq,state,name FROM dtf_local.hot_parts WHERE order_id=%s ORDER BY round,seq''', (order,)).fetchall() old = {row['seq']: row['state'] for row in parts if row['round'] == 1} assert old == {1: 'consumed', 2: 'consumed', 3: 'withdraw', 4: 'gone'}, old new = [row for row in parts if row['round'] == 2] assert [row['seq'] for row in new] == [1, 2, 3], parts assert [row['name'] for row in new] == ['parte 2.png', 'parte 3.png', 'parte 4.png'], new assert all(row['state'] == 'queued' for row in new), new moved = c.execute('SELECT machine_id,machine_chosen FROM dtf_local.orders WHERE id=%s', (order,)).fetchone() assert str(moved['machine_id']) == other and moved['machine_chosen'], moved row = c.execute('SELECT * FROM dtf_local.machine_transfers WHERE order_id=%s', (order,)).fetchone() assert row['resumed_from'] == 2 and row['reason'] == 'quebrou a cabeça', row assert row['delete_prt'] == ['parte 2.prt'], row assert (row['from_name'], row['to_name']) == (f'Transfere A {tag}', f'Transfere B {tag}'), row mine = next(o for o in client.call('/operator/board', operator=True)['orders'] if o['id'] == str(order)) assert mine['transfers'][0]['resumed_from'] == 2, mine.get('transfers') assert mine['transfers'][0]['delete_prt'] == ['parte 2.prt'], mine.get('transfers') # The board lists the new round's parts for the next transfer's dialog. assert [p['seq'] for p in mine['hot_parts']] == [1, 2, 3], mine.get('hot_parts') # Only Gestão and the administrators move a card between machines. op = signed_in(email) op.call(path, {'machine_id': machine, 'reason': 'outra tentativa'}, expected=403) # Outside Imprimindo there is no transfer. with db.connect() as c: c.execute("UPDATE dtf_local.orders SET state='tra' WHERE id=%s", (order,)) answer = client.call(path, {'machine_id': machine, 'reason': 'fora de hora'}, operator=True, expected=409) assert answer['detail'] == 'Só um pedido em Imprimindo muda de máquina por aqui.', answer print('PASS: an order in Imprimindo transfers machine with its reason and the part to resume from, ' 'the old machine unloads what was not taken and its .prt to delete is named') finally: if order is not None: with db.connect() as c: c.execute('DELETE FROM dtf_local.orders WHERE id=%s', (order,)) c.execute('DELETE FROM dtf_local.quotes WHERE id=%s', (quote,)) if operator_id: client.call(f'/operator/users/{operator_id}/delete', {}, operator=True) for machine_id in (machine, other): client.call(f'/operator/machines/{machine_id}/delete', {}, operator=True) if __name__ == '__main__': run()