Files
BEYOND/addons/beyond_base/static/tests/beyond_camara.test.js

112 lines
4.0 KiB
JavaScript

/** Part of Beyond. Collina Systems proprietary module. */
import { expect, test, describe } from "@odoo/hoot";
import { patchWithCleanup } from "@web/../tests/web_test_helpers";
import { patchTranslations } from "@web/../tests/_framework/translation_test_helpers";
import { browser } from "@web/core/browser/browser";
import {
SIN_CAMARA,
SIN_CONEXION_SEGURA,
SIN_PERMISO,
beyondCausaDeCamara,
beyondMensajeDeCamara,
} from "@beyond_base/barcode/beyond_camara_mensaje";
/**
* Por qué no abrió la cámara.
*
* El core culpa SIEMPRE al permiso («Odoo needs your authorization first»):
* dice un nombre que no es Beyond y, en un equipo sin cámara, manda a buscar
* un permiso que no arreglaría nada. Aquí se separan las tres causas.
*
* Es justo lo que no se puede comprobar a mano: dev corre en localhost, que
* SIEMPRE es contexto seguro y con la cámara del equipo puesta.
*/
describe.current.tags("desktop");
const UNA_CAMARA = [{ kind: "videoinput" }, { kind: "audioinput" }];
const SIN_VIDEO = [{ kind: "audioinput" }];
function conCamaras(lista) {
patchWithCleanup(browser, {
navigator: {
mediaDevices: {
getUserMedia: () => {},
enumerateDevices: async () => lista,
},
},
});
}
function texto(causa) {
patchTranslations();
return beyondMensajeDeCamara(causa).toString();
}
// ── La causa ────────────────────────────────────────────────────────────
test("sin mediaDevices la causa es la conexión, no el permiso", async () => {
patchWithCleanup(browser, { navigator: { mediaDevices: undefined } });
expect(await beyondCausaDeCamara()).toBe(SIN_CONEXION_SEGURA);
});
test("con cámara enumerada, la causa es el permiso", async () => {
conCamaras(UNA_CAMARA);
expect(await beyondCausaDeCamara()).toBe(SIN_PERMISO);
});
test("sin ninguna cámara enumerada, la causa es que no hay cámara", async () => {
// El discriminador: los navegadores listan las cámaras aunque el permiso
// esté denegado, así que cero videoinput sí significa que no hay.
conCamaras(SIN_VIDEO);
expect(await beyondCausaDeCamara()).toBe(SIN_CAMARA);
});
test("si enumerar falla no se inventa una causa", async () => {
patchWithCleanup(browser, {
navigator: {
mediaDevices: {
getUserMedia: () => {},
enumerateDevices: async () => {
throw new Error("bloqueado");
},
},
},
});
expect(await beyondCausaDeCamara()).toBe(SIN_PERMISO);
});
// ── El mensaje ──────────────────────────────────────────────────────────
test("sin conexión segura se dice eso, no que falten permisos", () => {
const mensaje = texto(SIN_CONEXION_SEGURA);
expect(mensaje).toInclude("conexión segura");
expect(mensaje).not.toInclude("permiso");
});
test("sin cámara no se manda a buscar un permiso que no existe", () => {
const mensaje = texto(SIN_CAMARA);
expect(mensaje).toInclude("no tiene cámara");
expect(mensaje).not.toInclude("permiso");
});
test("con la cámara bloqueada se dice dónde está el permiso", () => {
const mensaje = texto(SIN_PERMISO);
expect(mensaje).toInclude("permiso");
expect(mensaje).toInclude("candado");
});
test("ningún mensaje dice un nombre que no sea Beyond", () => {
for (const causa of [SIN_CONEXION_SEGURA, SIN_CAMARA, SIN_PERMISO]) {
expect(texto(causa)).not.toInclude("Odoo");
}
});
test("los tres dicen qué hacer mientras tanto", () => {
// Un error que solo informa deja a la persona con la caja en la mano y
// sin salida.
for (const causa of [SIN_CONEXION_SEGURA, SIN_CAMARA, SIN_PERMISO]) {
expect(texto(causa)).toInclude("nombre del artículo");
}
});