diff --git a/src/actions/movie/get-movie-info.spec.ts b/src/actions/movie/get-movie-info.spec.ts index e169f6d..646afd8 100644 --- a/src/actions/movie/get-movie-info.spec.ts +++ b/src/actions/movie/get-movie-info.spec.ts @@ -1,24 +1,63 @@ -import { getMovieInfo } from '@/actions/movie/get-movie-info.js'; +import { + getMovieInfo, + getMovieOverview, +} from '@/actions/movie/get-movie-info.js'; import { generateMovie } from '@/fakers/moviedb.js'; -test('should return the movie info', () => { - const movie = generateMovie({ - releaseDate: new Date('1986-05-16'), +describe('getMovieOverview', () => { + test('should return the overview if it is less than 350 characters', () => { + const overview = 'a'.repeat(349); + const result = getMovieOverview(overview); + + expect(result).toBe(overview); }); - const movieUrl = `https://www.themoviedb.org/movie/${movie.id}`; - const result = getMovieInfo(movie); + test('should return the overview truncated to 350 characters', () => { + const overview = 'a'.repeat(351); + const result = getMovieOverview(overview); - expect(result).toContain(movie.overview); - expect(result).toContain(movie.title); - expect(result).toContain(movieUrl); - expect(result).toContain('#movie #film #cinema'); + expect(result).toBe('a'.repeat(347) + '...'); + }); }); -test('should trim the overview to 350 characters', () => { - const movie = generateMovie(); - movie.overview = 'a'.repeat(500); +describe('getMovieInfo', () => { + test('contains the movie title and release date', () => { + const movie = generateMovie({ + releaseDate: new Date('1986-05-16'), + }); + const result = getMovieInfo(movie); + + expect(result).toContain(movie.title); + expect(result).toContain('May 16th, 1986'); + }); + + test('contains the entry URL', () => { + const movie = generateMovie(); + const result = getMovieInfo(movie); + + expect(result).toContain(`https://www.themoviedb.org/movie/${movie.id}`); + }); + + test('contains post tags', () => { + const movie = generateMovie(); + const result = getMovieInfo(movie); - const result = getMovieInfo(movie); - expect(result).toContain('a'.repeat(350)); + expect(result).toContain('#movie #film #cinema'); + }); + + test('contains the full overview if it is less than 350 characters', () => { + const movie = generateMovie(); + movie.overview = 'a'.repeat(300); + const result = getMovieInfo(movie); + + expect(result).toContain(movie.overview); + }); + + test('contains the truncated overview if it is more than 350 characters', () => { + const movie = generateMovie(); + movie.overview = 'a'.repeat(351); + const result = getMovieInfo(movie); + + expect(result).toContain('a'.repeat(347) + '...'); + }); }); diff --git a/src/actions/movie/get-movie-info.ts b/src/actions/movie/get-movie-info.ts index 3ff89a5..d4ace7f 100644 --- a/src/actions/movie/get-movie-info.ts +++ b/src/actions/movie/get-movie-info.ts @@ -1,6 +1,37 @@ import { type Movie } from '@/types/moviedb.js'; import { format } from 'date-fns'; +/** + * Mastodon has a DEFAULT character limit of 500. It can be changed per + * instance. + * + * There is no 'short' version of the overview in the API so we have to + * approximate a max length for it. + * + * @param {string} overview + * @return {*} {string} + */ +export function getMovieOverview(overview: string): string { + if (overview.length <= 350) { + return overview; + } + + return `${overview.substring(0, 347)}...`; +} + +/** + * Generate a message with the movie title, release date, overview, and + * a link to the movie's page on The Movie Database. + * + * @export + * @param {Movie} { + * id, + * overview, + * release_date, + * title, + * } + * @return {*} + */ export function getMovieInfo({ id, overview, @@ -8,14 +39,7 @@ export function getMovieInfo({ title, }: Movie) { const line1 = `${title} (${format(new Date(release_date), 'PPP')})`; - /** - * Mastodon has a DEFAULT character limit of 500. It can be changed per - * instance. - * - * There is no 'short' version of the overview in the API so we have to - * approximate a max length for it. - */ - const line2 = overview.substring(0, 350); + const line2 = getMovieOverview(overview); const line3 = `https://www.themoviedb.org/movie/${id}`; const line4 = '#movie #film #cinema';