openvibers/openvibe.actor Commits

An approval's expiry rounds down (#329)

OP OpenVibers committed 6 days ago · 984f2146d9 · 2 files, +7 −1

Changes

ddiff --git a/server/http/agent-pages.js b/server/http/agent-pages.js
iindex d09045a..7a7af92 100644
---- a/server/http/agent-pages.js
++++ b/server/http/agent-pages.js
@@@ -17,7 +17,10 @@ function ago(iso, now = Date.now()) {
if (!Number.isFinite(t)) return '';
const d = Math.round((t - now) / 1000);
const a = Math.abs(d);
-- const [n, unit] = a < 60 ? [a, 'second'] : a < 3600 ? [Math.round(a / 60), 'minute'] : a < 86400 ? [Math.round(a / 3600), 'hour'] : [Math.round(a / 86400), 'day'];
++ // A time to come rounds down (6.75 days left is "in 6 days": never promise more time than there is); a time past
++ // rounds to the nearest.
++ const r = d > 0 ? Math.floor : Math.round;
++ const [n, unit] = a < 60 ? [a, 'second'] : a < 3600 ? [r(a / 60), 'minute'] : a < 86400 ? [r(a / 3600), 'hour'] : [r(a / 86400), 'day'];
if (a < 45) return 'just now';
const words = `${n} ${unit}${n === 1 ? '' : 's'}`;
return d < 0 ? `${words} ago` : `in ${words}`;
ddiff --git a/test/approvals.test.js b/test/approvals.test.js
iindex 4b1cd1b..dc4f8ed 100644
---- a/test/approvals.test.js
++++ b/test/approvals.test.js
@@@ -138,6 +138,9 @@ const SAME = { 'sec-fetch-site': 'same-origin' };
const asked = (one.text.match(/Asked [\s\S]{0,240}/) || [''])[0];
assert.match(asked, /title="[^"]+ UTC">(just now|[^<]* ago)</, `a time a person reads, the exact one in its title: ${asked}`);
assert.match(one.text, /expires <time[^>]*>in [67] days</);
++ const later = await approvals.request(t.ctx.s, { owner, agentId: helper.id, kind: 'actor.action', summary: 'Run "ls" on your computer', payload: { argv: ['ls'], machine: 'dev_x' } });
++ await t.ctx.s.db.query("UPDATE actor_approvals SET expires_at = $2 WHERE id = $1", [later.id, new Date(Date.parse(t.ctx.s.iso()) + 6.75 * 86400000).toISOString()]);
++ assert.match((await t.get(`/me/approvals/${later.id}`, { as: ada })).text, /expires <time[^>]*>in 6 days</, 'never more time than there is');
assert.ok(one.text.includes('The exact request'), 'the details stay, after the plain words');
});
} finally {