Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions scripts/teleton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Description:
// Obtiene el cómputo actual de la Teletón desde el endpoint de OCI
//
// Dependencies:
// moment
//
// Commands:
// hubot teleton - Obtiene el cómputo actual de la Teletón
//
// Author:
// @jorgeepunan

const fetch = require('node-fetch')
const moment = require('moment')

module.exports = robot => {
robot.respond(/teleton/i, async msg => {
try {
const url = 'https://axtoczxlicpz.objectstorage.sa-santiago-1.oci.customer-oci.com/n/axtoczxlicpz/b/teleton-bucket/o/computo.json'

const response = await fetch(url)

if (!response.ok) {
robot.emit('error', new Error(`HTTP ${response.status}`), msg, 'teleton')
return msg.reply('ocurrió un error al obtener el cómputo de la Teletón 2025')
}

const data = await response.json()

// Obtener la cifra del endpoint
const cifra = data.cifra

if (cifra === undefined || cifra === null) {
robot.emit('error', new Error(`Invalid data format: ${JSON.stringify(data)}`), msg, 'teleton')
return msg.reply('los datos no tienen el formato esperado')
}

// Usar la hora actual
const time = moment()

// Formatear según la especificación: Cómputo a las [HH:MM] [DD/MM/YYYY]: [CIFRA]
const formatted = `Cómputo a las ${time.format('HH:mm')} ${time.format('DD/MM/YYYY')}: $ ${cifra}`

msg.send(formatted)
} catch (err) {
robot.emit('error', err, msg, 'teleton')
msg.reply('ocurrió un error al obtener el cómputo de la Teletón 2025')
}
})
}
89 changes: 89 additions & 0 deletions test/teleton.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import 'coffee-script/register'
import test from 'ava'
import Helper from 'hubot-test-helper'
import nock from 'nock'

const helper = new Helper('../scripts/teleton.js')
const sleep = m => new Promise(resolve => setTimeout(() => resolve(), m))

test.beforeEach(t => {
t.context.room = helper.createRoom({ httpd: false })
})

test.afterEach(t => t.context.room.destroy())

test('Teleton - obtener cómputo válido', async t => {
const cifra = '$58.540.000.000'

nock('https://axtoczxlicpz.objectstorage.sa-santiago-1.oci.customer-oci.com')
.get('/n/axtoczxlicpz/b/teleton-bucket/o/computo.json')
.reply(200, {
cifra
})

t.context.room.user.say('user', 'hubot teleton')
await sleep(500)

const user = t.context.room.messages[0]
const hubot = t.context.room.messages[1]

t.deepEqual(user, ['user', 'hubot teleton'])
t.is(hubot[0], 'hubot')
t.true(/Cómputo a las \d{2}:\d{2} \d{2}\/\d{2}\/\d{4}: \$58\.540\.000\.000/i.test(hubot[1]))
})

test('Teleton - error HTTP 500', async t => {
nock('https://axtoczxlicpz.objectstorage.sa-santiago-1.oci.customer-oci.com')
.get('/n/axtoczxlicpz/b/teleton-bucket/o/computo.json')
.reply(500)

t.context.room.user.say('user', 'hubot teleton')
await sleep(500)

const user = t.context.room.messages[0]
const hubot = t.context.room.messages[1]

t.deepEqual(user, ['user', 'hubot teleton'])
t.deepEqual(hubot, [
'hubot',
'@user ocurrió un error al obtener el cómputo de la Teletón'
])
})

test('Teleton - error 404', async t => {
nock('https://axtoczxlicpz.objectstorage.sa-santiago-1.oci.customer-oci.com')
.get('/n/axtoczxlicpz/b/teleton-bucket/o/computo.json')
.reply(404)

t.context.room.user.say('user', 'hubot teleton')
await sleep(500)

const user = t.context.room.messages[0]
const hubot = t.context.room.messages[1]

t.deepEqual(user, ['user', 'hubot teleton'])
t.deepEqual(hubot, [
'hubot',
'@user ocurrió un error al obtener el cómputo de la Teletón'
])
})

test('Teleton - datos inválidos (sin cifra)', async t => {
nock('https://axtoczxlicpz.objectstorage.sa-santiago-1.oci.customer-oci.com')
.get('/n/axtoczxlicpz/b/teleton-bucket/o/computo.json')
.reply(200, {
invalid: 'data'
})

t.context.room.user.say('user', 'hubot teleton')
await sleep(500)

const user = t.context.room.messages[0]
const hubot = t.context.room.messages[1]

t.deepEqual(user, ['user', 'hubot teleton'])
t.deepEqual(hubot, [
'hubot',
'@user los datos no tienen el formato esperado'
])
})
Loading