/* * Copyright (C) 2026 - present Instructure, Inc. * * This file is part of Canvas. * * Canvas is free software: you can redistribute it and/or modify it under * the terms of the GNU Affero General Public License as published by the Free * Software Foundation, version 3 of the License. * * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more * details. * * You should have received a copy of the GNU Affero General Public License along * with this program. If not, see . */ import {CC_FILE_MAX_BYTES} from '../../../shared/constants' import {defaultClosedCaptionPanelTranslations} from '../../translations' import {validateCaptionFile} from '../validation' function fileOfSize(name: string, size: number): File { return new File([new ArrayBuffer(size)], name) } describe('validateCaptionFile', () => { it('accepts a .vtt file within the size limit', () => { expect(validateCaptionFile(fileOfSize('captions.vtt', 100))).toEqual({valid: true}) }) it('accepts a .srt file within the size limit', () => { expect(validateCaptionFile(fileOfSize('captions.srt', 100))).toEqual({valid: true}) }) it('rejects a file over the size limit using the default English message', () => { const result = validateCaptionFile(fileOfSize('captions.vtt', CC_FILE_MAX_BYTES + 1)) expect(result).toEqual({ valid: false, error: `The selected file exceeds the ${CC_FILE_MAX_BYTES} Byte limit`, }) }) it('rejects a file with an unsupported extension using the default English message', () => { const result = validateCaptionFile(fileOfSize('captions.txt', 100)) expect(result).toEqual({valid: false, error: 'Please select a .vtt or .srt file'}) }) it('routes both error messages through a custom translations object', () => { const customTranslations = { ...defaultClosedCaptionPanelTranslations, fileSizeExceeded: ({maxSize}: {maxSize: number}) => `CUSTOM_TOO_BIG_${maxSize}`, invalidFileType: () => 'CUSTOM_BAD_TYPE', } expect( validateCaptionFile(fileOfSize('captions.vtt', CC_FILE_MAX_BYTES + 1), customTranslations) .error, ).toEqual(`CUSTOM_TOO_BIG_${CC_FILE_MAX_BYTES}`) expect(validateCaptionFile(fileOfSize('captions.txt', 100), customTranslations).error).toEqual( 'CUSTOM_BAD_TYPE', ) }) })