Skip to content
Closed
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
6 changes: 3 additions & 3 deletions extension/background/canvas-crawler.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ function stripHtml(html) {
.trim();
}

async function crawlCanvasCourse(origin, courseId, fetchImpl = fetch) {
async function crawlCanvasCourse(origin, courseId) {
if (!origin || !courseId) {
throw new Error('Canvas origin and courseId are required for course crawling.');
}

// 1. Fetch Course details & syllabus
const courseUrl = `${origin}/api/v1/courses/${courseId}?include[]=syllabus_body`;
const courseRes = await fetchImpl(courseUrl, { credentials: 'include' });
const courseRes = await fetch(courseUrl, { credentials: 'include' });
if (!courseRes.ok) {
throw new Error(`Failed to fetch Canvas course info (${courseRes.status})`);
}
Expand All @@ -30,7 +30,7 @@ async function crawlCanvasCourse(origin, courseId, fetchImpl = fetch) {
const assignmentsUrl = `${origin}/api/v1/courses/${courseId}/assignments?per_page=50`;
let assignments = [];
try {
const assignRes = await fetchImpl(assignmentsUrl, { credentials: 'include' });
const assignRes = await fetch(assignmentsUrl, { credentials: 'include' });
if (assignRes.ok) {
const assignJson = await assignRes.json();
if (Array.isArray(assignJson)) {
Expand Down
6 changes: 3 additions & 3 deletions extension/background/canvas-crawler.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ export function stripHtml(html) {
.trim();
}

export async function crawlCanvasCourse(origin, courseId, fetchImpl = fetch) {
export async function crawlCanvasCourse(origin, courseId) {
if (!origin || !courseId) {
throw new Error('Canvas origin and courseId are required for course crawling.');
}

// 1. Fetch Course details & syllabus
const courseUrl = `${origin}/api/v1/courses/${courseId}?include[]=syllabus_body`;
const courseRes = await fetchImpl(courseUrl, { credentials: 'include' });
const courseRes = await fetch(courseUrl, { credentials: 'include' });
if (!courseRes.ok) {
throw new Error(`Failed to fetch Canvas course info (${courseRes.status})`);
}
Expand All @@ -30,7 +30,7 @@ export async function crawlCanvasCourse(origin, courseId, fetchImpl = fetch) {
const assignmentsUrl = `${origin}/api/v1/courses/${courseId}/assignments?per_page=50`;
let assignments = [];
try {
const assignRes = await fetchImpl(assignmentsUrl, { credentials: 'include' });
const assignRes = await fetch(assignmentsUrl, { credentials: 'include' });
if (assignRes.ok) {
const assignJson = await assignRes.json();
if (Array.isArray(assignJson)) {
Expand Down
13 changes: 9 additions & 4 deletions test/test-crawler.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ const mockFetch = async (url, options) => {
};

(async () => {
const courseData = await crawlCanvasCourse('https://canvas.instructure.com', '12345', mockFetch);
const originalFetch = global.fetch;
global.fetch = mockFetch;

const courseData = await crawlCanvasCourse('https://canvas.instructure.com', '12345');
assert.strictEqual(courseData.title, 'Biology 101: Cell Systems');
assert.ok(fetchOptionsPassed.every(opt => opt && opt.credentials === 'include'), 'fetchImpl must include credentials');
assert.ok(courseData.syllabus.includes('Analyze cellular metabolism'));
Expand All @@ -43,7 +46,7 @@ const mockFetch = async (url, options) => {
// Test 2.5: Missing Error Path Test for crawlCanvasCourse
let caughtError1 = false;
try {
await crawlCanvasCourse(null, '12345', mockFetch);
await crawlCanvasCourse(null, '12345');
} catch (err) {
caughtError1 = true;
assert.strictEqual(err.message, 'Canvas origin and courseId are required for course crawling.');
Expand All @@ -52,7 +55,7 @@ const mockFetch = async (url, options) => {

let caughtError2 = false;
try {
await crawlCanvasCourse('https://canvas.instructure.com', null, mockFetch);
await crawlCanvasCourse('https://canvas.instructure.com', null);
} catch (err) {
caughtError2 = true;
assert.strictEqual(err.message, 'Canvas origin and courseId are required for course crawling.');
Expand All @@ -75,12 +78,14 @@ const mockFetch = async (url, options) => {

let errorThrown = false;
try {
await crawlCanvasCourse('https://canvas.instructure.com', 'invalid-id', mockFailedFetch);
global.fetch = mockFailedFetch;
await crawlCanvasCourse('https://canvas.instructure.com', 'invalid-id');
} catch (err) {
errorThrown = true;
assert.strictEqual(err.message, 'Failed to fetch Canvas course info (404)');
}
assert.ok(errorThrown, 'Expected an error to be thrown for failed fetch');
global.fetch = originalFetch;

console.log('✅ Task 2 Canvas crawler tests passed.');
})();
Loading