dawarich/spec/services/imports/source_detector_spec.rb

199 lines
6.5 KiB
Ruby
Raw Permalink Normal View History

2025-08-22 13:10:40 -04:00
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Imports::SourceDetector do
let(:detector) { described_class.new(file_content, filename) }
let(:filename) { nil }
describe '#detect_source' do
context 'with Google Semantic History format' do
let(:file_content) { file_fixture('google/semantic_history.json').read }
it 'detects google_semantic_history format' do
expect(detector.detect_source).to eq(:google_semantic_history)
end
end
context 'with Google Records format' do
let(:file_content) { file_fixture('google/records.json').read }
it 'detects google_records format' do
expect(detector.detect_source).to eq(:google_records)
end
end
context 'with Google Phone Takeout format' do
2025-08-23 10:07:15 -04:00
let(:file_content) { file_fixture('google/phone-takeout_w_3_duplicates.json').read }
2025-08-22 13:10:40 -04:00
it 'detects google_phone_takeout format' do
expect(detector.detect_source).to eq(:google_phone_takeout)
end
end
context 'with Google Phone Takeout array format' do
let(:file_content) { file_fixture('google/location-history.json').read }
it 'detects google_phone_takeout format' do
expect(detector.detect_source).to eq(:google_phone_takeout)
end
end
context 'with GeoJSON format' do
let(:file_content) { file_fixture('geojson/export.json').read }
it 'detects geojson format' do
expect(detector.detect_source).to eq(:geojson)
end
end
context 'with OwnTracks REC file' do
let(:file_content) { file_fixture('owntracks/2024-03.rec').read }
let(:filename) { 'test.rec' }
it 'detects owntracks format' do
expect(detector.detect_source).to eq(:owntracks)
end
end
context 'with OwnTracks content without .rec extension' do
let(:file_content) { '{"_type":"location","lat":52.225,"lon":13.332}' }
let(:filename) { 'test.json' }
it 'detects owntracks format based on content' do
expect(detector.detect_source).to eq(:owntracks)
end
end
context 'with GPX file' do
let(:file_content) { file_fixture('gpx/gpx_track_single_segment.gpx').read }
let(:filename) { 'test.gpx' }
it 'detects gpx format' do
expect(detector.detect_source).to eq(:gpx)
end
end
0.36.0 (#1952) * Implement OmniAuth GitHub authentication * Fix omniauth GitHub scope to include user email access * Remove margin-bottom * Implement Google OAuth2 authentication * Implement OIDC authentication for Dawarich using omniauth_openid_connect gem. * Add patreon account linking and patron checking service * Update docker-compose.yml to use boolean values instead of strings * Add support for KML files * Add tests * Update changelog * Remove patreon OAuth integration * Move omniauthable to a concern * Update an icon in integrations * Update changelog * Update app version * Fix family location sharing toggle * Move family location sharing to its own controller * Update changelog * Implement basic tagging functionality for places, allowing users to categorize and label places with custom tags. * Add places management API and tags feature * Add some changes related to places management feature * Fix some tests * Fix sometests * Add places layer * Update places layer to use Leaflet.Control.Layers.Tree for hierarchical layer control * Rework tag form * Add hashtag * Add privacy zones to tags * Add notes to places and manage place tags * Update changelog * Update e2e tests * Extract tag serializer to its own file * Fix some tests * Fix tags request specs * Fix some tests * Fix rest of the tests * Revert some changes * Add missing specs * Revert changes in place export/import code * Fix some specs * Fix PlaceFinder to only consider global places when finding existing places * Fix few more specs * Fix visits creator spec * Fix last tests * Update place creating modal * Add home location based on "Home" tagged place * Save enabled tag layers * Some fixes * Fix bug where enabling place tag layers would trigger saving enabled layers, overwriting with incomplete data * Update migration to use disable_ddl_transaction! and add up/down methods * Fix tag layers restoration and filtering logic * Update OIDC auto-registration and email/password registration settings * Fix potential xss
2025-11-24 13:45:09 -05:00
context 'with KML file' do
let(:file_content) { file_fixture('kml/points_with_timestamps.kml').read }
let(:filename) { 'test.kml' }
it 'detects kml format' do
expect(detector.detect_source).to eq(:kml)
end
end
2025-08-22 13:10:40 -04:00
context 'with invalid JSON' do
let(:file_content) { 'invalid json content' }
it 'returns nil for invalid JSON' do
expect(detector.detect_source).to be_nil
end
end
context 'with unknown JSON format' do
let(:file_content) { '{"unknown": "format", "data": []}' }
it 'returns nil for unknown format' do
expect(detector.detect_source).to be_nil
end
end
context 'with empty content' do
let(:file_content) { '' }
it 'returns nil for empty content' do
expect(detector.detect_source).to be_nil
end
end
end
describe '#detect_source!' do
context 'with valid format' do
let(:file_content) { file_fixture('google/records.json').read }
it 'returns the detected format' do
expect(detector.detect_source!).to eq(:google_records)
end
end
context 'with unknown format' do
let(:file_content) { '{"unknown": "format"}' }
it 'raises UnknownSourceError' do
expect { detector.detect_source! }.to raise_error(
Imports::SourceDetector::UnknownSourceError,
'Unable to detect file format'
)
end
end
end
2025-08-22 14:40:06 -04:00
describe '.new_from_file_header' do
2025-08-22 13:10:40 -04:00
context 'with Google Records file' do
let(:fixture_path) { file_fixture('google/records.json').to_s }
it 'detects source correctly from file path' do
2025-08-22 14:40:06 -04:00
detector = described_class.new_from_file_header(fixture_path)
2025-08-22 13:10:40 -04:00
expect(detector.detect_source).to eq(:google_records)
end
it 'can detect source efficiently from file' do
2025-08-22 14:40:06 -04:00
detector = described_class.new_from_file_header(fixture_path)
2025-08-23 10:07:15 -04:00
2025-08-22 13:10:40 -04:00
# Verify it can detect correctly using file-based approach
expect(detector.detect_source).to eq(:google_records)
end
end
context 'with GeoJSON file' do
let(:fixture_path) { file_fixture('geojson/export.json').to_s }
it 'detects source correctly from file path' do
2025-08-22 14:40:06 -04:00
detector = described_class.new_from_file_header(fixture_path)
2025-08-22 13:10:40 -04:00
expect(detector.detect_source).to eq(:geojson)
end
end
0.36.0 (#1952) * Implement OmniAuth GitHub authentication * Fix omniauth GitHub scope to include user email access * Remove margin-bottom * Implement Google OAuth2 authentication * Implement OIDC authentication for Dawarich using omniauth_openid_connect gem. * Add patreon account linking and patron checking service * Update docker-compose.yml to use boolean values instead of strings * Add support for KML files * Add tests * Update changelog * Remove patreon OAuth integration * Move omniauthable to a concern * Update an icon in integrations * Update changelog * Update app version * Fix family location sharing toggle * Move family location sharing to its own controller * Update changelog * Implement basic tagging functionality for places, allowing users to categorize and label places with custom tags. * Add places management API and tags feature * Add some changes related to places management feature * Fix some tests * Fix sometests * Add places layer * Update places layer to use Leaflet.Control.Layers.Tree for hierarchical layer control * Rework tag form * Add hashtag * Add privacy zones to tags * Add notes to places and manage place tags * Update changelog * Update e2e tests * Extract tag serializer to its own file * Fix some tests * Fix tags request specs * Fix some tests * Fix rest of the tests * Revert some changes * Add missing specs * Revert changes in place export/import code * Fix some specs * Fix PlaceFinder to only consider global places when finding existing places * Fix few more specs * Fix visits creator spec * Fix last tests * Update place creating modal * Add home location based on "Home" tagged place * Save enabled tag layers * Some fixes * Fix bug where enabling place tag layers would trigger saving enabled layers, overwriting with incomplete data * Update migration to use disable_ddl_transaction! and add up/down methods * Fix tag layers restoration and filtering logic * Update OIDC auto-registration and email/password registration settings * Fix potential xss
2025-11-24 13:45:09 -05:00
context 'with KML file' do
let(:fixture_path) { file_fixture('kml/points_with_timestamps.kml').to_s }
it 'detects source correctly from file path' do
detector = described_class.new_from_file_header(fixture_path)
expect(detector.detect_source).to eq(:kml)
end
end
2025-08-22 13:10:40 -04:00
end
describe 'detection accuracy with real fixture files' do
shared_examples 'detects format correctly' do |expected_format, fixture_path|
it "detects #{expected_format} format for #{fixture_path}" do
file_content = file_fixture(fixture_path).read
filename = File.basename(fixture_path)
detector = described_class.new(file_content, filename)
expect(detector.detect_source).to eq(expected_format)
end
end
# Test various Google Semantic History variations
include_examples 'detects format correctly', :google_semantic_history, 'google/location-history/with_activitySegment_with_startLocation.json'
include_examples 'detects format correctly', :google_semantic_history, 'google/location-history/with_placeVisit_with_location_with_coordinates.json'
# Test GeoJSON variations
include_examples 'detects format correctly', :geojson, 'geojson/export_same_points.json'
include_examples 'detects format correctly', :geojson, 'geojson/gpslogger_example.json'
# Test GPX files
include_examples 'detects format correctly', :gpx, 'gpx/arc_example.gpx'
include_examples 'detects format correctly', :gpx, 'gpx/garmin_example.gpx'
include_examples 'detects format correctly', :gpx, 'gpx/gpx_track_multiple_segments.gpx'
0.36.0 (#1952) * Implement OmniAuth GitHub authentication * Fix omniauth GitHub scope to include user email access * Remove margin-bottom * Implement Google OAuth2 authentication * Implement OIDC authentication for Dawarich using omniauth_openid_connect gem. * Add patreon account linking and patron checking service * Update docker-compose.yml to use boolean values instead of strings * Add support for KML files * Add tests * Update changelog * Remove patreon OAuth integration * Move omniauthable to a concern * Update an icon in integrations * Update changelog * Update app version * Fix family location sharing toggle * Move family location sharing to its own controller * Update changelog * Implement basic tagging functionality for places, allowing users to categorize and label places with custom tags. * Add places management API and tags feature * Add some changes related to places management feature * Fix some tests * Fix sometests * Add places layer * Update places layer to use Leaflet.Control.Layers.Tree for hierarchical layer control * Rework tag form * Add hashtag * Add privacy zones to tags * Add notes to places and manage place tags * Update changelog * Update e2e tests * Extract tag serializer to its own file * Fix some tests * Fix tags request specs * Fix some tests * Fix rest of the tests * Revert some changes * Add missing specs * Revert changes in place export/import code * Fix some specs * Fix PlaceFinder to only consider global places when finding existing places * Fix few more specs * Fix visits creator spec * Fix last tests * Update place creating modal * Add home location based on "Home" tagged place * Save enabled tag layers * Some fixes * Fix bug where enabling place tag layers would trigger saving enabled layers, overwriting with incomplete data * Update migration to use disable_ddl_transaction! and add up/down methods * Fix tag layers restoration and filtering logic * Update OIDC auto-registration and email/password registration settings * Fix potential xss
2025-11-24 13:45:09 -05:00
# Test KML files
include_examples 'detects format correctly', :kml, 'kml/points_with_timestamps.kml'
include_examples 'detects format correctly', :kml, 'kml/linestring_track.kml'
include_examples 'detects format correctly', :kml, 'kml/gx_track.kml'
include_examples 'detects format correctly', :kml, 'kml/multigeometry.kml'
2025-08-22 13:10:40 -04:00
end
end