How to write a test against randomly selected values from a collection?

I have a function which selects a random value from an array to be returned:

export function selectMovieUnderDuration(options: IStreamRequest, movies: Movie[], prevMovies: Movie[], duration: number): Movie {
    let filteredMovies: Movie[] = movies.filter(movie =>
        movie.Tags.some(tag => options.Tags.includes(tag)) &&
        movie.DurationLimit <= duration
    );

    let notRepeatMovies: Movie[] = filteredMovies.filter((item) => !prevMovies.some((obj) => obj.LoadTitle === item.LoadTitle));

    let selectedMovie: Movie = notRepeatMovies.length > 0 ?
        notRepeatMovies[Math.floor(Math.random() * notRepeatMovies.length)] :
        filteredMovies[Math.floor(Math.random() * filteredMovies.length)];

    return selectedMovie;
}

I know this works correctly after many live tests, but I would like to write some automated tests for this in mocha/chai.

I already have a couple tests that cover basic functionality:

describe('selectMovieUnderDuration', () => {
    it('should return a movie that is under the duration limit that has the tags in the request object', () => {
        let movies: Movie[] = [inception, matrix, interstellar, dune];
        let prevMovies: Movie[] = [];
        let args: ContStreamRequest = new ContStreamRequest('password', 'title', 'env', [], ['Sci-Fi']);

        let result = proEng.selectMovieUnderDuration(args, movies, prevMovies, 9000);

        expect(result).to.be.equal(inception);
    });

    it('should return a movie that is under the duration limit that has the tags in the request object and has not been selected before', () => {
        let movies: Movie[] = [inception, matrix, interstellar, dune];
        let prevMovies: Movie[] = [inception, matrix, dune];
        let args: ContStreamRequest = new ContStreamRequest('password', 'title', 'env', [], ['Sci-Fi', 'Action']);
        
        let result = proEng.selectMovieUnderDuration(args, movies, prevMovies, 10800);

        expect(result).to.be.equal(interstellar);
    });

});

However, I now want to write at least one test where all the items available in the “movies” array are also in the “prevMovies” array, so it is forced to pick any value that would have previously been limited by the “prevMovies” array.

I know I could write something like:

    it('should return any movie that is under the duration limit that has the correct tags, when all movies with the criteria have been selected before', () => {
        let movies: Movie[] = [inception, matrix, interstellar, dune];
        let prevMovies: Movie[] = [inception, matrix, interstellar, dune];
        let args: ContStreamRequest = new ContStreamRequest('password', 'title', 'env', [], ['Sci-Fi', 'Action']);

        let result = proEng.selectMovieUnderDuration(args, movies, prevMovies, 9000);

        expect(movies).to.be.oneOf([inception, matrix]);
    });

While this is great for this scenario because I know the function operates correctly, if the function did not operate correctly and I wrote this test, and sometimes the function would return dune or interstellar instead but only sometimes, the test would only fail sometimes.

Is there a way to make sure the test either consistently passes or consistently fails? I am considering that I might have to rewrite the function to break out certain parts and making them individually testable, but that seems extremely granular for how simple the function is already.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật